#include "configManger.h"
#include <QSqlDatabase>
#include <QApplication>
#include <QSqlError>
#include "preDefine.h"

void ConfigManger::GetStoreConfigPassword(QString &password)
{
    password=m_config->value(INI_PASSWORD).toString();
}

ConfigManger &ConfigManger::GetInstance()
{
    static ConfigManger cm;
    return cm;
}

ConfigManger::ConfigManger()
{
    QString appDir = QApplication::applicationDirPath();
    QString config = QString("%1/%2").arg(appDir).arg(CONFIG_NAME);
    QString userConfig = QString("%1/%2").arg(appDir).arg(USERCONFIG_NAME);
    QString proxyServiceConfig = QString("C:\\Freemud\\daemonService\\fmproxy_service.config");
    m_config = new QSettings(config, QSettings::IniFormat);
    m_userConfig = new QSettings(userConfig, QSettings::IniFormat);

    /*  // One Pos 守护程序: 守护插件的路径的修改;
    [FMKeeper]
    Heartbeat={"fm_cmd":4}
    HeartbeatPeer=24409
    ServiceName=PosPluginDaemon
    HeartbeatTryCnt=5
    CheckInterval=180
    PersistLaunch=0
    KillElapse=0
    DefaultExe=C:\\freemud\\fmPlugin\\fmTakeout.exe    //<<---//
    ServiceStartMode=2
    ServiceStartDelayInterval=330
    DaemonMultiProcessMode=1
    MultiProcessPortExeMap=24409@C:\\freemud\\fmPlugin\\fmTakeout.exe;8080@C:\\Micros\\Simphony\\WebServer\\ServiceHost.exe    //<<---//
    ExtraDaemonServiceNameList=SbuxCapsServices;
    */

    //判断文件是否存在,不存在则置空，存在则配置;
    QFileInfo proxyServiceConfigFileInfo(proxyServiceConfig);
    if ( proxyServiceConfigFileInfo.isFile() )
    {
        //QFile::rename(newConfigFileFullName ,newBakConfigFileFullName);
        //QFile::copy(configFileFullName, newConfigFileFullName);
        //QLOG_INFO() << QString(" Copy config.ini File:: %1 To %2 Success !!!").arg(configFileFullName).arg(newConfigFileFullName);
        m_proxyServiceConfig = new QSettings(proxyServiceConfig, QSettings::IniFormat);
        QString tempProcessMapCfg = m_proxyServiceConfig->value(INI_DAEMON_PROCESS_MAP_KEY).toString();
        QString tempDefaultExeCfg = m_proxyServiceConfig->value(INI_DAEMON_DEFAULT_EXE_KEY).toString();
        // 输出检查;
        QLOG_INFO() << QString(" One Pos(Backup Pos) Daemon DefaultExe::%1  ;  Daemon Service List:: %2 ").arg(tempDefaultExeCfg).arg(tempProcessMapCfg);

        if ( tempProcessMapCfg.indexOf(INI_DAEMON_PROCESS_MAP_VALUE,Qt::CaseInsensitive) >= 0
             && tempDefaultExeCfg.indexOf(INI_DAEMON_DEFAULT_EXE_VALUE,Qt::CaseInsensitive) >= 0 ) {
            QLOG_INFO() << QString(" One Pos(Backup Pos) Value Is New, No Need Modify !!!");
        } else {
            // 重置新路径配置;
            if ( m_proxyServiceConfig->isWritable() ) {
                QString tempNewProcessMapCfg = QString("24409@C:\\freemud\\fmPluginNew\\fmTakeout.exe;8080@C:\\Micros\\Simphony\\WebServer\\ServiceHost.exe");
                m_proxyServiceConfig->setValue(INI_DAEMON_PROCESS_MAP_KEY, tempNewProcessMapCfg.trimmed());
                QString tempNewDefaultExeCfg = "C:\\freemud\\fmPluginNew\\fmTakeout.exe";
                m_proxyServiceConfig->setValue( INI_DAEMON_DEFAULT_EXE_KEY, tempNewDefaultExeCfg);
                // 修改后再次输出检查;
                tempProcessMapCfg = m_proxyServiceConfig->value(INI_DAEMON_PROCESS_MAP_KEY).toString();
                tempDefaultExeCfg = m_proxyServiceConfig->value(INI_DAEMON_DEFAULT_EXE_KEY).toString();
                QLOG_INFO() << QString(" One Pos(Backup Pos) After Modify --> Daemon DefaultExe::%1 ; Daemon Service List:: %2").arg(tempDefaultExeCfg).arg(tempProcessMapCfg);
            } else {
                QLOG_ERROR() << QString(" One Pos(Backup Pos) Mode , fmproxy_service.config No Writable !!!");
            }
        }

    } else {
        QLOG_INFO() << QString(" Not One Pos(Backup Pos) Mode, No Need Do Anthing");
        m_proxyServiceConfig = new QSettings();
    }

}


