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

#ifndef GLOBAL_H
#define GLOBAL_H

#include <QObject>
#include <QList>
#include <QMutex>


struct LayerParam
{
    enum LayerType {Tile, Raster, Encs, Enc, Shp};

    QString layerName;
    QString layerUrl;
    QString layerType;

    QStringList layerNameList()
    {
        layerName.trimmed();
        return layerName.split(",");
    }
    QStringList layerUrlList()
    {
        layerUrl.trimmed();
        return layerUrl.split(",");
    }
    QList<LayerType> layerTypeList()
    {
        layerType.trimmed();
        QStringList list = layerType.split(",");
        QList<LayerType> _list;
        for(auto i : list)
        {
            LayerType _type;
            if(i == "Tile")
            {
                _type = Tile;
            }
            else if(i == "Raster")
            {
                _type = Raster;
            }
            else if(i == "Encs")
            {
                _type = Encs;
            }
            else if(i == "Enc")
            {
                _type = Enc;
            }
            else
            {
                _type = Shp;
            }
            _list << _type;
        }
        return _list;
    }
};

struct ServerParam
{
    QString host;
    int port;
};

class Global
{
public:
    static Global* Instance();

    void setRootDirPath(const QString& path);
    QString rootDirPath();

    QString configDirPath();

    void setLayerParam(LayerParam p);
    LayerParam layerParam();

    void setServerParam(ServerParam p);
    ServerParam serverParam();

protected:
    Global();

private:
    /**
    *@brief 检查资源目录或者文件是否存在
    **/
    void checkResource();

    QString configFilePath();

private:
    static Global *mInstance;
    static QMutex mMutex;

    QString mRootPath;

    LayerParam mLayerParam;
    ServerParam mServerParam;


    // GC类
    // 程序退出后，该类的析构函数释放单例内存
    class GC
    {
    public :
        ~GC()
        {
            if(mInstance != nullptr )
            {
                delete mInstance;
                mInstance = nullptr ;
            }
        }
    };

    static GC gc;
};




#endif // GLOBAL_H

