#ifndef DAEMON_H
#define DAEMON_H

#include "bluetoothagent.h"
#include "bluetoothobexagent.h"
#include "config.h"
#include "rfkill.h"
#include "buriedpointdata.h"

#ifdef BATTERY
#include <KF5/BluezQt/bluezqt/battery.h>
#endif
#include <KF5/BluezQt/bluezqt/adapter.h>
#include <KF5/BluezQt/bluezqt/manager.h>
#include <KF5/BluezQt/bluezqt/initmanagerjob.h>
#include <KF5/BluezQt/bluezqt/device.h>
#include <KF5/BluezQt/bluezqt/agent.h>
#include <KF5/BluezQt/bluezqt/pendingcall.h>
#include <KF5/BluezQt/bluezqt/obexmanager.h>
#include <KF5/BluezQt/bluezqt/initobexmanagerjob.h>
#include <KF5/BluezQt/bluezqt/obexobjectpush.h>
#include <KF5/BluezQt/bluezqt/obexsession.h>
#include <KF5/BluezQt/bluezqt/obextransfer.h>

#include <QObject>
#include <QCoreApplication>
#include <QString>
#include <QUrl>
#include <QDebug>
#include <QGSettings>
#include <QMap>
#include <QStringList>
#include <QVariant>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QFileDialog>
#include <QFileSystemWatcher>
#include <QDBusObjectPath>
#include <QDBusConnection>
#include <QThread>
#include <QEventLoop>



enum DevOperate {
    PAIR = 0,
    CONNECT,
    DISCONNECT,
    REMOVE,
    TRUST,
    ERROR,
};

class Daemon : public QObject
{
    Q_OBJECT
public:
    explicit Daemon(QObject *parent = nullptr);
    ~Daemon();

    static BluezQt::AdapterPtr _DefualtAdapter;
    static BluezQt::Manager    *_BluezManager;

    static void setBluetoothBlock(bool);
//    static void devOperateFunc(const QString,const DevOperate,const bool value = false);
//    static void sendDevSignal(DevOperate,QString);
    static QDBusMessage createCustomSignalMsg(QString);
    static void reconnectFunc();
    static void devConnectSignalDbus(QString);

    static void sendWriteInData(QString,QString);

public:
    void setDefaultDiscoverble();

    void launchDefaultBluetooth(void);

Q_SIGNALS:
    void startThread();
    void writeInData(QString,QString);

private slots:
    void gsettingsChangedSlot(const QString &);
    void rfkillStatusChange(int);
    void sendFile(QString,QString);
    void closeSession();
    void continueSendFiles(QString filename);
    void monitorSleepSlot(bool);

private:
    void initGSettingInfo();
    int  initBluezManager();
    void registerBluezAgent();
    void fileTransferSessionCreator(QString,QString);
    void initObexManager();
    void registerObexAgent();
    void launchTrayUI();
    BluezQt::AdapterPtr getDefualtAdapter();
//    void connectDefualtSignal();
    void monitorDbusSignal();
    void launchDbusSignal();

    void __launch_bluetooth(void);



    QString getFileName();

    Rfkill  *_rfkill       = nullptr;
    QThread *_rfkillThread = nullptr;
    Config  *_mConfig      = nullptr;
    QMap<QString,QVariant> _mGsetting;

    BuriedPointData  *_buriedPointData       = nullptr;
    QThread *_buriedPointDataThread = nullptr;

//    QDBusMessage devScanSignal;
//    QDBusMessage adapterPowerSignal;
//    QDBusMessage adapterDiscoverableSignal;
//    QDBusMessage adapterScanStatusSignal;

    QString     selected_file;
    QDBusObjectPath pre_session;
    quint64     transfer_file_size = 0;

    BluezQt::ObexObjectPush        *opp = nullptr;
    BluezQt::ObexTransferPtr    filePtr = nullptr;
    BluezQt::ObexManager  *_ObexManager = nullptr;
    BluetoothAgent         *_BluezAgent = nullptr;
    BluetoothObexAgent      *_ObexAgent = nullptr;
    QString transferDev;
//    BluetoothFileTransferWidget *transfer_widget    = nullptr;
};

#endif // DAEMON_H
