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

#include "theme.h"
#include "material/color_theme.h"
#include "schema/ui_schema.h"
#include "imgui.h"

namespace dragonx {
namespace ui {

// ============================================================================
// Current Acrylic Theme State
// ============================================================================

static AcrylicTheme s_currentAcrylicTheme;
static bool s_acrylicThemeInitialized = false;

// ============================================================================
// Acrylic Theme Presets
// ============================================================================

AcrylicTheme GetDragonXAcrylicTheme()
{
    AcrylicTheme theme;
    
    // Sidebar: Deep navy tint (DragonX brand)
    // #1B2432 ≈ (0.106, 0.141, 0.196)
    theme.sidebar.tintColor = ImVec4(0.11f, 0.14f, 0.20f, 1.0f);
    theme.sidebar.tintOpacity = 0.85f;
    theme.sidebar.luminosityOpacity = 0.4f;
    theme.sidebar.blurRadius = 40.0f;
    theme.sidebar.noiseOpacity = 0.02f;
    theme.sidebar.fallbackColor = ImVec4(0.07f, 0.08f, 0.13f, 0.85f);
    theme.sidebar.enabled = true;
    
    // Popups: Dark navy with subtle blue tint
    theme.popup.tintColor = ImVec4(0.09f, 0.11f, 0.16f, 1.0f);
    theme.popup.tintOpacity = 0.80f;
    theme.popup.luminosityOpacity = 0.5f;
    theme.popup.blurRadius = 30.0f;
    theme.popup.noiseOpacity = 0.02f;
    theme.popup.fallbackColor = ImVec4(0.09f, 0.11f, 0.16f, 0.88f);
    theme.popup.enabled = true;
    
    // Cards: Subtle navy tint
    theme.card.tintColor = ImVec4(0.10f, 0.13f, 0.18f, 1.0f);
    theme.card.tintOpacity = 0.65f;
    theme.card.luminosityOpacity = 0.6f;
    theme.card.blurRadius = 20.0f;
    theme.card.noiseOpacity = 0.015f;
    theme.card.fallbackColor = ImVec4(0.10f, 0.13f, 0.18f, 0.80f);
    theme.card.enabled = true;
    
    // Context menus: Dark navy, crisp
    theme.menu.tintColor = ImVec4(0.08f, 0.09f, 0.14f, 1.0f);
    theme.menu.tintOpacity = 0.88f;
    theme.menu.luminosityOpacity = 0.45f;
    theme.menu.blurRadius = 25.0f;
    theme.menu.noiseOpacity = 0.02f;
    theme.menu.fallbackColor = ImVec4(0.08f, 0.09f, 0.14f, 0.88f);
    theme.menu.enabled = true;
    
    // Tooltips: Very transparent navy
    theme.tooltip.tintColor = ImVec4(0.07f, 0.08f, 0.12f, 1.0f);
    theme.tooltip.tintOpacity = 0.75f;
    theme.tooltip.luminosityOpacity = 0.5f;
    theme.tooltip.blurRadius = 15.0f;
    theme.tooltip.noiseOpacity = 0.01f;
    theme.tooltip.fallbackColor = ImVec4(0.07f, 0.08f, 0.12f, 0.95f);
    theme.tooltip.enabled = true;
    
    return theme;
}

AcrylicTheme GetDarkAcrylicTheme()
{
    AcrylicTheme theme;
    
    // Sidebar: Neutral dark
    theme.sidebar.tintColor = ImVec4(0.12f, 0.12f, 0.14f, 1.0f);
    theme.sidebar.tintOpacity = 0.80f;
    theme.sidebar.luminosityOpacity = 0.5f;
    theme.sidebar.blurRadius = 35.0f;
    theme.sidebar.noiseOpacity = 0.02f;
    theme.sidebar.fallbackColor = ImVec4(0.12f, 0.12f, 0.14f, 0.85f);
    theme.sidebar.enabled = true;
    
    // Popups
    theme.popup.tintColor = ImVec4(0.14f, 0.14f, 0.16f, 1.0f);
    theme.popup.tintOpacity = 0.78f;
    theme.popup.luminosityOpacity = 0.5f;
    theme.popup.blurRadius = 30.0f;
    theme.popup.noiseOpacity = 0.02f;
    theme.popup.fallbackColor = ImVec4(0.14f, 0.14f, 0.16f, 0.88f);
    theme.popup.enabled = true;
    
    // Cards
    theme.card.tintColor = ImVec4(0.15f, 0.15f, 0.17f, 1.0f);
    theme.card.tintOpacity = 0.65f;
    theme.card.luminosityOpacity = 0.55f;
    theme.card.blurRadius = 20.0f;
    theme.card.noiseOpacity = 0.015f;
    theme.card.fallbackColor = ImVec4(0.15f, 0.15f, 0.17f, 0.80f);
    theme.card.enabled = true;
    
    // Menus
    theme.menu.tintColor = ImVec4(0.12f, 0.12f, 0.14f, 1.0f);
    theme.menu.tintOpacity = 0.85f;
    theme.menu.luminosityOpacity = 0.45f;
    theme.menu.blurRadius = 25.0f;
    theme.menu.noiseOpacity = 0.02f;
    theme.menu.fallbackColor = ImVec4(0.12f, 0.12f, 0.14f, 0.88f);
    theme.menu.enabled = true;
    
    // Tooltips
    theme.tooltip.tintColor = ImVec4(0.10f, 0.10f, 0.12f, 1.0f);
    theme.tooltip.tintOpacity = 0.75f;
    theme.tooltip.luminosityOpacity = 0.5f;
    theme.tooltip.blurRadius = 15.0f;
    theme.tooltip.noiseOpacity = 0.01f;
    theme.tooltip.fallbackColor = ImVec4(0.10f, 0.10f, 0.12f, 0.95f);
    theme.tooltip.enabled = true;
    
    return theme;
}

AcrylicTheme GetLightAcrylicTheme()
{
    AcrylicTheme theme;
    
    // Sidebar: Light with slight tint
    theme.sidebar.tintColor = ImVec4(0.96f, 0.96f, 0.98f, 1.0f);
    theme.sidebar.tintOpacity = 0.82f;
    theme.sidebar.luminosityOpacity = 0.7f;
    theme.sidebar.blurRadius = 30.0f;
    theme.sidebar.noiseOpacity = 0.015f;
    theme.sidebar.fallbackColor = ImVec4(0.96f, 0.96f, 0.98f, 0.85f);
    theme.sidebar.enabled = true;
    
    // Popups
    theme.popup.tintColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
    theme.popup.tintOpacity = 0.85f;
    theme.popup.luminosityOpacity = 0.75f;
    theme.popup.blurRadius = 25.0f;
    theme.popup.noiseOpacity = 0.015f;
    theme.popup.fallbackColor = ImVec4(0.98f, 0.98f, 1.0f, 0.88f);
    theme.popup.enabled = true;
    
    // Cards
    theme.card.tintColor = ImVec4(0.98f, 0.98f, 1.0f, 1.0f);
    theme.card.tintOpacity = 0.70f;
    theme.card.luminosityOpacity = 0.75f;
    theme.card.blurRadius = 18.0f;
    theme.card.noiseOpacity = 0.01f;
    theme.card.fallbackColor = ImVec4(0.98f, 0.98f, 1.0f, 0.80f);
    theme.card.enabled = true;
    
    // Menus
    theme.menu.tintColor = ImVec4(0.98f, 0.98f, 1.0f, 1.0f);
    theme.menu.tintOpacity = 0.88f;
    theme.menu.luminosityOpacity = 0.7f;
    theme.menu.blurRadius = 22.0f;
    theme.menu.noiseOpacity = 0.015f;
    theme.menu.fallbackColor = ImVec4(0.98f, 0.98f, 1.0f, 0.88f);
    theme.menu.enabled = true;
    
    // Tooltips
    theme.tooltip.tintColor = ImVec4(0.95f, 0.95f, 0.97f, 1.0f);
    theme.tooltip.tintOpacity = 0.80f;
    theme.tooltip.luminosityOpacity = 0.7f;
    theme.tooltip.blurRadius = 12.0f;
    theme.tooltip.noiseOpacity = 0.01f;
    theme.tooltip.fallbackColor = ImVec4(0.95f, 0.95f, 0.97f, 0.95f);
    theme.tooltip.enabled = true;
    
    return theme;
}

const AcrylicTheme& GetCurrentAcrylicTheme()
{
    if (!s_acrylicThemeInitialized) {
        s_currentAcrylicTheme = GetDragonXAcrylicTheme();
        s_acrylicThemeInitialized = true;
    }
    return s_currentAcrylicTheme;
}

void SetCurrentAcrylicTheme(const AcrylicTheme& theme)
{
    s_currentAcrylicTheme = theme;
    s_acrylicThemeInitialized = true;
}

// ============================================================================
// ImGui Theme Functions
// ============================================================================

void SetDragonXTheme()
{
    ImGuiStyle& style = ImGui::GetStyle();
    ImVec4* colors = style.Colors;
    const auto& S = schema::UI();

    // DragonX brand colors:
    // Primary: Red (#F64740)
    // Background: Navy (#121420 / #1B2432)
    // Text: Blue-gray (#BFD1E5)

    // Main background colors — navy palette
    colors[ImGuiCol_WindowBg]             = ImVec4(0.07f, 0.08f, 0.13f, 1.00f);
    colors[ImGuiCol_ChildBg]              = ImVec4(0.11f, 0.14f, 0.20f, 1.00f);
    colors[ImGuiCol_PopupBg]              = ImVec4(0.14f, 0.18f, 0.25f, 0.98f);

    // Borders
    colors[ImGuiCol_Border]               = ImVec4(1.00f, 1.00f, 1.00f, 0.12f);
    colors[ImGuiCol_BorderShadow]         = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);

