#include "../headers/scstwappthememanager.h"

ScStwAppThemeManager::ScStwAppThemeManager(QObject *parent) : QObject(parent)
{
    QFontDatabase::addApplicationFont(":/fonts/fa5solid.woff");
    QFontDatabase::addApplicationFont(":/fonts/PTMono-Regular.ttf");

    QVariantMap icons = {
        {"back", "\uf053"},
        {"settings", "\uf013"},
        {"toppad", "\uf10a"},
        {"startpad", "\uf3fa"},
        {"profiles", "\uf007"},
        {"confirm", "\uf00c"},
        {"volumeUp", "\uf028"},
        {"volumeDown", "\uf027"}
    };

    QVariantMap fonts= {
        {"icons", "Font Awesome 5 Free"},
        {"timers", "PT Mono"}
    };

    ScStwAppTheme * lightTheme = new ScStwAppTheme (
                "Light",
    {
                    {"accent", "#0094ff"},
                    {"background", "white"},

                    {"button", "white"},
                    {"buttonPressed", "lightgrey"},
                    {"buttonBorder", "grey"},
                    {"disabledButton", "#d5d5d5"},
                    {"buttonText", "grey"},
                    {"buttonPressedText",this->lighter("lightgrey", 0.5)},
                    {"buttonDisabledText", this->lighter("lightgrey", 0.9)},

                    {"view", "white"},
                    {"menu", "#f8f8f8"},

                    {"delegate1", "#202227"},
                    {"delegate2", "#202227"},
                    {"delegateBackground", "white"},
                    {"delegatePressed", "#dddedf"},

                    {"text", "black"},
                    {"textDark", "#232323"},
                    {"disabledText", "grey"},

                    {"slider", "#6ccaf2"},

                    {"success", "#60de26"},
                    {"error", "#ff0000"},
                    {"warning", "#e0b928"},

                    {"line", "grey"},
                },
                icons,
                fonts,
    {
                    {"backIcon", "qrc:/graphics/icons/back_black.png"},
                    {"settIcon", "qrc:/graphics/icons/settings_black.png"},
                    {"buzzerIcon", "qrc:/graphics/icons/buzzer_black.png"},
                    {"startpadIcon", "qrc:/graphics/icons/startpad_black.png"},
                    {"baseStationIcon", "qrc:/images/BaseStationBlack.png"},
                    {"profilesIcon", "qrc:/graphics/icons/user_black.png"},
                    {"confirmIcon", "qrc:/graphics/icons/ok_black.png"}
                });

    ScStwAppTheme *darkTheme = new ScStwAppTheme(
                "Dark",
    {
                    {"accent", "#0094ff"},
                    {"background", "#2d3037"},

                    {"button", "#202227"},
                    {"buttonPressed", "#41454f"},
                    {"buttonBorder", "grey"},
                    {"disabledButton", "#555555"},
                    {"buttonText", "white"},
                    {"buttonPressedText",this->lighter("lightgrey", 0.8)},
                    {"buttonDisabledText", this->lighter("lightgrey", 0.6)},

                    {"view", "#202227"},
                    {"menu", "#292b32"},

                    {"delegate1", "#202227"},
                    {"delegate2", "#202227"},
                    {"delegateBackground", "#202227"},
                    {"delegatePressed", "#41454f"},

                    {"text", "#ffffff"},
                    {"textDark", "#232323"},
                    {"disabledText", "#777777"},

                    {"slider", "#6ccaf2"},

                    {"success", "#6bd43b"},
                    {"error", "#e03b2f"},
                    {"warning", "#e0b928"},

                    {"line", "grey"},

                    {"iconFontName", "Font Awesome 5 Free"},
                },
                icons,
                fonts,
    {
                    {"backIcon", "qrc:/graphics/icons/back.png"},
                    {"settIcon", "qrc:/graphics/icons/settings.png"},
                    {"buzzerIcon", "qrc:/graphics/icons/buzzer.png"},
                    {"startpadIcon", "qrc:/graphics/icons/startpad.png"},
                    {"baseStationIcon", "qrc:/images/BaseStationWhite.png"},
                    {"profilesIcon", "qrc:/graphics/icons/user.png"},
                    {"confirmIcon", "qrc:/graphics/icons/ok.png"}
                }
                );

    this->themes.append(lightTheme);
    this->themes.append(darkTheme);
    this->currentTheme = this->themes[0];
}

ScStwAppTheme* ScStwAppThemeManager::getTheme() {
    return this->currentTheme;
}

bool ScStwAppThemeManager::setTheme(QString themeName) {
    ScStwAppTheme * foundTheme = this->findThemeByName(themeName);

    if(foundTheme == nullptr)
        return false;

    this->currentTheme = foundTheme;
    emit this->themeChanged();

    return true;
}

ScStwAppTheme * ScStwAppThemeManager::findThemeByName(QString themeName) {
    foreach (ScStwAppTheme *theme, this->themes) {
        if(theme->getName() == themeName)
            return theme;
    }

    return nullptr;
}

QString ScStwAppThemeManager::lighter(QString color, double factor) {
    QColor qcolor = QColor(color);
    qcolor.toHsv();
    int h, s, v;
    qcolor.getHsv(&h, &s, &v);
    qcolor.setHsv(h,s,v * factor);
    return qcolor.name();
}
