#include "global.h"

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

#include <Windows.h>
#include <shlwapi.h>

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::resourceDirPath()
{
    return mRootPath + "/config";
}


void Global::setConfigParam(ConfigParam p)
{
    mConfigParam = p;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/InputPath", mConfigParam.getInputStr());
    settings.setValue("/Config/OutputPath", mConfigParam.outputPath);
}

ConfigParam Global::configParam()
{
    return mConfigParam;
}

void Global::setInput(QStringList list)
{
    mConfigParam.inputPaths = list;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/InputPath", mConfigParam.getInputStr());
}

void Global::setOutput(const QString &output)
{
    mConfigParam.outputPath = output;
    QSettings settings(configFilePath(), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.setValue("/Config/OutputPath", mConfigParam.outputPath);
}

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

void Global::checkResource()
{
    // 检查配置文件目录是否存在
    QList<QString> listPath;
    listPath << rootDirPath() << resourceDirPath();
    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/InputPath", mConfigParam.getInputStr());
        settings.setValue("/Config/OutputPath", mConfigParam.outputPath);
    }
    else
    {
        if (!settings.contains("/Config/InputPath"))
        {
            settings.setValue("/Config/InputPath", mConfigParam.getInputStr());
        }

        if (!settings.contains("/Config/OutputPath"))
        {
            settings.setValue("/Config/OutputPath", mConfigParam.outputPath);
        }
    }

    QString inputPath = settings.value("/Config/InputPath", mConfigParam.getInputStr()).toString();
    QStringList _inputPaths = inputPath.split(";");
    for(auto i : _inputPaths)
    {
//        QString fileName = i.trimmed();
//        if(!fileName.isEmpty())
//        {
//            QFileInfo info(fileName);
//            if(info.suffix().toLower().compare("obj") == 0)
//                mConfigParam.inputPaths.append(i);
//        }
        if(!i.trimmed().isEmpty())
            mConfigParam.inputPaths.append(i);
    }

    mConfigParam.outputPath = settings.value("/Config/OutputPath", mConfigParam.outputPath).toString();
}

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

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