#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
#include <QObject>
#include <QHash>
#include <QVariantMap>
#include "define.h"

class config : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString suserId READ suserId WRITE setSuserId)
    Q_PROPERTY(QString sdevUuid READ sdevUuid WRITE setSdevUuid NOTIFY sdevUuidChanged)
    Q_PROPERTY(QString shttpHost READ shttpHost WRITE setShttpHost NOTIFY shttpHostChanged)
    Q_PROPERTY(int sloginTimeout READ sloginTimeout WRITE setSloginTimeout NOTIFY sloginTimeoutChanged)
    Q_PROPERTY(int slockCloseTimeout READ slockCloseTimeout WRITE setSlockCloseTimeout NOTIFY slockCloseTimeoutChanged)
    Q_PROPERTY(QString susername READ susername)
    Q_PROPERTY(QString srfidPort READ srfidPort WRITE setSrfidPort NOTIFY srfidPortChanged)
    Q_PROPERTY(QString slockPort READ slockPort WRITE setSlockPort NOTIFY slockPortChanged)
    Q_PROPERTY(quint16 srfidPower READ srfidPower WRITE setSrfidPower NOTIFY srfidPowerChanged)
    Q_PROPERTY(quint32 srfidWorkTime READ srfidWorkTime WRITE setSrfidWorkTime NOTIFY srfidWorkTimeChanged)
    Q_PROPERTY(QStringList sserialPortList READ sserialPortList)
    Q_PROPERTY(bool smaterialStock READ smaterialStock WRITE setSmaterialStock NOTIFY smaterialStockChanged)
    Q_PROPERTY(bool sfingerStat READ sfingerStat)
    Q_PROPERTY(bool sopenDoor READ sopenDoor)

    Q_PROPERTY(quint8 imaterworkstat READ imaterworkstat WRITE setimaterworkstat)
    Q_PROPERTY(bool blocktimeout READ blocktimeout WRITE setblocktimeout)

    Q_PROPERTY(QVariantMap smaterialTypePngList READ smaterialTypePngList)
    Q_PROPERTY(QVariantMap smaterialTypeCountList READ smaterialTypeCountList)
    Q_PROPERTY(QVariantMap smaterialTypeBorrowableNumberList READ smaterialTypeBorrowableNumberList)

    Q_PROPERTY(QVariantMap smaterialNameList READ smaterialNameList)
    Q_PROPERTY(QVariantMap smaterialTypeList READ smaterialTypeList)
    Q_PROPERTY(QVariantMap smaterialPictureList READ smaterialPictureList)
    Q_PROPERTY(QVariantMap smaterialLoanUserList READ smaterialLoanUserList)
    Q_PROPERTY(QVariantMap smaterialStatusList READ smaterialStatusList)


    Q_PROPERTY(QVariantMap smaterialLoanStateList READ smaterialLoanStateList)

    Q_PROPERTY(QList<QString> stakeMaterialList READ stakeMaterialList)
    Q_PROPERTY(QList<QString> sbackMaterialList READ sbackMaterialList)

    Q_PROPERTY(QList<QString> sbindedMaterialList READ sbindedMaterialList)
    Q_PROPERTY(QList<QString> sunbindMaterialList READ sunbindMaterialList)

    Q_PROPERTY(QList<QString> sbackElseMaterialList READ sbackElseMaterialList)
    Q_PROPERTY(QList<QString> stakeElseMaterialList READ stakeElseMaterialList)

    Q_PROPERTY(QVariantMap smaterialNameListElse READ smaterialNameListElse)
    Q_PROPERTY(QVariantMap smaterialPictureListElse READ smaterialPictureListElse)

    Q_PROPERTY(int scurrentPlanId READ scurrentPlanId WRITE setScurrentPlanId)


    Q_PROPERTY(QString schangeNumber READ schangeNumber)
    Q_PROPERTY(QString scheckNumber READ scheckNumber)
    Q_PROPERTY(QString srarNumber READ srarNumber)

    Q_PROPERTY(QStringList sbottomTypeId READ sbottomTypeId)
    Q_PROPERTY(QVariantMap sbottomTypeName READ sbottomTypeName)
    Q_PROPERTY(QVariantMap sbottomTypeIcon READ sbottomTypeIcon)
    Q_PROPERTY(QVariantMap sbottomTypeAll READ sbottomTypeAll)
    Q_PROPERTY(QVariantMap sbottomTypeBorrowable READ sbottomTypeBorrowable)

public:
    config();
    void configWrite(void);
    bool configRead(void);
    QString getDeviceUUID(void);

    enum xmlType {
        XML_NULL = 0,
        XML_CONFIG,
        XML_DEVICE,
        XML_RFID,
        XML_LOCK,
        XML_SERVER,
        XML_PARAM,
    };
    Q_ENUM(xmlType)