    // Frame backgrounds (inputs, checkboxes, etc.) — glass-card style
    colors[ImGuiCol_FrameBg]              = ImVec4(1.00f, 1.00f, 1.00f, 0.07f);
    colors[ImGuiCol_FrameBgHovered]       = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
    colors[ImGuiCol_FrameBgActive]        = ImVec4(1.00f, 1.00f, 1.00f, 0.15f);

    // Title bar — navy
    colors[ImGuiCol_TitleBg]              = ImVec4(0.11f, 0.14f, 0.20f, 1.00f);
    colors[ImGuiCol_TitleBgActive]        = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_TitleBgCollapsed]     = ImVec4(0.07f, 0.08f, 0.13f, 0.75f);

    // Menu bar — navy
    colors[ImGuiCol_MenuBarBg]            = ImVec4(0.11f, 0.14f, 0.20f, 1.00f);

    // Scrollbar — minimal glass style
    colors[ImGuiCol_ScrollbarBg]          = ImVec4(0, 0, 0, 0);
    colors[ImGuiCol_ScrollbarGrab]        = ImVec4(1.0f, 1.0f, 1.0f, 15.0f / 255.0f);
    colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(1.0f, 1.0f, 1.0f, 30.0f / 255.0f);
    colors[ImGuiCol_ScrollbarGrabActive]  = ImVec4(1.0f, 1.0f, 1.0f, 45.0f / 255.0f);