ConfigManger::SqlConnectInfo ConfigManger::GetSqlConnectInfo()
{
    SqlConnectInfo info;
    info.host = m_userConfig->value(INI_DB_HOST).toString();
    info.username = m_userConfig->value(INI_DB_USERNAME).toString();
    info.password = m_userConfig->value(INI_DB_PASSWORD).toString();
    info.database = m_userConfig->value(INI_DB_DATABASE).toString();
    info.port = m_userConfig->value(INI_DB_PORT).toInt();
    return info;
}

void ConfigManger::SetSqlConnectInfo(const ConfigManger::SqlConnectInfo &info)
{
    m_userConfig->setValue(INI_DB_HOST, info.host);
    m_userConfig->setValue(INI_DB_USERNAME, info.username);
    m_userConfig->setValue(INI_DB_PASSWORD, info.password);
    m_userConfig->setValue(INI_DB_DATABASE, info.database);
}

QString ConfigManger::GetLoginServerUrl()
{
    return m_config->value(INI_LOGINSERVER).toString();
}

QString ConfigManger::GetOrderServerUrl()
{
    return m_config->value(INI_ORDERSERVER).toString();
}

QString ConfigManger::GetStoreServerUrl()
{
    return m_config->value(INI_STORESERVER).toString();
}

QString ConfigManger::GetInterfaceName(const QString &name)
{
    return m_config->value(QString("Interface/")+name).toString();
}

QString ConfigManger::GetPrinterName()
{
    return m_userConfig->value(INI_PRINTERNAME).toString();
}

void ConfigManger::SetPrinterName(const QString &printer)
{
    m_userConfig->setValue(INI_PRINTERNAME, printer);
}

QPoint ConfigManger::GetFloatInitPostion()
{
    return m_userConfig->value(INI_FLOATPOSTION, QPoint(20, 20)).toPoint();
}

void ConfigManger::setFloatInitPostion(const QPoint &postion)
{
    m_userConfig->setValue(INI_FLOATPOSTION, postion);
}

int ConfigManger::GetBlinkInterval()
{
    return m_userConfig->value(INI_BLINKINTERVAL).toFloat()*1000*2;
}

int ConfigManger::GetSoundInterval()
{
    return m_userConfig->value(INI_SOUNDINTERVAL).toFloat()*1000;
}

bool ConfigManger::GetIsExistReEntryOrder()
{
    return m_config->value(INI_REENTRY, 0).toBool();
}

int ConfigManger::GetTcpServerPort()
{
    return m_config->value(INI_TCPPORT, 93953).toInt();
}

QString ConfigManger::GetDbServerIp()
{
    return m_config->value(INI_DBSERVERIP).toString();
}

int ConfigManger::GetDbServerPort()
{
    return m_config->value(INI_DBSERVERPORT).toInt();
}

int ConfigManger::GetAutoconfirm()
{
    return m_config->value(INI_AUTOCONFIRM).toInt();
}

int ConfigManger::GetHttpServerPort()
{
    return m_config->value(INI_HTTPSERVERPORT, 80).toInt();
}

bool ConfigManger::GetOpenTcpChannel()
{
    return m_config->value(INI_OPEN_TCP_CHANNEL, false).toBool();
}

QString ConfigManger::GetPartnerId()
{
    return m_config->value(INI_PARTNERID).toString();
}

QString ConfigManger::GetSubStoreId()
{
    return m_config->value(INI_SUBSTOREID).toString();
}

bool ConfigManger::GetPoskeyExistVerify()
{
    return m_config->value(INI_POSKEYEXISTVERIFY, false).toBool();
}

int ConfigManger::GetUpdDishesInterval()
{
    return m_config->value(INI_INTERVAL).toInt();
}

int ConfigManger::GetPushServerPort()
{
    return m_config->value(INI_PUSHSERVERPORT,24409).toInt();
}

QString ConfigManger::GetIpAddress()
{
    return m_config->value("IpAddress/ip").toString();
}

QPoint ConfigManger::GetOMSBtnPostion()
{
    return m_userConfig->value(INI_OMSBTNPOSTION).toPoint();
}

int ConfigManger::GetOMSBtnWidthOffset()
{
    return m_userConfig->value(INI_OMSBTNWIDTHOFFSET).toInt();
}

int ConfigManger::GetOMSBtnHeightOffset()
{
    return m_userConfig->value(INI_OMSBTNHEIGHTOFFSET).toInt();
}


int ConfigManger::GetLoginSslConfig()
{
    return m_config->value("SSLConfig/login").toInt();
}

int ConfigManger::GetOrderSslConfig()
{
    return m_config->value("SSLConfig/order").toInt();
}

int ConfigManger::GetSubStoreSkipVerify()
{
    return m_config->value(INI_SUBSTORESKIPVERIFY).toInt();
}

int ConfigManger::GetHurryPrintSum()
{
    return m_config->value(INI_HURRYPRINTSUM).toInt();
}

int ConfigManger::GetNoHurryRecordPos()
{
    return m_config->value(INI_NOHURRYRECORDPOS).toInt();
}


int ConfigManger::GetPosAgentSvrPort()
{
    return m_config->value(INI_POSAGENTSVR_PORT).toUInt();
}

QString ConfigManger::GetPosAgentDomainName()
{
    return m_config->value(INI_POSAGENTSVR_DOMAIN_NAME).toString();
}

