#ifndef CONFIG_H_
#define CONFIG_H_
#include <QString>
#include <QObject>
#include <QHash>
#include <QVariantMap>
#include <QtQml/qqml.h>

class config : public QObject
{
    Q_OBJECT
    QML_SINGLETON
    QML_NAMED_ELEMENT(Config)

    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(QString susername READ susername)
    Q_PROPERTY(QStringList sserialPortList READ sserialPortList)

    Q_PROPERTY(int scurrentPlanId READ scurrentPlanId WRITE setScurrentPlanId)
private:
    config();

    Q_DISABLE_COPY(config);
public:
    static config* pInstance;
    static config* instance();
    static config* create(QQmlEngine*, QJSEngine*);

    void configWrite(void);
    bool configRead(void);
    QString getDeviceUUID(void);

    enum xmlType {
        XML_NULL = 0,
        XML_CONFIG,
        XML_DEVICE,
        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;

    const char* lotoQmlModuleName = "Loto";
    int lotoQmlModuleMajorVersion = 1;
    int lotoQmlModuleMinorVersion = 0;

#ifdef Q_OS_WIN
    QString rfidPort = "COM1";
    QString lockPort = "COM2";
#else
    QString rfidPort = "/dev/ttyUSB1";
    QString lockPort = "/dev/ttyUSB2";
#endif
    QStringList serialPortList;

    QString m_systemMACAddr;

//    QString httpHost = "120.27.232.27:9292";
    QString httpHost = "192.168.0.10:48080";
    QString tenant_id = "149";

    QString userInfoUrl = "/admin-api/system/user/profile/get";                                             // 用户中心
    QString updateUserInfoUrl = "/admin-api/system/user/profile/update";                                    // 更新用户信息
    QString updatePasswordUrl = "/admin-api/system/user/profile/update-password";                           // 更新用户密码
    QString userInfoByIdUrl = "/admin-api/system/user/get";                                                 // 根据用户ID获取用户头像等信息

    QString usernameLogin_url = "/admin-api/system/auth/login";                                             //用户名登陆接口
    QString cardLogin_url = "/admin-api/iscs/job-card/loginByCard";                                          //卡号登陆接口
    QString logout_url = "/logout";                                                                         //登出接口

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

    QString loginByFace_url = "/admin-api/system/auth/loginByArcFace";   // 人脸登录

    QString jobTicketsUrl = "/admin-api/iscs/workflow-work/getMyWorkPage";                                    // 获取我的作业页面
    QString workNodeDetail = "/admin-api/iscs/workflow-work/getMyWorkNodeDetail";                               // 表单详情页面
    QString workNodeDetailForm = "/admin-api/bpm/form/get";                                                   // 表单详情页中的组件信息
    QString updateNodeApprovalUrl = "/admin-api/iscs/workflow-work/updateNodeApproval";                        // 表单提交

    QString keyMACByNFC = "/admin-api/iscs/key/selectKeyByNfc";                                                 // 根据NFC信息获取钥匙MAC地址

    QString isolationPointById = "/admin-api/iscs/isolation-point/selectIsolationPointById";                   // 根据隔离点ID获取隔离点具体信息

    QString uploadJobTicketUrl = "/admin-api/isc/work-handle/insertWorkTicket";
    QString updateWorkTicketUrl = "/admin-api/isc/work-handle/updateWorkTicket";
    QString uploadPositionInfoUrl = "/admin-api/isc/work-handle/updatePointLock";

    QString updateColockUrl = "/admin-api/isc/work-handle/updateUserLock";
    QString updateUncolockUrl = "/admin-api/isc/work-handle/updateUserUnlock";
    QString updatePointUnlock = "/admin-api/isc/work-handle/updatePointUnlock";
    QString updateBackLock = "/admin-api/isc/work-handle/updateBackLock";
    QString updateKeyBack = "/admin-api/isc/work-handle/updateKeyBack";

    QString workTicketByNodeId = "/admin-api/isc/work-handle/getWorkTicketByNodeId";

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

    int loginTimeout = 99;

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

    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();
    }

    QString susername() const {
        return username;
    }

    QStringList sserialPortList() const {
        return serialPortList;
    }

    int scurrentPlanId()
    {
        return currentPlanId;
    }

    Q_INVOKABLE void setScurrentPlanId(int scurrentPlanId)
    {
        currentPlanId = scurrentPlanId;
    }
private:
    void getDeviceValue(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();
};

inline config* Config() {
    return config::instance();
}

#endif // CONFIG_H
