#ifndef BLUETOOTHMAIN_H
#define BLUETOOTHMAIN_H

#include <QQueue>
#include <QVector>
#include <QList>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QString>
#include <QDebug>
#include <QLabel>
#include <QFrame>
#include <QSystemTrayIcon>
#include <QFont>
#include <QMetaType>
#include <QSizePolicy>
#include <QWidget>
#include <QScrollArea>
#include <QMenu>
#include <QTimer>
#include <QVariant>
#include <QProcess>
#include <QComboBox>
#include <QMetaEnum>
#include <QVector>
#include <QStringView>
#include <QLayoutItem>
#include <QMouseEvent>
#include <QStackedWidget>
#include <QDBusObjectPath>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusAbstractAdaptor>
#include <QtDBus/QDBusConnectionInterface>
#include <polkit-qt5-1/PolkitQt1/Authority>
#include <QGSettings/QGSettings>

#include <QMetaEnum>
#include <QTranslator>
#include <QApplication>
#include <QDBusMessage>
#include <QMessageLogger>
#include <QStandardPaths>

#include <ukcc/widgets/switchbutton.h>
#include <ukcc/widgets/titlelabel.h>
#include <ukcc/widgets/imageutil.h>


#include "config.h"
#include "devicebase.h"
#include "deviceinfoitem.h"
#include "bluetoothnamelabel.h"
#include "ukccbluetoothconfig.h"
#include "bluetoothdbusservice.h"

#include "kswitchbutton.h"
using namespace kdk;

#define GSETTING_STR_ACTIVE_CONNECTION               "active-connection"
#define GSETTING_STR_ACTIVE_CONNECTION_TRAN          "activeConnection"

class BlueToothMain : public QMainWindow
{
    Q_OBJECT
public:
    enum DevTypeShow {
        All = 0,
        Audio,
        Peripherals,
        PC,
        Phone,
        Other,
    };
    Q_ENUM(DevTypeShow)

    BlueToothMain(QWidget *parent = nullptr);
    ~BlueToothMain();
protected:
    void leaveEvent(QEvent *event);

//dev-3.1 界面服务分离

public:
    static bool m_device_operating ; //有设备正在操作
    static QString m_device_operating_address ;//正在操作设备的mac地址

    static bool m_device_pin_flag ;
    void setbluetoothAdapterDiscoveringStatus(bool);
signals:
    void sendAdapterNameChangeSignal(const QString &value);
    void defaultAdapterNameChanged(const QString &name);

private slots:
    //report
    void reportDefaultAdapterChanged(QString);
    void reportDefaultAdapterNameChanged(QString);
    void reportAdapterAddSignal(QString );
    void reportAdapterRemoveSignal(QString);
    void reportPowerProgress(bool);
    void reportDefaultAdapterPowerChanged(bool);
    void reportDefaultDiscoverableChanged(bool);
    void reportDefaultScanStatusChanged(bool);
    void reportDeviceScanResult(QString,QString,QString,bool,qint16);

    void reportDevRemoveSignal(QString);
    void reportDevPairSignal(QString,bool);
    void reportDevConnectStatusSignal(QString,bool);
    void reportDevNameChangedSignal(QString,QString);
    void reportDevTypeChangedSignal(QString,QString);
    void reportRequestConfirmation(QString,QString);
    void reportReplyRequestConfirmation(bool);
    void reportDevOperateErrorSignal(QString,int,QString);

    void reportDevMacAddressChangedSignal(QString,QString);

    void reportClearBluetoothDev(QStringList);

    void reportDevRssiChangedSignal(QString,qint16);

    void reportDevLaunchConnecting(QString);
    //receive
    void receiveConnectsignal(QString);
    void receiveDisConnectSignal(QString);
    void receiveRemoveSignal(QString);

    //gsetting
    void setTrayVisible(bool);
    void onClick_Open_Bluetooth(bool);

    void changeDeviceParentWindow(const QString &address);
    void refreshLoadLabelIcon();
    void refreshWaitLabelIcon();

    void monitorSleepSlot(bool value);
    void changeListOfDiscoveredDevices(int);
    void receiveSendFileSignal(QString);
    void gSettingsChanged(const QString &);
    void adapterPoweredChanged(bool value);
    void adapterComboxChanged(int i);
    void adapterListComboxEnabledState(bool);

    void gSettingsBluetoothChanged(const QString &);
    void longTimeNoDevAddSlots();
private:
    QStackedWidget      * _MCentralWidget = nullptr;

    QWidget             * frame_top = nullptr;
    QWidget             * frame_middle = nullptr;
    QWidget             * frame_bottom = nullptr;
    QWidget             * m_normal_main_widget = nullptr;
    QWidget             * m_error_main_widget = nullptr;
    QVBoxLayout         * errorWidgetLayout = nullptr;
    QLabel              * errorWidgetIcon = nullptr;
    QLabel              * errorWidgetTip0 = nullptr;
    QFrame              * line_frame2 = nullptr;
    QFrame              * mDev_frame = nullptr;
    QFrame              * device_list = nullptr;
    QVBoxLayout         * device_list_layout = nullptr;
    BluetoothNameLabel  * bluetooth_name = nullptr;

    QVBoxLayout         * paired_dev_layout = nullptr;
    QVBoxLayout         * main_layout = nullptr;
    QFrame              * frame_2 = nullptr;
    QComboBox           * cacheDevTypeList = nullptr;
    QLabel              * label_2 = nullptr;
    QLabel              * loadLabel = nullptr;
    QLabel              * waitLabel = nullptr;

