#include "settingsmanager.h"

#include <QDir>
#include <QCoreApplication>
#include <QSettings>
#include <Common/constants.h>

SettingsManager::SettingsManager()
{

}

bool SettingsManager::saveDefaultDir(QString dir)
{
    bool isSave = false;

    if(!dir.isEmpty() && QDir(dir).exists())
    {
        QSettings settings(getSettingsPath(), QSettings::IniFormat);
        settings.setValue(Constants::Settings::defaultDirKey, dir);
        settings.setValue(Constants::Settings::alwaysAskKey, false);
        isSave = true;
    }

    return isSave;
}

void SettingsManager::saveAlwaysAsk(bool isAlwaysAsk)
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    settings.setValue(Constants::Settings::alwaysAskKey, isAlwaysAsk);
}

void SettingsManager::saveLanguage(QString language)
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    settings.setValue(Constants::Settings::languageKey, language);
}

void SettingsManager::saveShowInitTips(bool show)
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);   
    settings.setValue(Constants::Settings::beforeStartKey, show);
}

void SettingsManager::clearValues()
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    settings.clear();
}

QString SettingsManager::getCurrentLanguage()
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    return settings.value(Constants::Settings::languageKey).toString();
}

QString SettingsManager::getDefaultDir()
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    return settings.value(Constants::Settings::defaultDirKey).toString();
}

bool SettingsManager::getIsAlwaysAsk()
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    return settings.value(Constants::Settings::alwaysAskKey).toBool();
}

bool SettingsManager::getShowInitTips()
{
    QSettings settings(getSettingsPath(), QSettings::IniFormat);
    QVariant value =  settings.value(Constants::Settings::beforeStartKey);

    if(value == QVariant::Invalid)
        return true;

    return value.toBool();

}

QString SettingsManager::getSettingsPath()
{
    return QCoreApplication::applicationDirPath().append(Constants::Settings::settingsName);
}
