#ifndef ZIGATEPLUGIN_H
#define ZIGATEPLUGIN_H

#include <QObject>
#include <QtPlugin>
#include <iostream>
#include "interface.h"
//#include "library.h"
#include "serial.h"
#include <QMap>
#include <QHash>
#include <QSerialPort>
#include "responseClasses.h"
#include <QtConcurrent/QtConcurrent>
#include <QObject>
#include <QtPlugin>
#include <unistd.h>
#include <QFutureWatcher>

#define DEVICE_ANNOUNCE "004D"
#define STATUS_RETURNED "8000"
#define LOG_MESSAGE "8001"
#define DATA_INDICATION "8002"
#define CLUSTERS_LIST "8003"
#define ATTRIBUTES_LIST "8004"
#define COMMANDS_LIST "8005"
#define STATUS "8006"
#define STATUS2 "8007"
#define VERSION_LIST "8010"
#define PERMIT_JOIN "8014"
#define NETWORK_JOINED_FORMED "8024"
#define BIND_RESPONSE "8030"
#define UNBIND_RESPONSE "8031"
#define NETWORK_ADDRESS "8040"
#define IEEE_ADDRESS "8041"
#define NODE_DESCRIPTOR "8042"
#define SIMPLE_DESCRIPTOR "8043"
#define POWER_DESCRIPTOR "8044"
#define ACTIVE_ENDPOINT "8045"
#define MATCH_DESCRIPTOR "8046"
#define USER_DESCRIPTOR_NOTIFY "802B"
#define USER_DESCRIPTOR "802C"
#define MANAGEMENT_LEAVE "8047"
#define LEAVE_INDICATION "8048"
#define MANAGEMENT_NETWORK_UPDATE "804A"
#define SYSTEM_SERVER_DISCOVERY "804B"
#define MANAGEMENT_LQI "804E"
#define ATTRIBUTE_DISCOVERY "8140"
#define HEARTBEAT "8008"
#define NETWORK_STATE "8009"
#define ACK_DATA "8011"
#define DATA_CONFIRM "8012"
#define DEVICES_LIST "8015"
#define TIME_SERVER "8017"
#define AUTHENTICATE_RESPONSE "8028"
#define PDM_EVENT "8035"
#define PERMIT_JOINING "8049"
#define PDM_CHILD_TABLE_SIZE "8052"
#define ADD_GROUP "8060"
#define VIEW_GROUP "8061"
#define GROUP_MEMBERSHIP "8062"
#define REMOVE_GROUP "8063"
#define LEVEL_UPDATE "8085"
#define ON_OFF "8095"
#define VIEW_SCENE "80A0"
#define ADD_SCENE "80A1"
#define REMOVE_SCENE "80A2"
#define REMOVE_ALL_SCENE "80A3"
#define STORE_SCENE "80A4"
#define SCENE_MEMBERSHIP "80A6"
#define SCENES_UPDATE  "80A7"
#define READ_INDIVIDUAL_ATTRIBUTE "8100"
#define DEFAULT_RESPONSE "8101"
#define REPORT_INDIVIDUAL_ATTRIBUTE "8102"
#define WRITE_ATTRIBUTE "8110"
#define CONFIGURE_REPORTING "8120"
#define READ_REPORT_CONFIG "8122"
#define COMPLEX_DESCRIPTOR "8531"
#define ROUTER_DISCOVERY "8701"
#define APS_DATA_CONFIRM_FAIL "8702"
#define EXTENDED_ERROR "9999"

class ZigatePlugin : public QObject, public ZigbeeManagerInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.zigbeeManagerInterface" FILE "zigateplugin.json")
    Q_INTERFACES(ZigbeeManagerInterface)


public slots:
    void interpretResult(QByteArray);
    void readDatas();

signals:

