/**
 * @file Theme.cpp
 */

#include "Theme.h"

#include <QApplication>
#include <QPalette>

namespace shq {

QColor tracePalette(int idx) {
    static const QColor pal[8] = {
        QColor(0x89, 0xb4, 0xfa), /* blue */
        QColor(0xa6, 0xe3, 0xa1), /* green */
        QColor(0xf3, 0x8b, 0xa8), /* red */
        QColor(0xfa, 0xb3, 0x87), /* peach */
        QColor(0xcb, 0xa6, 0xf7), /* mauve */
        QColor(0x94, 0xe2, 0xd5), /* teal */
        QColor(0x89, 0xdc, 0xeb), /* sky */
        QColor(0xb4, 0xbe, 0xfe), /* lavender */
    };
    if (idx < 0) idx = 0;
    return pal[idx % 8];
}

void applyTheme(QApplication& app) {
    app.setStyle("Fusion");

    QPalette p;
    p.setColor(QPalette::Window,          col::base());
    p.setColor(QPalette::WindowText,      col::text());
    p.setColor(QPalette::Base,            col::mantle());
    p.setColor(QPalette::AlternateBase,   col::surface0());
    p.setColor(QPalette::ToolTipBase,     col::surface0());
    p.setColor(QPalette::ToolTipText,     col::text());
    p.setColor(QPalette::Text,            col::text());
    p.setColor(QPalette::Button,          col::surface0());
    p.setColor(QPalette::ButtonText,      col::text());
    p.setColor(QPalette::BrightText,      col::red());
    p.setColor(QPalette::Link,            col::blue());
    p.setColor(QPalette::Highlight,       col::blue());
    p.setColor(QPalette::HighlightedText, col::crust());
    p.setColor(QPalette::PlaceholderText, col::overlay0());
    p.setColor(QPalette::Disabled, QPalette::Text,       col::overlay0());
    p.setColor(QPalette::Disabled, QPalette::ButtonText, col::overlay0());
    p.setColor(QPalette::Disabled, QPalette::WindowText, col::overlay0());
    app.setPalette(p);

    app.setStyleSheet(QStringLiteral(R"qss(
        QToolTip { color: #cdd6f4; background-color: #313244; border: 1px solid #45475a; }
        QPushButton {
            background-color: #313244; color: #cdd6f4;
            border: 1px solid #45475a; border-radius: 5px;
            padding: 4px 9px;
        }
        QPushButton:hover    { background-color: #45475a; }
        QPushButton:pressed  { background-color: #585b70; }
        QPushButton:checked  { background-color: #3a4a6a; border-color: #89b4fa; color: #89b4fa; }
        QPushButton:disabled { color: #6c7086; }
        QComboBox, QSpinBox, QDoubleSpinBox, QLineEdit {
            background-color: #313244; color: #cdd6f4;
            border: 1px solid #45475a; border-radius: 5px; padding: 3px 6px;
        }
        QComboBox QAbstractItemView { background-color: #181825; color: #cdd6f4; selection-background-color: #45475a; }
        QTreeWidget, QTableWidget, QListWidget {
            background-color: #181825; color: #cdd6f4;
            border: 1px solid #313244; border-radius: 6px;
        }
        QHeaderView::section { background-color: #313244; color: #cdd6f4; border: none; padding: 4px; }
        QSplitter::handle { background-color: #313244; }
        QToolBar { background-color: #11111b; border: none; spacing: 4px; padding: 3px; }
        QMenu { background-color: #181825; color: #cdd6f4; border: 1px solid #45475a; }
        QMenu::item:selected { background-color: #45475a; }
        QSlider::groove:horizontal { height: 4px; background: #45475a; border-radius: 2px; }
        QSlider::handle:horizontal { width: 14px; background: #89b4fa; border-radius: 7px; margin: -5px 0; }
        QLabel { color: #cdd6f4; }
        QDialog, QMainWindow { background-color: #1e1e2e; }
    )qss"));
}

} /* namespace shq */
