#include "fmtools.h"
#include "fmdefine.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QSqlDatabase>
#include <QDomComment>
#include <QDomElement>
#include <QSqlQuery>
#include <QFile>
#include <QDir>

FmTools &FmTools::GetInstance()
{
    static FmTools ft;
    return ft;
}

FmTools::FmTools()
{
    QString iniPath = QString("%1/%2").arg(QCoreApplication::applicationDirPath(), INIFILE_NAME);
    m_config = new QSettings(iniPath, QSettings::IniFormat);

#ifdef BALIBEITIAN

    #define MY_CFG "FMPOS.ini"

    QFile file("C:/SPCINT/DATA/DBPOS/SHOPINFO.INI");

    if(file.exists())
        m_siIniPath = "C:/SPCINT/DATA/DBPOS/SHOPINFO.INI";
    else
    {
        m_siIniPath = qApp->applicationDirPath() + "/" + MY_CFG;
        m_partnerId = "1321";
        m_keyStoreId = "SHOPINFO/STORECD";
        m_keyPosId = "SHOPINFO/POSNO";;

        QString tmppath = "C:/SPC/POS/INI/PosConfig.ini";

        QDomDocument doc;
        QFile file(tmppath);

        if(!file.open(QFile::ReadOnly | QFile::Text))
        {
           return ;
        }
        QString strs;
        int line, row;
        if(!doc.setContent(&file, &strs, &line, &row))
        {
            file.close();
            return ;
        }
        file.close();
        QDomElement root = doc.documentElement();
        if(root.tagName().compare("MESSAGE", Qt::CaseInsensitive) != 0)
        {
            return ;
        }

        QDomNode n = root.firstChild();

        QString tmpposno, tmpstoreid;

        while(!n.isNull())
        {
            QDomElement e = n.toElement();

            if(e.nodeName().compare("pos", Qt::CaseInsensitive) == 0)
            {
                QDomNode ser = e.firstChild();

                while(!ser.isNull())
                {

                    QDomElement na = ser.toElement();

                    if(na.nodeName().compare("StoreNo", Qt::CaseInsensitive) == 0)
                    {
                        tmpstoreid = na.firstChild().nodeValue();
                    }
                    if(na.nodeName().compare("PosNo", Qt::CaseInsensitive) == 0)
                    {
                        tmpposno = na.firstChild().nodeValue();
                    }
                    ser = ser.nextSibling();
                }

            }
            n = n.nextSibling();
        }
       QSettings set(m_siIniPath, QSettings::IniFormat);

       set.setValue(m_keyStoreId, tmpstoreid);
       set.setValue(m_keyPosId, tmpposno);
    }

    m_partnerId = "1321";
    m_keyStoreId = "SHOPINFO/STORECD";
    m_keyPosId = "SHOPINFO/POSNO";
#endif
#ifdef SHIQIJIA
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    QString dsn = QString("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=C:/ZGIT/MST.ACCDB");

    db.setDatabaseName(dsn);

    m_partnerId = "1396";
    m_keyStoreId = "0000";
    m_keyPosId = "0000";

    if(db.open())
    {
        QSqlQuery query(db);
        if(query.exec("select SHOP_CD, POS_NO from MST_PSET"))
        {
            if(query.next())
            {
                m_keyStoreId = query.value(0).toString();
                m_keyPosId = query.value(1).toString();
            }
        }
        db.close();
    }
#endif  
#ifdef AI_SEN
    m_partnerId = "1484";

    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Hisensoft Co.Ltd\\FoodPos", QSettings::NativeFormat);

    QString appDir = reg.value("AppDir").toString();

    m_siIniPath = appDir + "/PosCfg.ini";
    m_keyStoreId = "POS/ORGCODE";
    m_keyPosId = "POS/POSNO";
#endif
#ifdef SHAN_LIN
    m_partnerId = "1234";
    m_siIniPath = "D:/hspos/PosCfg.ini";
    m_keyStoreId = "POS/ORGCODE";
    m_keyPosId = "POS/POSNO";
#endif
#ifdef XIAN_FENG
    m_partnerId = "1418";
    m_siIniPath = QString("%1/storeinfo.ini").arg(QCoreApplication::applicationDirPath());
    m_keyStoreId = "info/store_id";
    m_keyPosId = "info/pos_no";
#endif
#ifdef TODAY
    m_partnerId = "1377";
    m_siIniPath = QString("%1/todaypos.ini").arg(QCoreApplication::applicationDirPath());
    m_keyStoreId = "store_id";
    m_keyPosId = "pos_no";
#endif

#ifdef  RUBY
    m_partnerId = "1774";
    m_siIniPath = QCoreApplication::applicationDirPath() + "/user.ini";
    m_keyStoreId = "storeInfo/storeId";
    m_keyPosId = "storeInfo/posId";
#endif
#ifdef  YOUQI
    m_partnerId = "1758";
    m_siIniPath = QCoreApplication::applicationDirPath() + "/user.ini";
    m_keyStoreId = "storeInfo/storeId";
    m_keyPosId = "storeInfo/posId";
#endif
}

