#include <QSettings>

#include "settings.h"


namespace Settings {

QMap<QString, QVariant> defaults = {
    // General info
    {"General/UI_scale", 100},
    {"General/CurrentInitStage", "0"},
    {"General/MicroUpdates", false},
    {"General/PatchDownloadDir", "data"},

    // Lotro Manager
    {"Lotro/game_path", "none"},
    {"Lotro/original_locale", "English"},
    {"Lotro/skip_raw_download", true},
    {"Lotro/no_splash_screen", false},

    // Backup
    {"Backup/installed", false},
    {"Backup/path", "/backup/"},
    {"Backup/creation_time", "none"},

    // Databases download settings
    {"DatabaseDownload/text", false},          // TextsPatch
    {"DatabaseDownload/font", false},          // TextsPatch
    {"DatabaseDownload/image", false},         // GraphicsPatch
    {"DatabaseDownload/loadscreen", false},    // GraphicsPatch
    {"DatabaseDownload/texture", false},       // GraphicsPatch
    {"DatabaseDownload/sound", false},         // SoundsPatch
    {"DatabaseDownload/video", false},         // VideosPatch
    {"DatabaseDownload/micro", false},         // MicroPatch

    // Flags, meaning that database is fresh and needs to be installed

    {"DatabaseNeedInstall/text", false},           // TextsPatch
    {"DatabaseNeedInstall/font", false},           // TextsPatch
    {"DatabaseNeedInstall/image", false},          // GraphicsPatch
    {"DatabaseNeedInstall/loadscreen", false},     // GraphicsPatch
    {"DatabaseNeedInstall/texture", false},        // GraphicsPatch
    {"DatabaseNeedInstall/sound", false},          // SoundsPatch
    {"DatabaseNeedInstall/video", false},          // VideosPatch
    {"DatabaseNeedInstall/micro", false},          // MircoPatch

    // Flags, meaning that database was installed and needs to be activated

    {"DatabaseNeedActivate/text", false},           // TextsPatch
    {"DatabaseNeedActivate/font", false},           // TextsPatch
    {"DatabaseNeedActivate/image", false},          // GraphicsPatch
    {"DatabaseNeedActivate/loadscreen", false},     // GraphicsPatch
    {"DatabaseNeedActivate/texture", false},        // GraphicsPatch
    {"DatabaseNeedActivate/sound", false},          // SoundsPatch
    {"DatabaseNeedActivate/video", false},          // VideosPatch
    {"DatabaseNeedActivate/micro", false},          // MircoPatch

    // Localisation components
    {"Components/texts_main", false},          // TextsPatch
    {"Components/texts_items", false},         // TextsPatch
    {"Components/texts_emotes", false},        // TextsPatch
    {"Components/maps", false},                // ImagesPatch
    {"Components/loadscreens", false},         // ImagesPatch
    {"Components/textures", false},            // ImagesPatch
    {"Components/sounds", false},              // SoundsPatch
    {"Components/videos", false},              // VideosPatch
    {"Components/micropatch", false},          // PatchList

    // Network
    {"Network/site_url", "http://translate.lotros.ru/"},
    {"Network/forum_url", "http://lotros.ru/"},
    {"Network/discord_url", "https://discord.gg/j25MdKR"},
    {"Network/add_report_url", "http://translate.lotros.ru/bugs/add"},
    {"Network/donate_url", "http://translate.lotros.ru/donate"},
    {"Network/game_servers_status", "http://translate.lotros.ru/servers.txt"},
    {"Network/game_servers_message", "http://translate.lotros.ru/profmessage.txt"},
    {"Network/weekly_code_url", "http://translate.lotros.ru/coupon.txt"},
    {"Network/news_list_url", "http://translate.lotros.ru/groupware/launcher_news/30/1"},
    {"Network/patch_updates_url", "http://translate.lotros.ru/groupware/check_updates"}
};

void setDefaultSettings()
{
    QSettings settings;
    foreach (QString key, defaults.keys()) {
        settings.setValue(key, defaults[key]);
    }
}

QVariant getValue(QString key) {
    QSettings settings;
    return settings.value(key, defaults.value(key, QVariant()));
}

void setValue(QString key, QVariant value) {
    QSettings settings;
    settings.setValue(key, value);
}

SettingsBackup createSettingsBackup() {
    QSettings settings;
    QMap<QString, QVariant> keysValuesPairs;
    QStringList keys = settings.allKeys();
    QStringListIterator it(keys);

    while (it.hasNext()) {
        QString currentKey = it.next();
        keysValuesPairs.insert(currentKey, settings.value(currentKey));
    }

    return keysValuesPairs;
}

void restoreFromSettingsBackup(const SettingsBackup& backup) {
    QSettings settings;
    settings.clear();

    for (auto i = backup.begin(); i != backup.end(); ++i) {
        settings.setValue(i.key(), i.value());
    }
}

void updatePatchComponentsDependencies() {
    QSettings settings;

    auto isComponentActive = [&settings](const QString& component) -> bool {
        return settings.value("Components/" + component).toBool();
    };

    if (isComponentActive("texts_main") || isComponentActive("texts_items")
            || isComponentActive("texts_emotes") || isComponentActive("videos")) {
        // Videos also need text database, because it contains videos path references
        settings.setValue("DatabaseDownload/text", true);
    } else {
        settings.setValue("DatabaseDownload/text", false);
    }

    if (isComponentActive("texts_main") || isComponentActive("texts_items")
            || isComponentActive("texts_emotes")) {
        settings.setValue("DatabaseDownload/font", true);
    } else {
        settings.setValue("DatabaseDownload/font", false);
    }

    settings.setValue("DatabaseDownload/image", isComponentActive("maps"));
    settings.setValue("DatabaseDownload/loadscreen", isComponentActive("loadscreens"));
    settings.setValue("DatabaseDownload/texture", isComponentActive("textures"));
    settings.setValue("DatabaseDownload/sound", isComponentActive("sounds"));
    settings.setValue("DatabaseDownload/video", isComponentActive("videos"));
    settings.setValue("DatabaseDownload/micro", isComponentActive("micropatch"));
}

}  /* namespace Settings */

