// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#pragma once

#include "imgui.h"
#include <string>
#include <unordered_map>

namespace dragonx {
namespace ui {
namespace schema {

/**
 * @brief CSS custom property resolver for color values
 *
 * Resolves color strings in multiple formats:
 * - var(--name)     → lookup from theme palette
 * - #RRGGBB        → direct hex (6 digits, alpha=255)
 * - #RRGGBBAA      → direct hex with alpha (8 digits)
 * - rgba(r,g,b,a)  → CSS-style with float alpha 0.0-1.0
 * - transparent     → fully transparent
 * - empty string    → returns fallback
 */
class ColorVarResolver {
public:
    /**
     * @brief Set the palette map (called when skin is loaded)
     * @param palette Map of "--name" → ImU32 color values
     */
    void setPalette(const std::unordered_map<std::string, ImU32>& palette);

    /**
     * @brief Get the current palette
     */
    const std::unordered_map<std::string, ImU32>& palette() const { return palette_; }

    /**
     * @brief Resolve a color string to an ImU32 value
     *
     * @param value Color string (var ref, hex, rgba, or "transparent")
     * @param fallback Value to return if resolution fails or value is empty
     * @return Resolved ImU32 color
     */
    ImU32 resolve(const std::string& value, ImU32 fallback = IM_COL32(0,0,0,0)) const;

    /**
     * @brief Check if a color string is non-empty (has an override)
     */
    static bool hasValue(const std::string& value);

    /**
     * @brief Parse a hex color string (#RRGGBB or #RRGGBBAA)
     * @param hex The hex string (with or without # prefix)
     * @param[out] out Parsed color
     * @return true if parsed successfully
     */
    static bool parseHex(const std::string& hex, ImU32& out);

    /**
     * @brief Parse an rgba() color string
     * @param rgba String like "rgba(183,28,28,0.5)"
     * @param[out] out Parsed color
     * @return true if parsed successfully
     */
    static bool parseRgba(const std::string& rgba, ImU32& out);

private:
    std::unordered_map<std::string, ImU32> palette_;
};

} // namespace schema
} // namespace ui
} // namespace dragonx