FmTools::~FmTools()
{
    if(m_config != NULL)
    {
        delete m_config;
        m_config = NULL;
    }
}

QStringList FmTools::GetKeepExes()
{
    return m_config->value(INIPATH_EXELIST).toStringList();
}

QString FmTools::GetVersion()
{
    return m_config->value(INIPATH_VERSION).toString();
}

QString FmTools::GetPartnerId()
{
    return m_config->value(INIPATH_PARTNERID).toString();
}

int FmTools::GetDownloadTimeout()
{
    return m_config->value(INIPATH_TIMEOUT0, 300).toInt()*1000;
}

int FmTools::GetRequestTimeout()
{
    return m_config->value(INIPATH_TIMEOUT1, 20).toInt()*1000;
}

QString FmTools::GetUpdateUrl()
{
    return m_config->value(INIPATH_UPDATE_URL).toString();
}

QString FmTools::GetReportUrl()
{
    return m_config->value(INIPATH_REPORT_URL).toString();
}

QByteArray FmTools::GetUpdateData()
{
    QSettings setting(m_siIniPath, QSettings::IniFormat);
    QJsonObject rObj;
    rObj.insert(JSON_PARTNERID, m_partnerId);

#ifdef SHIQIJIA
    rObj.insert(JSON_STOREID, m_keyStoreId);
    rObj.insert(JSON_POSNO, m_keyPosId);
#else
    rObj.insert(JSON_STOREID, setting.value(m_keyStoreId).toString());
    rObj.insert(JSON_POSNO, setting.value(m_keyPosId).toString());
#endif
    rObj.insert(JSON_VERSION, GetVersion());
    return QJsonDocument(rObj).toJson(QJsonDocument::Compact);
}

QByteArray FmTools::GetReportData(QString planid, QString msg, QString updateState)
{
    if(planid.isEmpty())
    {
        QFile file(QString("%1/download/planid.txt").arg(QCoreApplication::applicationDirPath()));
        if(file.open(QIODevice::ReadOnly))
        {
            planid = QString(file.readAll());
            file.close();
        }
        QFile file1(QString("%1/download/updateresult.txt").arg(QCoreApplication::applicationDirPath()));
        if(file1.open(QIODevice::ReadOnly))
        {
            QString readStr = file1.readAll();
            updateState = readStr.mid(0, 1);
            msg = readStr.mid(1);
            file1.close();
        }
    }

    QSettings setting(m_siIniPath, QSettings::IniFormat);
    QJsonObject rObj;
    rObj.insert(JSON_PARTNERID, m_partnerId);
    rObj.insert(JSON_STOREID, setting.value(m_keyStoreId).toString());
    rObj.insert(JSON_POSNO, setting.value(m_keyPosId).toString());
    rObj.insert(JSON_PLANID, planid);
    rObj.insert(JSON_UPDATESTATE, updateState);
    rObj.insert(JSON_MSG, msg);
    return QJsonDocument(rObj).toJson(QJsonDocument::Compact);
}