#ifdef Q_OS_WIN
    QString configfile = "config.xml";
#else
    QString configpath = "/storage/emulated/0/Android/data/com.cabinet/";
    QString configfile = configpath + "config.xml";
#endif
    bool devInit = false;
    QString devUuid = "CABINET_016";
    bool logfileStat = false;
    bool readBee = false;
    int heartTime = 60;
    int lockCloseTimeout = 10 * 60;
    bool rfidInit = false;
    bool lockInit = false;

    int currentPlanId = 0;

#ifdef Q_OS_WIN
    QString rfidPort = "COM1";
    QString lockPort = "COM2";
#else
    QString rfidPort = "/dev/ttyUSB1";
    QString lockPort = "/dev/ttyUSB2";
#endif
    QStringList serialPortList;
    quint16 rfidPower = 1750;           //发射接收功率0.01dB
    quint16 rfidSetTime = 1000;         //天线切换稳定时间，单位：us
    quint32 rfidWorkTime = 10000;        //同步读取标签时间，单位：ms
    quint16 rfidport = 0x00;            //天线端口

    int rfidBaud = 115200;
    int lockBaud = 115200;

    QString changeNumber;   // 物资更换数量
    QString checkNumber;    // 物资检查数量
    QString rarNumber;      // 物资取还数量

    QHash<QString, QString> bottomTypeName;         // 物资类型名称
    QHash<QString, QString> bottomTypeIcon;         // 物资类型图标
    QHash<QString, QString> bottomTypeAll;          // 物资类型总数量
    QHash<QString, QString> bottomTypeBorrowable;   // 物资类型可借数量

    QString httpHost = "120.27.232.27:9190";

    QString usernameLogin_url = "/prod-api/login";                                                      //用户名登陆接口
    QString cardLogin_url = "/prod-api/iscs/card/loginByCard";                                          //卡号登陆接口
    QString logout_url = "/prod-api/logout";                                                            //登出接口
    QString getMaterial_url = "/prod-api/iscs/hardware/material-api/selectMaterialsByRfidList";         //获取物资信息
    QString userMaterial_url = "/prod-api/iscs/hardware/material-api/selectMaterialsByCabinetId";       //用户登录获取物资信息
    QString openCabinet_url = "/prod-api/iscs/hardware/material-api/insertCabinetOpenRecord";           //打开物资柜
    QString closeCabinet_url = "/prod-api/iscs/hardware/material-api/updateCabinetClose";               //关闭物资柜
    QString updateMaterial_url = "/prod-api/iscs/materials/updateIsMaterialById";                       //更新物资状态
    QString addfinger_url = "/prod-api/system/user/characteristic/insertUserFingerprintDat";            //录入指纹
    QString fingerLogin_url = "/prod-api/loginByFingerprintDat";                                        //指纹登陆


    QString instructionsGet_url = "/prod-api/iscs/instructions/getIsMaterialsInstructionsPage";          // 获取物资使用说明

    QString getMaterialCheckPlan_url = "/prod-api/iscs/hardware/material-api/getLastCheckPlanByCabinetCode";    // 获取物资检查计划
    QString getMaterialCheckForm_url = "/prod-api/iscs/hardware/material-api/getCheckMaterialsByCabinetCode";   // 获取物资检查列表

    QString postSaveForm_url = "/prod-api/iscs/hardware/material-api/insertCheckRecord";        // 提交物资检查列表
    QString postCheckSign_url = "/prod-api/iscs/hardware/material-api/insertSign";              // 提交签名检查
    QString postSubmit_url = "/prod-api/iscs/hardware/material-api/insertSubmitPlan";           // 提交检查计划

    QString getMaterialPropList_url = "/prod-api/iscs/propvalue/getIsMaterialsPropertyValuePage";   // 获取物资属性列表
    QString getMaterialTypeList_url = "/prod-api/iscs/type/getIsMaterialsTypePage";                 // 获取物资类型列表
    QString getMaterialInfoList_url = "/prod-api/iscs/materials/getIsMaterialsPage";                // 获取物资信息列表
    QString getExMaterialInfoList_url = "/prod-api/iscs/hardware/material-api/selectExMaterialTypeById";  // 获取异常物资信息列表

    QString postMaterialReplace_url = "/prod-api/iscs/hardware/material-api/insertCheckedRecord";   // 物资手动更换提交
    QString postMaterialReplaceAuto_url = "/prod-api/iscs/hardware/material-api/insertAutoChange";  // 物资自动更换提交

    QString postInsertCabinetOpenTimeout_url = "/prod-api/iscs/hardware/material-api/insertCabinetOpenTimeout"; // 新增物资柜关门异常
    QString postUpdateCabinetOpenTimeout_url = "/prod-api/iscs/hardware/material-api/updateCabinetOpenTimeout"; // 更新物资柜关门异常

    QString getIsMaterialsLoanExceptionPage_url = "/prod-api/iscs/exception/getIsMaterialsLoanExceptionPage";   // 获取当前物资柜物资错放异常

    QString getInfo_url = "/getInfo";                                           // 获取当前用户信息
    QString getSysUserCharacteristicPage_url = "/system/user/characteristic/getSysUserCharacteristicPage"; //获取当前用户特征
    QString insertUserFace_url = "/system/user/characteristic/insertUserFace";   // 新增面部信息

    QString loginByFace_url = "/loginByArcFace";   // 人脸登录

    QString ip = "0.0.0.0";
    QString mask = "255.255.255.0";
    QString dns = "0.0.0.0";

    int loginTimeout = 99;

    bool openDoor = false;


    // 物资工作状态   0.物资存取 1.物资自动更换 2.物资手动更换 3.异常处理 4.物资检查
    uint8_t materworkstat = 0;
    bool locktimeout = false;       // 超时标志
    qint64 lastLockTimeEpoch = 0; // 开锁时间戳 (关锁为0，开锁为当前时间戳)


    bool materialStock = false;

    QString userId;
    QString username;
    QString nickName;
    QString cardNo;
    QString devId;
    QString devName;

    bool fingerStat = false;

    QHash<QString, QString> materialNameList;
    QHash<QString, QString> materialTypeList;
    QHash<QString, QString> materialPictureList;
    QHash<QString, QString> materialLoanUserList;
    QHash<QString, int> materialLoanStateList;
    QHash<QString, QString> materialStatusList;


    QHash<QString, QString> materialNameListElse;
    QHash<QString, QString> materialPictureListElse;

    QList<QString> backMaterialList;
    QList<QString> takeMaterialList;

    QList<QString> bindedMaterialList;  // 绑定
    QList<QString> unbindMaterialList;  // 解绑

    QList<QString> backElseMaterialList;
    QList<QString> takeElseMaterialList;

    QList<QString> elseMaterialList; // 不属于当前柜子的物资

    QHash<QString, QString> materialTypePngList;
    QHash<QString, int> materialTypeCountList;
    QHash<QString, int> materialTypeBorrowableNumberList;

    QString suserId()
    {
        return userId;
    }
    void setSuserId(const QString& suserId)
    {
        userId = suserId;
    }

    QString sdevUuid() const {
        return devUuid;
    }

    void setSdevUuid(const QString &sdevUuid) {
        if (sdevUuid == devUuid)
            return;
        devUuid = sdevUuid;
        emit sdevUuidChanged();
    }

    QString shttpHost() const {
        return httpHost;
    }

    void setShttpHost(const QString &shttpHost) {
        if (shttpHost == httpHost)
            return;
        httpHost = shttpHost;
        emit shttpHostChanged();
    }

    int sloginTimeout() const {
        return loginTimeout;
    }

    void setSloginTimeout(const int &sloginTimeout) {
        if (sloginTimeout == loginTimeout)
            return;
        loginTimeout = sloginTimeout;
        emit sloginTimeoutChanged();
    }

    // Q_PROPERTY(int slockCloseTimeout READ slockCloseTimeout WRITE setSlockCloseTimeout NOTIFY slockCloseTimeoutChanged)
    int slockCloseTimeout() const {
        return lockCloseTimeout;
    }

    void setSlockCloseTimeout(const int &slockCloseTimeout) {
        if (slockCloseTimeout == lockCloseTimeout)
            return;
        lockCloseTimeout = slockCloseTimeout;
        emit slockCloseTimeoutChanged();
    }

    QString susername() const {
        return username;
    }

    QStringList sserialPortList() const {
        return serialPortList;
    }

    QString slockPort() const {
        return lockPort;
    }

    void setSlockPort(const QString &slockPort) {
        if (slockPort == lockPort)
            return;
        lockPort = slockPort;
        emit slockPortChanged();
    }

    QString srfidPort() const {
        return rfidPort;
    }

    void setSrfidPort(const QString &srfidPort) {
        if (srfidPort == rfidPort)
            return;
        rfidPort = srfidPort;
        emit srfidPortChanged();
    }

    quint16 srfidPower() const {
        return rfidPower;
    }

    void setSrfidPower(const int &srfidPower) {
        if (srfidPower == rfidPower)
            return;
        rfidPower = srfidPower;
        emit srfidPowerChanged();
    }

    quint32 srfidWorkTime() const {
        return rfidWorkTime;
    }

    void setSrfidWorkTime(const int &srfidWorkTime) {
        if (srfidWorkTime == rfidWorkTime)
            return;
        rfidWorkTime = srfidWorkTime;
        emit srfidWorkTimeChanged();
    }

    bool sopenDoor() const {
        return openDoor;
    }

    bool sfingerStat() const {
        return fingerStat;
    }

    quint8 imaterworkstat()
    {
        return materworkstat;
    }

    void setimaterworkstat(quint8 imaterworkstat)
    {
        materworkstat = imaterworkstat;
    }

    bool blocktimeout()
    {
        return locktimeout;
    }

    void setblocktimeout(bool bLockTimeout)
    {
        locktimeout = bLockTimeout;
    }


    bool smaterialStock() const {
        return materialStock;
    }

    void setSmaterialStock(const bool &smaterialStock) {
        if (smaterialStock == materialStock)
            return;
        materialStock = smaterialStock;
        emit smaterialStockChanged();
    }

    QVariantMap smaterialTypePngList() const {
        QVariantMap map;
        for (auto it = materialTypePngList.begin(); it != materialTypePngList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialTypeCountList() const {
        QVariantMap map;
        for (auto it = materialTypeCountList.begin(); it != materialTypeCountList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialTypeBorrowableNumberList() const {
        QVariantMap map;
        for (auto it = materialTypeBorrowableNumberList.begin(); it != materialTypeBorrowableNumberList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }


    QVariantMap smaterialNameList() const {
        QVariantMap map;
        for (auto it = materialNameList.begin(); it != materialNameList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialTypeList() const {
        QVariantMap map;
        for (auto it = materialTypeList.begin(); it != materialTypeList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialPictureList() const {
        QVariantMap map;
        for (auto it = materialPictureList.begin(); it != materialPictureList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialLoanUserList() const {
        QVariantMap map;
        for (auto it = materialLoanUserList.begin(); it != materialLoanUserList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialStatusList() const {
        QVariantMap map;
        for (auto it = materialStatusList.begin(); it != materialStatusList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }


    QVariantMap smaterialLoanStateList() const {
        QVariantMap map;
        for (auto it = materialLoanStateList.begin(); it != materialLoanStateList.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QList<QString> sbackMaterialList() const {
        return backMaterialList;
    }

    QList<QString> stakeMaterialList() const {
        return takeMaterialList;
    }

    QList<QString> sbindedMaterialList() const {
        return bindedMaterialList;
    }

    QList<QString> sunbindMaterialList() const {
        return unbindMaterialList;
    }

    QList<QString> sbackElseMaterialList() const {
        return backElseMaterialList;
    }

    QList<QString> stakeElseMaterialList() const {
        return takeElseMaterialList;
    }


    QVariantMap smaterialNameListElse() const {
        QVariantMap map;
        for (auto it = materialNameListElse.begin(); it != materialNameListElse.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    QVariantMap smaterialPictureListElse() const {
        QVariantMap map;
        for (auto it = materialPictureListElse.begin(); it != materialPictureListElse.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }

    int scurrentPlanId()
    {
        return currentPlanId;
    }

    Q_INVOKABLE void setScurrentPlanId(int scurrentPlanId)
    {
        currentPlanId = scurrentPlanId;
    }

    QString schangeNumber()
    {
        return changeNumber;
    }
    QString scheckNumber()
    {
        return checkNumber;
    }
    QString srarNumber()
    {
        return rarNumber;
    }


    QStringList sbottomTypeId() const {
        return bottomTypeName.keys();
    }
    QVariantMap sbottomTypeName() const {
        QVariantMap map;
        for (auto it = bottomTypeName.begin(); it != bottomTypeName.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }
    QVariantMap sbottomTypeIcon() const {
        QVariantMap map;
        for (auto it = bottomTypeIcon.begin(); it != bottomTypeIcon.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }
    QVariantMap sbottomTypeAll() const {
        QVariantMap map;
        for (auto it = bottomTypeAll.begin(); it != bottomTypeAll.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }
    QVariantMap sbottomTypeBorrowable() const {
        QVariantMap map;
        for (auto it = bottomTypeBorrowable.begin(); it != bottomTypeBorrowable.end(); ++it) {
            map.insert(it.key(), it.value());
        }
        return map;
    }


private:
    void updateSerialPortList();
    void getDeviceValue(QString key, QString value);
    void getRfidValue(QString key, QString value);
    void getLockValue(QString key, QString value);
    void getParamValue(QString key, QString value);
    void getServerValue(QString key, QString value);

signals:
    void sdevUuidChanged();
    void shttpHostChanged();
    void shttpPortChanged();
    void sloginTimeoutChanged();
    void slockCloseTimeoutChanged();
    void srfidPowerChanged();
    void srfidWorkTimeChanged();
    void smaterialStockChanged();
    void srfidPortChanged();
    void slockPortChanged();
};

#endif // CONFIG_H
