#ifndef THEMEMANAGER_H
#define THEMEMANAGER_H

#include <QColor>
#include <QFont>
#include <QJsonObject>
#include <QMap>
#include <QObject>
#include <QVector>

class ThemeManager : public QObject
{
    Q_OBJECT
public:
    enum ThemeMode { Light, Dark };
    enum SizeMode { Small, Medium, Large };

    enum ColorToken {
        colorText = 1,
        colorTextSecondary,
        colorTextTertiary,
        colorTextQuaternary,

        colorFill,
        colorFillSecondary,
        colorFillTertiary,
        colorFillQuaternary,

        colorBgSolid,
        colorBgSolidHover,
        colorBgSolidActive,

        colorBgElevated,
        colorBgContainer,
        colorBgLayout,
        colorBgSpotlight,
        colorBgBlur,

        colorBorder,
        colorBorderSecondary,

        colorPrimaryBg,
        colorPrimaryBgHover,
        colorPrimaryBorder,
        colorPrimaryBorderHover,
        colorPrimaryHover,
        colorPrimary,
        colorPrimaryActive,
        colorPrimaryTextHover,
        colorPrimaryText,
        colorPrimaryTextActive,

        colorSuccessBg,
        colorSuccessBgHover,
        colorSuccessBorder,
        colorSuccessBorderHover,
        colorSuccessHover,
        colorSuccess,
        colorSuccessActive,
        colorSuccessTextHover,
        colorSuccessText,
        colorSuccessTextActive,

        colorErrorBg,
        colorErrorBgHover,
        colorErrorBgFilledHover,
        colorErrorBgActive,
        colorErrorBorder,
        colorErrorBorderHover,
        colorErrorHover,
        colorError,
        colorErrorActive,
        colorErrorTextHover,
        colorErrorText,
        colorErrorTextActive,

        colorWarningBg,
        colorWarningBgHover,
        colorWarningBorder,
        colorWarningBorderHover,
        colorWarningHover,
        colorWarning,
        colorWarningActive,
        colorWarningTextHover,
        colorWarningText,
        colorWarningTextActive,

        colorInfoBg,
        colorInfoBgHover,
        colorInfoBorder,
        colorInfoBorderHover,
        colorInfoHover,
        colorInfo,
        colorInfoActive,
        colorInfoTextHover,
        colorInfoText,
        colorInfoTextActive,

        colorLinkHover,
        colorLink,
        colorLinkActive,

        colorTextBase,
        colorBgBase,

        colorBgMask,
        colorWhite,
    };
    Q_ENUM(ColorToken);

    // 尺寸系统
    enum SizeToken {
        SizeXXL,
        SizeXL,
        SizeLG,
        SizeMD,
        SizeMS,
        size,
        SizeSM,
        SizeXS,
        SizeXXS,
        SizeTokenCount
    };
    Q_ENUM(SizeToken);

    enum ControlHeightToken {
        controlHeightSM,
        controlHeightXS,
        controlHeightLG,
    };
    Q_ENUM(ControlHeightToken);
    // 圆角
    enum BorderToken {
        BorderRadiusSM,
        BorderRadiusMD,
        BorderRadiusLG,
        BorderRadiusCircle,
        BorderTokenCount
    };
    Q_ENUM(BorderToken);

    static ThemeManager& instance();

    void loadConfig(const QJsonObject& object);
    void loadConfig(const QString& configPath);
    void applyTheme();
    QColor color(const QString& name) const;
    QColor color(ColorToken token) const;
    QHash<QString, QColor> colors() const { return m_colors; }

    void setPrimaryColor(const QColor& color);
    QColor primaryColor() const { return token.colorPrimary; }

    void toggleThemeMode();
    ThemeMode themeMode() const;
    void setThemeMode(ThemeMode themeMode);

    SizeMode sizeMode() const;
    void setSizeMode(SizeMode mode);
signals:
    void themeChanged();

private:
    struct SeedTokens
    {
        // Color
        QColor colorPrimary;
        QColor colorSuccess;
        QColor colorWarning;
        QColor colorError;
        QColor colorInfo;
        QColor colorLink;
        QColor colorTextBase;

        QColor colorBgBase;
        // Font
        QFont fontFamily;

        // Line
        int lineWidth;
        QString lineType;

        // Radius
        int borderRadius;

        // Size
        int sizeUnit;
        int sizeStep;
        int sizePopupArrow;

        int controlHeight;
    };
    ThemeManager(QObject* parent = nullptr);
    void generateDerivedColors();
    QString loadQssTemplate() const;
    QString processQss(const QString& qss) const;

    // 类似 Antd 的主题系统
    QVector<QColor> generateAntdPalette(const QColor& baseColor) const;
    QVector<QColor> applyDarkMap(const QVector<QColor>& palette,
                                 const QColor& backgroundColor = QColor()) const;

    void generateNeutralColorPalettes(const QColor& baseColor, const QColor& textBaseColor);

    QColor mixColor(const QColor& base, const QColor& target, int ratio) const;

    //
    QMap<SizeToken, int> genSizeMapToken(const SeedTokens& token);
    QMap<ControlHeightToken, int> genControlHeight(const SeedTokens& token);

    ThemeMode m_themeMode = Light;
    SizeMode m_sizeMode = Small;

    SeedTokens token;

    QJsonObject m_colorConfig;
    QJsonObject m_sizeConfig;

    QHash<QString, QColor> m_colors; // 作为字符串使用
    QMap<ColorToken, QColor> m_colorTokens;
};

#endif // THEMEMANAGER_H
