#ifndef BLUETOOTHDBUS_H
#define BLUETOOTHDBUS_H

#include <glib.h>
#include <gio/gio.h>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QDBusConnection>
#include <QDBusReply>
#include <QDBusMessage>
#include <QMap>
#include <QProcess>
#include <QDBusConnectionInterface>

#define SYSTEMD_SERVICE         "com.ukui.bluetooth"
#define SYSTEMD_PATH            "/com/ukui/bluetooth"
#define SYSTEMD_INTERFACE       SYSTEMD_SERVICE

#define CONNECT_DBUS_SIGNAL(signame, func) \
                                    QDBusConnection::sessionBus().connect(SYSTEMD_SERVICE, \
                                    SYSTEMD_PATH, \
                                    SYSTEMD_INTERFACE, \
                                    signame, \
                                    this,\
                                    SLOT(func))

#define TRANSFER_DBUS_SIGNAL(signame, func) \
                                    QDBusConnection::sessionBus().connect(SYSTEMD_SERVICE, \
                                    SYSTEMD_PATH, \
                                    SYSTEMD_INTERFACE, \
                                    signame, \
                                    this,\
                                    SIGNAL(func))

#define CREATE_METHOD_CALL(funcName) \
                                    QDBusMessage::createMethodCall(SYSTEMD_SERVICE, \
                                    SYSTEMD_PATH, \
                                    SYSTEMD_INTERFACE, \
                                    funcName)

#define CALL_METHOD(dbusmsg) QDBusConnection::sessionBus().call(dbusmsg);

class BluetoothDbus : public QObject
{
    Q_OBJECT
public:
    explicit BluetoothDbus(QObject *parent = nullptr);
    ~BluetoothDbus();
    void setDeviceList(QStringList devicelist);
    void setDeviceType(QStringList typelist);

public:
    //DBus调用函数
    void getDefaultAdapterPower();
    void getDefaultAdapterPairedDevices();
    void getDevConnectStatus(QString address);
    void getTrayIconShow();
    void setDefaultAdapterPower(bool power);

    void closeSession();
    void clearOldSession();
    void closeSessionSignal();
    void openBluetoothSettings();
    void devConnect(const QString address);
    void devDisconnect(const QString address);
    void sendFiles(QString address, QString filename);

    void cancelFileReceiving();
    void replyFileReceiving(bool v);
    void continueRecieveFilesRequest();
    void continueSendFiles(QString filename);
    void replyRequestConfirmation(bool accept);
    void replyFileReceivingSignal(bool accept);
    void activeConnectionReply(QString dev, bool v);

    //返回信息
    bool isPowered();
    bool isConnected(QString address);
    bool getDevPairStatus(QString address);
    QMap<QString, QStringList> getPairedDevices();
    QMap<QString, QStringList> getSendableDevices();
    QList<QString> getPairedDevicesList();

signals:
    void powerProgressSignal(bool);
    void showTrayIcon(bool);
    void adapterAddSignal(QString);
    void adapterRemoveSignal(QString);
    void powerChangedSignal(bool);
    void displayPasskeySignal(QString,QString);
    void receiveFilesSignal(QString, QString,QString,QString,QString,quint64);
    void requestConfirmationSignal(QString,QString);
    void transferredChangedSignal(quint64,QString);
    void statusChangedSignal(QString,QString,QString);
    void initTransferPathSignal(QString,bool);
    void pairAgentCanceledSignal();
    void obexAgentCanceledSignal();
    void devRemoveSignal(QString);
    void devNameChangedSignal();
    void devBatteryChangedSignal();
    void sessionDevPairSignal(QString, bool);
    void sendTransferFilesMesgSignal(QStringList);
    void sendTransferDeviceMesgSignal(QString);
    void AdapterChangedSignal(QString);
    void devConnectStatusSignal(QString, bool);
    void fileReceiveMore(QString, quint64, QString);
    void showTrayWidgetUISignal();
    void activeConnectionSignal(QString,QString,QString,int,int);
    void devLaunchConnectingSignal(QString);

private:
    void connectDBusSignals();
    void addNewPairedDevice(QString address);
    bool powered = false;
    QList<QString> pairedList;
    QMap<QString, QStringList> pairedDevice;
    QMap<QString, QString> connectedDevice;

private slots:
    bool getDevSupportFileSend(QString address);
    void adapterAddSignalSLot(QString address);
    void adapterRemoveSignalSLot(QString address);
    void defaultAdapterPowerChangedSLot(bool value);
    void defaultAdapterChangedSLot(QString value);
    void devPairSignalSLot(QString address, bool paired);
    void devConnectStatusSignalSLot(QString address, bool connected);
    void devRemoveSignalSLot(QString address);
    void devErrorSignalSLot(QString);
    void devNameChangedSignalSLot(QString address, QString name);
    void devBatteryChangedSignalSLot(QString address, QString battery);
    void receiveFilesSlot(QString destination, QString filename, QString type, QString source, quint64 size);
};

#endif // BLUETOOTHDBUS_H
