// src/thememanager.cpp
#include "thememanager.h"
#include <QColor>
#include <QGuiApplication>
#include <QPalette>

ThemeManager::ThemeManager(QObject *parent)
    : QObject(parent)
    , m_isDarkTheme(false)
    , m_isAutoTheme(true)
    , m_settings("StellarMate", "Discovery")
{
    loadTheme();

    // Install event filter to monitor system palette changes
    QGuiApplication::instance()->installEventFilter(this);

    // If auto theme is enabled, update from system
    if (m_isAutoTheme)
    {
        updateThemeFromSystem();
    }
}

void ThemeManager::setIsDarkTheme(bool isDark)
{
    if (m_isDarkTheme != isDark)
    {
        m_isDarkTheme = isDark;
        // When user explicitly sets theme, disable auto mode
        m_isAutoTheme = false;
        saveTheme();
        emit isDarkThemeChanged();
        emit isAutoThemeChanged();
        emit themeChanged();
    }
}

void ThemeManager::toggleTheme()
{
    setIsDarkTheme(!m_isDarkTheme);
}

void ThemeManager::onSystemThemeChanged()
{
    if (m_isAutoTheme)
    {
        updateThemeFromSystem();
    }
}

void ThemeManager::updateThemeFromSystem()
{
    bool systemDark = getSystemDarkTheme();
    if (m_isDarkTheme != systemDark)
    {
        m_isDarkTheme = systemDark;
        emit isDarkThemeChanged();
        emit themeChanged();
    }
}

bool ThemeManager::getSystemDarkTheme() const
{
    // Check system palette to determine if dark theme is active
    QPalette palette = QGuiApplication::palette();
    QColor windowColor = palette.color(QPalette::Window);
    QColor textColor = palette.color(QPalette::WindowText);

    // If window is darker than text, we're likely in dark mode
    return windowColor.lightness() < textColor.lightness();
}

QColor ThemeManager::backgroundColor() const
{
    return m_isDarkTheme ? QColor("#1a1a1a") : QColor("#f5f6fa");
}

QColor ThemeManager::surfaceColor() const
{
    return m_isDarkTheme ? QColor("#2d3748") : QColor("#ffffff");
}

QColor ThemeManager::primaryColor() const
{
    return m_isDarkTheme ? QColor("#ff6600") : QColor("#ff6600");
}

QColor ThemeManager::onPrimaryColor() const
{
    return QColor("#ffffff");
}

QColor ThemeManager::secondaryColor() const
{
    return m_isDarkTheme ? QColor("#0098cc") : QColor("#0098cc");
}

QColor ThemeManager::dangerColor() const
{
    return m_isDarkTheme ? QColor("#cf021b") : QColor("#cf021b");
}

QColor ThemeManager::textColor() const
{
    return m_isDarkTheme ? QColor("#e2e8f0") : QColor("#2c3e50");
}

QColor ThemeManager::secondaryTextColor() const
{
    return m_isDarkTheme ? QColor("#9ca3af") : QColor("#6b7280");
}

QColor ThemeManager::borderColor() const
{
    return m_isDarkTheme ? QColor("#4b5563") : QColor("#e5e7eb");
}

QColor ThemeManager::statusBarColor() const
{
    return m_isDarkTheme ? QColor("#374151") : QColor("#f8f9ff");
}

bool ThemeManager::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == QGuiApplication::instance() && event->type() == QEvent::ApplicationPaletteChange)
    {
        onSystemThemeChanged();
    }
    return QObject::eventFilter(obj, event);
}

void ThemeManager::loadTheme()
{
    m_isAutoTheme = m_settings.value("isAutoTheme", true).toBool();
    m_isDarkTheme = m_settings.value("isDarkTheme", false).toBool();
}

void ThemeManager::saveTheme()
{
    m_settings.setValue("isAutoTheme", m_isAutoTheme);
    m_settings.setValue("isDarkTheme", m_isDarkTheme);
}
