// src/thememanager.h
#ifndef THEMEMANAGER_H
#define THEMEMANAGER_H

#include <QObject>
#include <QSettings>
#include <qqml.h>
#include <QColor>
#include <QGuiApplication>
#include <QPalette>
#include <QEvent>

class ThemeManager : public QObject
{
        Q_OBJECT
        QML_ELEMENT
        Q_PROPERTY(bool isDarkTheme READ isDarkTheme WRITE setIsDarkTheme NOTIFY isDarkThemeChanged)
        Q_PROPERTY(bool isAutoTheme READ isAutoTheme NOTIFY isAutoThemeChanged)
        Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY themeChanged)
        Q_PROPERTY(QColor surfaceColor READ surfaceColor NOTIFY themeChanged)
        Q_PROPERTY(QColor primaryColor READ primaryColor NOTIFY themeChanged)
        Q_PROPERTY(QColor onPrimaryColor READ onPrimaryColor NOTIFY themeChanged)
        Q_PROPERTY(QColor secondaryColor READ secondaryColor NOTIFY themeChanged)
        Q_PROPERTY(QColor dangerColor READ dangerColor NOTIFY themeChanged)
        Q_PROPERTY(QColor textColor READ textColor NOTIFY themeChanged)
        Q_PROPERTY(QColor secondaryTextColor READ secondaryTextColor NOTIFY themeChanged)
        Q_PROPERTY(QColor borderColor READ borderColor NOTIFY themeChanged)
        Q_PROPERTY(QColor statusBarColor READ statusBarColor NOTIFY themeChanged)

    public:
        explicit ThemeManager(QObject *parent = nullptr);

        bool isDarkTheme() const
        {
            return m_isDarkTheme;
        }
        void setIsDarkTheme(bool isDark);

        bool isAutoTheme() const
        {
            return m_isAutoTheme;
        }

        // Theme colors
        QColor backgroundColor() const;
        QColor surfaceColor() const;
        QColor primaryColor() const;
        QColor onPrimaryColor() const;
        QColor secondaryColor() const;
        QColor dangerColor() const;
        QColor textColor() const;
        QColor secondaryTextColor() const;
        QColor borderColor() const;
        QColor statusBarColor() const;

    protected:
        bool eventFilter(QObject *obj, QEvent *event) override;

    public slots:
        void toggleTheme();

    signals:
        void isDarkThemeChanged();
        void isAutoThemeChanged();
        void themeChanged();

    private slots:
        void onSystemThemeChanged();

    private:
        void loadTheme();
        void saveTheme();
        void updateThemeFromSystem();
        bool getSystemDarkTheme() const;

        bool m_isDarkTheme;
        bool m_isAutoTheme;
        QSettings m_settings;
};

#endif // THEMEMANAGER_H