    // Checkmarks, sliders — DragonX red
    colors[ImGuiCol_CheckMark]            = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_SliderGrab]           = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_SliderGrabActive]     = ImVec4(0.97f, 0.48f, 0.46f, 1.00f);

    // Buttons — red accent
    colors[ImGuiCol_Button]               = ImVec4(0.96f, 0.28f, 0.25f, 0.80f);
    colors[ImGuiCol_ButtonHovered]        = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_ButtonActive]         = ImVec4(0.44f, 0.10f, 0.03f, 1.00f);

    // Headers (collapsing headers, tree nodes, selectable, menu items)
    colors[ImGuiCol_Header]               = ImVec4(0.96f, 0.28f, 0.25f, 0.50f);
    colors[ImGuiCol_HeaderHovered]        = ImVec4(0.96f, 0.28f, 0.25f, 0.70f);
    colors[ImGuiCol_HeaderActive]         = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);

    // Separator — blue-gray
    colors[ImGuiCol_Separator]            = ImVec4(0.75f, 0.82f, 0.90f, 0.25f);
    colors[ImGuiCol_SeparatorHovered]     = ImVec4(0.96f, 0.28f, 0.25f, 0.78f);
    colors[ImGuiCol_SeparatorActive]      = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);

    // Resize grip
    colors[ImGuiCol_ResizeGrip]           = ImVec4(0.96f, 0.28f, 0.25f, 0.25f);
    colors[ImGuiCol_ResizeGripHovered]    = ImVec4(0.96f, 0.28f, 0.25f, 0.67f);
    colors[ImGuiCol_ResizeGripActive]     = ImVec4(0.96f, 0.28f, 0.25f, 0.95f);

    // Tabs — navy + red accent
    colors[ImGuiCol_Tab]                  = ImVec4(0.14f, 0.18f, 0.27f, 0.86f);
    colors[ImGuiCol_TabHovered]           = ImVec4(0.96f, 0.28f, 0.25f, 0.90f);
    colors[ImGuiCol_TabSelected]          = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_TabSelectedOverline]  = ImVec4(0.75f, 0.82f, 0.90f, 1.00f);
    colors[ImGuiCol_TabDimmed]            = ImVec4(0.09f, 0.11f, 0.16f, 0.97f);
    colors[ImGuiCol_TabDimmedSelected]    = ImVec4(0.44f, 0.10f, 0.03f, 1.00f);

    // Plot colors — red accent, blue-gray secondary
    colors[ImGuiCol_PlotLines]            = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_PlotLinesHovered]     = ImVec4(0.75f, 0.82f, 0.90f, 1.00f);
    colors[ImGuiCol_PlotHistogram]        = ImVec4(0.96f, 0.28f, 0.25f, 1.00f);
    colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.75f, 0.82f, 0.90f, 1.00f);

    // Tables — navy palette
    colors[ImGuiCol_TableHeaderBg]        = ImVec4(0.14f, 0.18f, 0.27f, 1.00f);
    colors[ImGuiCol_TableBorderStrong]    = ImVec4(0.75f, 0.82f, 0.90f, 0.30f);
    colors[ImGuiCol_TableBorderLight]     = ImVec4(0.75f, 0.82f, 0.90f, 0.15f);
    colors[ImGuiCol_TableRowBg]           = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
    colors[ImGuiCol_TableRowBgAlt]        = ImVec4(0.75f, 0.82f, 0.90f, 0.03f);

    // Text — blue-gray
    colors[ImGuiCol_Text]                 = ImVec4(0.75f, 0.82f, 0.90f, 1.00f);
    colors[ImGuiCol_TextDisabled]         = ImVec4(0.55f, 0.64f, 0.74f, 0.60f);
    colors[ImGuiCol_TextSelectedBg]       = ImVec4(0.96f, 0.28f, 0.25f, 0.43f);

    // Drag/drop — red accent
    colors[ImGuiCol_DragDropTarget]       = ImVec4(0.96f, 0.28f, 0.25f, 0.90f);

    // Navigation highlight
    colors[ImGuiCol_NavCursor]            = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
    colors[ImGuiCol_NavWindowingHighlight]= ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
    colors[ImGuiCol_NavWindowingDimBg]    = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);

    // Modal window dim
    colors[ImGuiCol_ModalWindowDimBg]     = ImVec4(0.00f, 0.00f, 0.00f, 0.60f);

    // Style adjustments (from UISchema)
    auto brElem = S.drawElement("style", "border-radius");
    style.WindowRounding    = brElem.extraFloats.count("window")    ? brElem.extraFloats.at("window")    : 6.0f;
    style.ChildRounding     = brElem.extraFloats.count("child")     ? brElem.extraFloats.at("child")     : 4.0f;
    style.FrameRounding     = brElem.extraFloats.count("frame")     ? brElem.extraFloats.at("frame")     : 4.0f;
    style.PopupRounding     = brElem.extraFloats.count("popup")     ? brElem.extraFloats.at("popup")     : 4.0f;
    style.ScrollbarRounding = brElem.extraFloats.count("scrollbar") ? brElem.extraFloats.at("scrollbar") : 4.0f;
    style.GrabRounding      = brElem.extraFloats.count("grab")      ? brElem.extraFloats.at("grab")      : 4.0f;
    style.TabRounding       = brElem.extraFloats.count("tab")       ? brElem.extraFloats.at("tab")       : 4.0f;

    style.WindowTitleAlign  = ImVec2(0.5f, 0.5f);
    auto sElem = [&](const char* key, float fb) {
        float v = S.drawElement("style", key).size;
        return v >= 0 ? v : fb;
    };
    style.WindowPadding     = ImVec2(sElem("window-padding-x", 10.0f), sElem("window-padding-y", 10.0f));
    style.FramePadding      = ImVec2(sElem("frame-padding-x", 8.0f), sElem("frame-padding-y", 4.0f));
    style.ItemSpacing       = ImVec2(sElem("item-spacing-x", 8.0f), sElem("item-spacing-y", 6.0f));
    style.ItemInnerSpacing  = ImVec2(sElem("item-inner-spacing-x", 6.0f), sElem("item-inner-spacing-y", 4.0f));
    style.IndentSpacing     = sElem("indent-spacing", 20.0f);

    style.ScrollbarSize     = sElem("scrollbar-size", 6.0f);
    style.GrabMinSize       = sElem("grab-min-size", 8.0f);

    auto bwElem = S.drawElement("style", "border-width");
    style.WindowBorderSize  = bwElem.extraFloats.count("window") ? bwElem.extraFloats.at("window") : 1.0f;
    style.ChildBorderSize   = bwElem.extraFloats.count("child")  ? bwElem.extraFloats.at("child")  : 1.0f;
    style.PopupBorderSize   = bwElem.extraFloats.count("popup")  ? bwElem.extraFloats.at("popup")  : 1.0f;
    style.FrameBorderSize   = bwElem.extraFloats.count("frame")  ? bwElem.extraFloats.at("frame")  : 0.0f;
    style.TabBorderSize     = 0.0f;

    style.AntiAliasedLines  = true;
    style.AntiAliasedFill   = true;
    
    // Set matching acrylic theme
    SetCurrentAcrylicTheme(GetDragonXAcrylicTheme());
}

