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

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

QString AppName = "军军模型转换神器";
int Major = 0;
int Minor = 0;
int Patch = 1;

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 + "/005ReadVis";
}

void Global::setParam(Param p)
{
    mParam = p;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/InputVis", mParam.inputVisPath);
    settings.setValue("/Config/ConvertFbx", mParam.convertFbxPath);
}

Param Global::param()
{
    return mParam;
}

void Global::setInputVisPath(const QString &path)
{
    mParam.inputVisPath = path;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/InputVis", mParam.inputVisPath);
}

void Global::setConvertFbxPath(const QString &path)
{
    mParam.convertFbxPath = path;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/ConvertFbx", mParam.convertFbxPath);
}

Global::Global()
{
    mRootPath = "";
}

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("/Config/InputVis", mParam.inputVisPath);
        settings.setValue("/Config/ConvertFbx", mParam.convertFbxPath);
    }
    else
    {
        if(!settings.contains("/Config/InputVis"))
        {
            settings.setValue("/Config/InputVis", mParam.inputVisPath);
        }

        if(!settings.contains("/Config/ConvertFbx"))
        {
            settings.setValue("/Config/ConvertFbx", mParam.convertFbxPath);
        }

        mParam.inputVisPath = settings.value("/Config/InputVis", mParam.inputVisPath).toString();
        mParam.convertFbxPath = settings.value("/Config/ConvertFbx", mParam.convertFbxPath).toString();
    }
}

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


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

QString appName()
{
    return AppName;
}

QString appVersion()
{
    return QString("v%1.%2.%3").arg(Major).arg(Minor).arg(Patch);
}
