#ifndef SESSIONDBUSREGISTER_H
#define SESSIONDBUSREGISTER_H

#include "daemon.h"
#include "config.h"


#include <KF5/BluezQt/bluezqt/adapter.h>
#include <KF5/BluezQt/bluezqt/manager.h>
#include <KF5/BluezQt/bluezqt/types.h>
#include <KF5/BluezQt/bluezqt/device.h>

#ifdef BATTERY
#include <KF5/BluezQt/bluezqt/battery.h>
#endif

#include <QObject>
#include <QDBusContext>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusReply>
#include <QDebug>
#include <QString>
#include <QStringList>
#include <QTimer>


class  DelayStruct : public QObject
{
    Q_OBJECT

public:
    DelayStruct(BluezQt::DevicePtr dev, QObject *parent = nullptr);
    ~DelayStruct();

    void  close(void);  //关闭并销毁函数

protected:

    virtual void timerEvent( QTimerEvent *event);

private:
    int m_TimerID = 0;
    BluezQt::DevicePtr m_dev = nullptr;
};


class SessionDbusRegister : public QObject, protected QDBusContext
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "com.ukui.bluetooth")
public:
    explicit SessionDbusRegister(QObject *parent = nullptr);
    ~SessionDbusRegister();

private:
    QMap<QString,QMap<QString,QString>> sessionList;

    int      scanOnCount = 0;
    QTimer * scanOnTimer = nullptr;
    bool     huawei_scan_interval_flag = false;
    QString  tmpDevCord = QString("");

    bool InterfaceAlreadyExists();

    void AudioDevConnectFunc(BluezQt::DevicePtr);

    void clearDeviceScanList(BluezQt::DevicePtr);

    bool deviceMacAddressContrast(QString,QString,int);

    bool deviceMacAddressChanged(BluezQt::DevicePtr);

    void deviceConnectAgain(BluezQt::DevicePtr,bool);

    void deal_active_connection(BluezQt::DevicePtr);
private slots:

    void dealStatusChanged(QString status, QString dev, QString path);

public slots:
    //客户端id + 类型 + 用户名
    //客户端id: dbus唯一名称
    //类型 0： 控制面板， 1：托盘
    bool registerClient(QString, int, QString);
    bool unregisterClient(QString);

     // 蓝牙默认适配器信息处理dbus
     bool    getBluetoothBlock();

     QString getDefaultAdapterAddress();

     bool    getDefaultAdapterPower();

     bool    getDefaultAdapterScanStatus();

     bool    getDefaultAdapterDiscoverable();

     QString getAdapterNameByAddr(QString);

     QStringList getDefaultAdapterPairedDevAddress();

     QStringList getDefaultAdapterTrustedDevAddress();

     QStringList getDefaultAdapterCacheDevAddress();

     QStringList getAdapterDevAddressList();

     void    setDefaultAdapterPower(bool);

     void    setDefaultAdapterScanOn(bool);

     void    setDefaultAdapter(QString);

     void    setDefaultAdapterDiscoverable(bool);

     void    setBluetoothBlock(bool);

     void    setDefaultAdapterName(QString);

     void    clearNonViableDevice(QStringList);
     //蓝牙设备处理dbus
     void devPair(const QString);

     void devConnect(const QString);

     void devDisconnect(const QString);

     void devRemove(const QString);

     void devTrust(const QString,const bool);

     bool getDevPairStatus(const QString);

     bool getDevConnectStatus(const QString);

     QString getDevName(const QString);

     QString getDevType(const QString);

     void clearAllUnPairedDevicelist();

     void setSendTransferDeviceMesg(QString);

     void setSendTransferFileMesg(QStringList files);

     void setclearOldSession();

     void continueSendFiles(QString filename);

     void cancelFileReceiving();
     
//#ifdef BATTERY
     qint64  getDevBattery(const QString);
//#endif
     //设备是否支持文件发送
     bool getDevSupportFileSend(const QString);
     qint16 getDevRssi(const QString);
     //文件传输dbus
     void sendFiles(QString,QString);//test

     void exit();

     //其他dbus
     void sendNotifyMessage(QString);

     void openBluetoothSettings();

     void connectSignal();

     void showTrayWidgetUI();

     bool getTransferDevAndStatus(QString dev);

     //test
     void sendContinueRecieveFilesSignal();

     void sendReplyRequestConfirmation(bool);

     void sendReplyFileReceivingSignal(bool v);

     void sendCloseSession();

     //主动连接请求, v： 接受/拒绝
     void activeConnectionReply(QString dev, bool v);

signals:
     //适配器相关信号
     void defaultAdapterPowerChanged(bool);

     void defaultAdapterChanged(QString);

     void defaultScanStatusChanged(bool);

     void defaultDiscoverableChanged(bool);

     void adapterAddSignal(QString);

     void adapterRemoveSignal(QString);

     void defaultAdapterNameChanged(QString);

     //设备相关信号
     void deviceScanResult(QString,QString,QString,bool,qint16);

     void devPairSignal(QString,bool);

     void devTypeChangedSignal(QString,QString);

     void devBatteryChangedSignal(QString, QString);

     void devNameChangedSignal(QString,QString);

     void devConnectStatusSignal(QString,bool);

     void devRemoveSignal(QString);

     void devLaunchConnecting(QString);

     void devOperateErrorSignal(QString,int,QString);

     void devMacAddressChangedSignal(QString,QString);

     void devRssiChangedSignal(QString,qint16);
     //代理相关信号
     void sendFile(QString,QString);

     void continueSendFilesSignal(QString);

     void displayPasskey(QString,QString);

//     void replyDisplayPasskey(bool);

     void requestConfirmation(QString,QString);

     void replyRequestConfirmation(bool);

     void transferredChanged(quint64,QString);

     void statusChanged(QString,QString,QString);

     void fileReceivingSignal(QString,QString,QString,QString,quint64);

     void replyFileReceivingSignal(bool);

     void continueRecieveFilesSignal();

     void closeSessionSignal();

     void pairAgentCanceled();

     void obexAgentCanceled();

     void propertyChanged(quint64);

     void sendTransferDeviceMesg(QString);

     void sendTransferFilesMesg(QStringList);

     void clearOldSession();

     void cancelFileReceivingSignal();

     void showTrayWidgetUISignal();

     void initTransferPath(QString, bool);

     void powerProgress(bool);

     void clearBluetoothDev(QStringList);

     //id, name , type, rssi, timeout
     void ActiveConnection(QString, QString, QString, int, int);
};

#endif // SESSIONDBUSREGISTER_H