// ============================================================================
// Material Design Theme Functions
// ============================================================================

void SetDragonXMaterialTheme()
{
    material::ApplyColorThemeToImGui(material::GetDragonXColorTheme());
    SetCurrentAcrylicTheme(GetDragonXAcrylicTheme());
}

void SetDarkTheme()
{
    material::ApplyColorThemeToImGui(material::GetMaterialDarkTheme());
    SetCurrentAcrylicTheme(GetDarkAcrylicTheme());
}

void SetLightTheme()
{
    material::ApplyColorThemeToImGui(material::GetMaterialLightTheme());
    SetCurrentAcrylicTheme(GetLightAcrylicTheme());
}

bool SetThemeById(const std::string& themeId)
{
    if (themeId == "dragonx") {
        material::ApplyColorThemeToImGui(material::GetDragonXColorTheme());
        SetCurrentAcrylicTheme(GetDragonXAcrylicTheme());
        return true;
    } else if (themeId == "dark") {
        material::ApplyColorThemeToImGui(material::GetMaterialDarkTheme());
        SetCurrentAcrylicTheme(GetDarkAcrylicTheme());
        return true;
    } else if (themeId == "light") {
        material::ApplyColorThemeToImGui(material::GetMaterialLightTheme());
        SetCurrentAcrylicTheme(GetLightAcrylicTheme());
        return true;
    }
    
    // Theme not found - fall back to DragonX
    material::ApplyColorThemeToImGui(material::GetDragonXColorTheme());
    SetCurrentAcrylicTheme(GetDragonXAcrylicTheme());
    return false;
}

} // namespace ui
} // namespace dragonx