    //QString     Default_Adapter;
    QGSettings * m_settings = nullptr;
    QGSettings * styleSettings = nullptr;

    int         i = 7;
    int         cnt = 7;
    bool        sleep_status = false;
    bool        m_myDev_show_flag = false;
    bool        isblack = false;


    bool        m_service_dbus_adapter_power_change_flag = false;
    bool        m_service_dbus_adapter_discovery_change_flag = false;

    bool        m_current_adapter_power_swtich;
    bool        m_current_adapter_disconvery_swtich;
    bool        m_current_bluetooth_block_status;
    bool        m_current_adapter_scan_status;          //正在扫描：true，否则false
    bool        m_current_active_connection_status = false;

    QString     m_default_adapter_name;
    QString     m_default_adapter_address;

    QStringList m_adapter_name_list;
    QStringList m_adapter_address_list;

    //QStringList last_discovery_device_address;
    QStringList m_default_adapter_paired_device_address_list;      //默认适配器的配对列表
    QStringList m_discovery_device_address_all_list;        //所有device mac list
    QStringList m_load_device_address_list;                 //加载的所有的device list

    KSwitchButton * m_open_bluetooth_btn = nullptr;
    KSwitchButton * m_show_panel_btn = nullptr;
    KSwitchButton * m_discover_switch_btn = nullptr;
    KSwitchButton * m_active_scan_switch_btn = nullptr;

    QComboBox    * m_adapter_list_cmbox = nullptr;
    QComboBox    * m_cache_dev_type_list_cmbox = nullptr;

    bluetoothadapter * m_default_bluetooth_adapter = nullptr ;
    QList <bluetoothadapter *> m_bluetooth_adapter_list;
    //QList <bluetoothdevice  *> m_bluetooth_device_list;

    DevTypeShow discoverDevFlag = DevTypeShow::All;

    QTimer   * m_timer = nullptr;
    QTimer   * w_timer = nullptr;
    QTimer   * delayStartDiscover_timer = nullptr;
    //用于实时监控蓝牙适配器状态的计时器
    //长时间未收到新增设备时检测当前适配器是否处于扫描状态
    //若不处于扫描状态则开启
    QTimer   * longTimeNoDevAddSignal_timer = nullptr;

    QQueue <bluetoothdevice *> devPreShowQue;
    QTimer * devPreShow_timer = nullptr;
    QTimer * adapterListCmbox_timer = nullptr;
private:
    void    mDevFrameAddLineFrame(int ,QString,QString);
    void    removeMDevFrameLineFrame(QString);
    void    monitorBluetoothDbusConnection();
    void    removeDeviceItemUI(QString address);
    void    addMyDeviceItemUI(bluetoothdevice *);
    void    monitorSleepSignal();
    void    cleanPairDevices();
    void    addDiscoverDevListByFlag(DevTypeShow);

    //get adapter data
    void    connectBluetoothServiceSignal();

    void    initTimerLoadDevFunction();
    void    getAllAdapterData();
    void    getDefaultAdapterData(QString);
    QStringList getAdapterDevAddressList();
    QStringList getAdapterDevNameList();

    QString getDefaultAdapterAddress();
    QString getAdapterName(QString);

    bool    getBluetoothBlock();
    bool    getDefaultAdapterPower();
    bool    getDefaultAdapterDiscoverable();
    bool    getDefaultAdapterScanStatus();

    QStringList getDefaultAdapterPairedDevAddress();
    QStringList getDefaultAdapterCacheDevAddress();
    QString getDevName(QString);
    bluetoothdevice::DEVICE_TYPE getDeviceType(QString,QString);
    QString getDevType(QString);
    bool    getDevPairStatus(QString);
    bool    getDevConnectStatus(QString);
    bool    getDevSupportFileSend(QString);
    qint16  getDevRssi(QString);
    int     getDevAddressItemIndex(QString);
    int     getDevRssiItemInsertIndex(qint16);
    void    clearAllUnPairedDevicelist();
    void    clearNonViableDevice(QStringList);
    //QStringList getAdapterNameList();

    void    setBluetoothBlock(bool);
    void    setDefaultAdapterPower(bool);
    void    setDefaultAdapter(QString);
    void    setDefaultAdapterName(QString);
    void    setDefaultAdapterDiscoverable(bool);
    void    setDefaultAdapterScanOn(bool);

    // Dbus注册
    bool    registerClient();
    bool    unregisterClient();
    //
    void    showBluetoothNormalMainWindow();
    void    initMainWindowTopUI();
    void    initMainWindowMiddleUI();
    void    initMainWindowbottomUI();
    void    initMainWindowParameters();

    void    showBluetoothErrorMainWindow();

    void    refreshUIWhenAdapterChanged();
    void    refreshBluetoothAdapterInterfaceUI();
    bool    isInvalidDevice(QString , bluetoothdevice::DEVICE_TYPE);

    bluetoothdevice  * createOneBluetoothDeviceFromMacAddress(QString);
    bluetoothdevice  * createOneBluetoothDeviceFromBluetoothService(QString,QString,QString,bool,qint16);
    bluetoothadapter * createOneBluetoothAdapter(QString);
    bool whetherToAddCurrentInterface(bluetoothdevice *);
    void addOneBluetoothDeviceItemUi(bluetoothdevice *);

    void addAdapterDataList(QString);
    void removeAdapterDataList(QString);
    void stopAllTimer();

    void setGSettingsActiveConnectionStatus(bool);
};

#endif // BLUETOOTHMAIN_H
