#include "settings.h"
#include <QCoreApplication>
#include <QFile>

Settings& Settings::instance()
{
    //采用静态局部变量的方式，静态局部变量的初始化是在第一次访问时，以后的调用不会多次初始化，并且生命周期和程序一致
    static Settings instance;
    return instance;
}

Settings::Settings()
{
    //和应用程序同一路径
    QString cofigDir = QCoreApplication::applicationDirPath();
    m_settingsFile = cofigDir + "/app_config.ini";
    QFile file(m_settingsFile);
    if(file.open(QIODevice::ReadWrite))
    {
        m_isVaildSettingsFile = true;
        m_settings = new QSettings(m_settingsFile, QSettings::IniFormat);
    }
    else
        m_isVaildSettingsFile = false;
}

Settings::~Settings()
{
    delete m_settings;
}

QVariant Settings::getDefaultValue(const QString& group, const QString& name)
{
    if(group.contains("Database-") && name == "host")
        return "127.0.0.1";
    else if(group.contains("Database-") && name == "port")
        return 5432;
    else if(group.contains("Database-") && name == "dbType")
        return "QPSQL";
    else if(group.contains("Database-") && name == "database")
        return "postgres";
    else if(group.contains("Database-") && name == "username")
        return "postgres";
    else if(group.contains("Database-") && name == "password")
        return "123456";
    else if(group.contains("Database-") && name == "comment")
        return "";
    else if(group == "Log" && name == "logFile")
        return QCoreApplication::applicationDirPath() + "/log/app.log";
    else if(group == "Log" && name == "level")
        return "DEBUG";
    else if(group == "Log" && name == "maxSize")
        return 10485760; //1024 * 1024 *10 10MB
    else if(group == "Log" && name == "backups")
        return 5;
    else if(group == "Log" && name == "consoleOutput")
        return "false";
    else if(group == "Log" && name == "fileOutput")
        return "true";
    else
    return QVariant();
}

void Settings::setValue(const QString& group, const QString& name, const QVariant& value)
{
    if(!m_isVaildSettingsFile)
        return;

    m_settings->beginGroup(group);
    m_settings->setValue(name, value);
    m_settings->endGroup();
}

QVariant Settings::value(const QString& group, const QString& name)
{
    QVariant defaultVaule = getDefaultValue(group, name);
    if(!m_isVaildSettingsFile)
        return defaultVaule;

    QVariant value = m_settings->value(group + "/" + name, defaultVaule);
    return value;
}

void Settings::clearValue(const QString& group, const QString& name)
{
    m_settings->beginGroup(group);
    m_settings->remove(name);
    m_settings->endGroup();
}

QStringList Settings::getConnectionList()
{
    QStringList result;
    QStringList litsGroup = m_settings->childGroups();
    for(QString gropu : litsGroup)
    {
        if(gropu.contains("Database-"))
        {
            QStringList tempList = gropu.split("-");
            if(tempList.count() == 2)
                result.append(tempList.at(1));
        }
    }
    return result;
}

DatabaseConfig Settings::loadDatabaseConfig(const QString& configID)
{
    DatabaseConfig config;
    QString group = "Database-" + configID;
    //m_settings->beginGroup(group);
    config.strConnectionName = value(group, "connName").toString();
    config.strHost = value(group, "host").toString();
    config.nPort = value(group, "port").toInt();
    config.strDBType = value(group, "dbType").toString();
    config.strDBName = value(group, "database").toString();
    config.strUserName = value(group, "username").toString();
    config.strPassword = value(group, "password").toString();
    config.strComment = value(group, "comment").toString();
    //m_settings->endGroup();

    return config;
}

void Settings::saveDatabaseConfig(const DatabaseConfig& config)
{
    QString group = "Database-" + config.strID;
    m_settings->beginGroup(group);
    m_settings->setValue("connName", config.strConnectionName);
    m_settings->setValue("host", config.strHost);
    m_settings->setValue("port", config.nPort);
    m_settings->setValue("dbType", config.strDBType);
    m_settings->setValue("database", config.strDBName);
    m_settings->setValue("username", config.strUserName);
    if(config.bSavePS)
        m_settings->setValue("password", config.strPassword);
    else
        m_settings->remove("password");
    m_settings->setValue("comment", config.strComment);
    m_settings->endGroup();

    m_settings->sync();
}

void Settings::removeDatabaseConfig(const QString& configID)
{
    QString group = "Database-" + configID;
    m_settings->beginGroup(group);
    QStringList keys = m_settings->childKeys();
    for(QString key : keys)
        m_settings->remove(key);
    m_settings->endGroup();
}