public:
    bool initPlugin() override;
    QByteArray checksum(QByteArray  msgType, QByteArray length, QByteArray datas);
    QByteArray transcode(QByteArray datas);
    QByteArray unTranscode(QByteArray datas);
    void sendCmd(QByteArray cmd, QByteArray datas);
    //int interpretResult(QByteArray datas);
    //void getResponse();
    bool resetCoordinator();
    void interpretResult();
    void infoOutput(ResponseProperties *);
    void debug(QString text, int level);
    void signal_handler(int signal);

    // commands
    void getVersion();

    QByteArray datas;    //datas not complete waiting reading new datas
    QMap <int, QString> resultCodes;
    QMap <QString, QList <QByteArray>> cmdList =
    {
        {"setHeartBeat", {"0008", "0000", ""}},
        {"getNetWorkState", {"0009", "0000", ""}},
        {"getVersion", {"0010", "0000", ""}},
        {"reset", {"0011", "0000", ""}},
        {"erasePersistentData", {"0012", "0000", ""}},
        {"factoryReset", {"0013", "0000", ""}},
        {"permitJoin", {"0014", "0000", ""}},
        {"setExpendedPANID", {"0020", "0008", ""}},
        {"setChannelMask", {"0021", "0004", ""}},
        {"setSecurityStateandKey", {"0022", "0000", ""}},
        {"setDeviceType", {"0023", "0001", ""}},
        {"startNetwork", {"0024", "0000", ""}},
        {"startNetworkScan", {"0025", "0000", ""}},
        {"removeDevice", {"0026", "0010", ""}},
        {"enablePermissionsControlledJoins", {"0027", "0001", ""}},
        {"authenticateDevice", {"0028", "0018", ""}},
        {"outOfBandCommissioningDataRequest", {"0029", "0018", ""}},
        {"userDescriptorSet", {"002B", "0000", ""}},
        {"userDescritporRequest", {"002C", "0000", ""}},
        {"bind", {"0030", "", ""}},
        {"unbind", {"0031", "", ""}},
        {"complexDescriptorRequest", {"0034", "0004", ""}},
        {"networkAddressRequest", {"0040", "0012", ""}},
        {"ieeeAddressRequest", {"0041", "0006", ""}},
        {"nodeDescriptorRequest", {"0042", "0002", ""}},
        {"simpleDescriptorRequest", {"0043", "0003", ""}},
        {"powerDescriptorRequest", {"0044", "0002", ""}},
        {"activeEndpointRequest", {"0045", "0002", ""}},
        {"matchDescriptorRequest", {"0046", "", ""}},
        {"managementLeaveRequest", {"0047", "001C", ""}},
        {"permitJoiningRequest", {"0049", "0004", ""}},
        {"managementNetworkUpdateRequest", {"004A", "000B", ""}},
        {"systemServerDiscoveryRequest", {"004B", "0004", ""}},
        {"managementLQIRequest", {"004E", "0003", ""}},
        {"addGroup", {"0060", "0007", ""}},
        {"viewGroup", {"0061", "0007", ""}},
        {"getGroupMembership", {"0062", "", ""}},
        {"removeGroup", {"0063", "0007", ""}},
        {"removeAllGroups", {"0064", "0005", ""}},
        {"addGroupIfIdentify", {"0065", "0007", ""}},
        {"identifySend", {"0070", "0007", ""}},
        {"identifyQuery", {"0071", "0005", ""}},
        {"moveToLevel", {"0080", "0008", ""}},
        {"moveToLevelWithWithoutOnOff", {"0081", "0009", ""}},
        {"moveStep", {"0082", "000A", "0000"}},
        {"moveStopMove", {"0083", "0005", ""}},
        {"moveStopWithOnOff", {"0084", "0005", ""}},
        {"onOffWithNoEffects", {"0092", "0006", ""}},
        {"onOffTimedSend", {"0093", "000A", ""}},
        {"onOffWithEffectsSend", {"0094", "0007", ""}},
        {"viewScene", {"00A0", "0008", ""}},
        {"addScene", {"00A1", "000D", ""}},
        {"removeScene", {"00A2", "0008", ""}},
        {"removeAllScenes", {"00A3", "0007", ""}},
        {"storeScene", {"00A4", "0008", ""}},
        {"recallScene", {"00A5", "0008", ""}},
        {"sceneMembershipRequest", {"00A6", "0007", ""}},
        {"addEnhancedScene", {"00A7", "", ""}},
        {"viewEnhancedHostToNodeScene", {"00A8", "0008", ""}},
        {"copyScene", {"00A9", "000C", ""}},
        {"moveToHue", {"00B0", "0009", ""}},
        {"moveHue", {"00B1", "0007", ""}},
        {"stepHue", {"00B2", "0008", ""}},
        {"moveToSaturation", {"00B3", "0008", ""}},
        {"moveSaturation", {"00B4", "0006", ""}},
        {"stepSaturation", {"00B5", "0008", ""}},
        {"moveToHueAndSaturation", {"00B6", "0008", ""}},
        {"moveToColour", {"00B7", "000B", ""}},
        {"moveColour", {"00B8", "0009", ""}},
        {"stepColour", {"00B9", "000B", ""}},
        {"enhancedMoveToHue", {"00BA", "000A", ""}},
        {"enhancedMoveHue", {"00BB", "0007", ""}},
        {"enhancedStepHue", {"00BC", "0008", ""}},
        {"enhancedMoveToHueAndSaturation", {"00BD", "000E", ""}},
        {"colourLoopSet", {"00BE", "000D", ""}},
        {"stopMoveStep", {"00BF", "0005", ""}},
        {"moveToColourTemperature", {"00C0", "0009", ""}},
        {"moveColourTemperature", {"00C1", "000D", ""}},
        {"stepColourTemperature", {"00C2", "000E", ""}},
        {"Initiate Touchlink", {"00D0", "0000", ""}},
        {"touchLinkFactoryResetTarget", {"00D2", "0000", ""}},
        {"identifyTriggerEffect", {"00E0", "0007", ""}},
        {"lockUnlockDoor", {"00F0", "0006", ""}},
        {"readAttributeRequest", {"0100", "", ""}},
        {"writeAttributeRequest", {"0110", "000", ""}},
        {"configureReportingRequest", {"0120", "", ""}},
        {"attributeDiscoveryRequest", {"0140", "000E", ""}},
        {"iASZoneEnrollResponse", {"0400", "0007", ""}},
        {"rawApsDataRequest", {"0530", "000D", ""}}
    };


