/****************************************************************
 *
 * 作   者：王建军
 * 时   间：2020/08/24
 * 开发语言：C++
 * 开发工具：QtCreator(Qt)
 * 文   件：global.h
 * 版   权：武汉创景可视技术有限公司
 *
 ***************************************************************/
#include "global.h"

#include <QDir>
#include <QSettings>
#include <QFileInfo>
#include <QDebug>

Global *Global::Instance()
{
    if(mInstance == nullptr)
    {
        QMutexLocker locker(&mMutex);
        if(mInstance == nullptr)
            mInstance = new Global;
    }
    return mInstance;
}

void Global::setRootDirPath(const QString &path)
{
    mRootPath = path;
    checkResource();
}

QString Global::rootDirPath()
{
    return mRootPath;
}

QString Global::configDirPath()
{
    return mRootPath + "/config";
}

void Global::setLayerParam(LayerParam p)
{
    mLayerParam = p;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Layer/Name", mLayerParam.layerName);
    settings.setValue("/Layer/Url", mLayerParam.layerUrl);
    settings.setValue("/Layer/Type", mLayerParam.layerType);
}

LayerParam Global::layerParam()
{
    return mLayerParam;
}

void Global::setServerParam(ServerParam p)
{
    mServerParam = p;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Server/Host", mServerParam.host);
    settings.setValue("/Server/Port", mServerParam.port);
}

ServerParam Global::serverParam()
{
    return mServerParam;
}

Global::Global()
{
    mRootPath = "";
    mServerParam = {"127.0.0.1", 8003};
}

void Global::checkResource()
{
    // 检查配置文件目录是否存在
    QList<QString> listPath;
    listPath << rootDirPath() << configDirPath();
    for(QString path : listPath)
    {
        QDir dir(path);
        if(!dir.exists())
            dir.mkpath(path);
    }

    // 定义ini文件处理类
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    QFileInfo iniFileInfo(configFilePath());
    if(!iniFileInfo.exists())
    {
        settings.setValue("/Layer/Name", mLayerParam.layerName);
        settings.setValue("/Layer/Url", mLayerParam.layerUrl);
        settings.setValue("/Layer/Type", mLayerParam.layerType);
        settings.setValue("/Server/Host", mServerParam.host);
        settings.setValue("/Server/Port", mServerParam.port);
    }
    else
    {
        if(!settings.contains("/Layer/Name"))
        {
            settings.setValue("/Layer/Name", mLayerParam.layerName);
        }

        if(!settings.contains("/Layer/Url"))
        {
            settings.setValue("/Layer/Url", mLayerParam.layerUrl);
        }

        if(!settings.contains("/Layer/Type"))
        {
            settings.setValue("/Layer/Type", mLayerParam.layerType);
        }

        if(!settings.contains("/Server/Host"))
        {
            settings.setValue("/Server/Host", mServerParam.host);
        }

        if(!settings.contains("/Server/Port"))
        {
            settings.setValue("/Server/Port", mServerParam.port);
        }

        mLayerParam.layerName = settings.value("/Layer/Name", mLayerParam.layerName).toString();
        mLayerParam.layerUrl = settings.value("/Layer/Url", mLayerParam.layerUrl).toString();
        mLayerParam.layerType = settings.value("/Layer/Type", mLayerParam.layerType).toString();

        mServerParam.host = settings.value("/Server/Host", mServerParam.host).toString();
        mServerParam.port = settings.value("/Server/Port", mServerParam.port).toInt();
    }
}

QString Global::configFilePath()
{
    return configDirPath() + "/config.ini";
}


Global* Global::mInstance = nullptr;
QMutex Global::mMutex;
Global::GC Global::gc;
