#include "ThemeManager.h"

#include <QApplication>
#include <QStyleFactory>

ThemeManager::ThemeManager(QObject *parent)
    : QObject(parent)
{
}

void ThemeManager::applyTheme(Theme theme)
{
    m_currentTheme = theme;
    QApplication::setStyle(QStyleFactory::create("Fusion"));

    if (theme == Theme::Dark)
    {
        QApplication::setPalette(buildDarkPalette());
    }
    else
    {
        QApplication::setPalette(buildLightPalette());
    }

    emit themeChanged(theme);
}

ThemeManager::Theme ThemeManager::currentTheme() const
{
    return m_currentTheme;
}

QPalette ThemeManager::buildDarkPalette()
{
    QPalette p;

    const QColor bg       = QColor("#1e1e1e");
    const QColor widget   = QColor("#252526");
    const QColor alt      = QColor("#2d2d30");
    const QColor hi       = QColor("#264f78");
    const QColor hiText   = QColor("#ffffff");
    const QColor text     = QColor("#d4d4d4");
    const QColor disabled = QColor("#6d6d6d");
    const QColor btn      = QColor("#3c3c3c");
    const QColor mid      = QColor("#333333");
    const QColor dark     = QColor("#1a1a1a");
    const QColor light    = QColor("#454545");
    const QColor link     = QColor("#569cd6");

    p.setColor(QPalette::Window,          bg);
    p.setColor(QPalette::WindowText,      text);
    p.setColor(QPalette::Base,            widget);
    p.setColor(QPalette::AlternateBase,   alt);
    p.setColor(QPalette::Text,            text);
    p.setColor(QPalette::Button,          btn);
    p.setColor(QPalette::ButtonText,      text);
    p.setColor(QPalette::Highlight,       hi);
    p.setColor(QPalette::HighlightedText, hiText);
    p.setColor(QPalette::Link,            link);
    p.setColor(QPalette::LinkVisited,     link.darker(120));
    p.setColor(QPalette::Mid,             mid);
    p.setColor(QPalette::Dark,            dark);
    p.setColor(QPalette::Light,           light);
    p.setColor(QPalette::Shadow,          QColor("#000000"));
    p.setColor(QPalette::ToolTipBase,     widget);
    p.setColor(QPalette::ToolTipText,     text);
    p.setColor(QPalette::PlaceholderText, disabled);

    p.setColor(QPalette::Disabled, QPalette::WindowText, disabled);
    p.setColor(QPalette::Disabled, QPalette::Text,       disabled);
    p.setColor(QPalette::Disabled, QPalette::ButtonText, disabled);

    return p;
}

QPalette ThemeManager::buildLightPalette()
{
    // Fusion-Standard-Palette
    QPalette p;

    const QColor bg       = QColor("#f3f3f3");
    const QColor widget   = QColor("#ffffff");
    const QColor alt      = QColor("#e8e8e8");
    const QColor hi       = QColor("#0078d4");
    const QColor hiText   = QColor("#ffffff");
    const QColor text     = QColor("#1e1e1e");
    const QColor disabled = QColor("#a0a0a0");
    const QColor btn      = QColor("#e1e1e1");
    const QColor mid      = QColor("#c8c8c8");
    const QColor dark     = QColor("#a0a0a0");
    const QColor light    = QColor("#ffffff");
    const QColor link     = QColor("#0078d4");

    p.setColor(QPalette::Window,          bg);
    p.setColor(QPalette::WindowText,      text);
    p.setColor(QPalette::Base,            widget);
    p.setColor(QPalette::AlternateBase,   alt);
    p.setColor(QPalette::Text,            text);
    p.setColor(QPalette::Button,          btn);
    p.setColor(QPalette::ButtonText,      text);
    p.setColor(QPalette::Highlight,       hi);
    p.setColor(QPalette::HighlightedText, hiText);
    p.setColor(QPalette::Link,            link);
    p.setColor(QPalette::LinkVisited,     link.darker(130));
    p.setColor(QPalette::Mid,             mid);
    p.setColor(QPalette::Dark,            dark);
    p.setColor(QPalette::Light,           light);
    p.setColor(QPalette::PlaceholderText, disabled);

    p.setColor(QPalette::Disabled, QPalette::WindowText, disabled);
    p.setColor(QPalette::Disabled, QPalette::Text,       disabled);
    p.setColor(QPalette::Disabled, QPalette::ButtonText, disabled);

    return p;
}