/*    QList <QString> responseListCode
    {
        "004D",
        "8000",
        "8001",
        "8002",
        "8003",
        "8004",
        "8005",
        "8006",
        "8007",
        "8010",
        "8014",
        "8024",
        "8030",
        "8031",
        "8040",
        "8041",
        "8042",
        "8043",
        "8044",
        "8045",
        "8046",
        "802B",
        "802C",
        "8047",
        "8048",
        "804A",
        "804B",
        "804E",
        "8140",
        "8008",
        "8009",
        "8011",
        "8012",
        "8015",
        "8017",
        "8028",
        "8035",
        "8049",
        "8052",
        "8060",
        "8061",
        "8062",
        "8063",
        "8085",
        "8095",
        "80A0",
        "80A1",
        "80A2",
        "80A3",
        "80A4",
        "80A6",
        "80A7",
        "8100",
        "8101",
        "8102",
        "8110",
        "8120",
        "8122",
        "8531",
        "8701",
        "8702",
        "9999"
    };
*/
   //ZigbeeMgr serial;
    QMap <QByteArray, QString> clusters
    {
        {"0000", " (General: Basic)"},
        {"0001", " (General: Power Config)"},
        {"0002", " (General: Temperature Config)"},
        {"0003", " (General: Identify)"},
        {"0004", " (General: Groups)"},
        {"0005", " (General: Scenes)"},
        {"0006", " (General: On/Off)"},
        {"0007", " (General: On/Off Config)"},
        {"0008", " (General: Level Control)"},
        {"0009", " (General: Alarms)"},
        {"000A", " (General: Time)"},
        {"000F", " (General: Binary Input Basic)"},
        {"0020", " (General: Poll Control)"},
        {"0019", " (General: OTA)"},
        {"0101", " (General: Door Lock"},
        {"0201", " (HVAC: Thermostat)"},
        {"0202", " (HVAC: Fan Control)"},
        {"0300", " (Lighting: Color Control)"},
        {"0400", " (Measurement: Illuminance)"},
        {"0402", " (Measurement: Temperature)"},
        {"0405", " (Measurement: Humidity)"},
        {"0406", " (Measurement: Occupancy Sensing)"},
        {"0500", " (Security & Safety: IAS Zone)"},
        {"0702", " (Smart Energy: Metering)"},
        {"0B05", " (Misc: Diagnostics)"},
        {"1000", " (ZLL: Commissioning)"},
        {"FF01", " Xiaomi private"},
        {"FF02", " Xiaomi private"},
        {"1234", " Xiaomi private"}
    };
};



#endif
