#include "tokens.h"

#if __has_include("implot.h")
#include "implot.h"
#define FN_HAS_IMPLOT 1
#endif

namespace fn_tokens {

void apply_dark_theme() {
    ImGuiStyle& s = ImGui::GetStyle();

    // ---------- Colors ----------
    auto& c = s.Colors;
    c[ImGuiCol_WindowBg]             = colors::bg;
    c[ImGuiCol_ChildBg]              = colors::surface;
    c[ImGuiCol_PopupBg]              = colors::surface;
    c[ImGuiCol_MenuBarBg]            = colors::surface;
    c[ImGuiCol_FrameBg]              = colors::surface;
    c[ImGuiCol_FrameBgHovered]       = colors::surface_hover;
    c[ImGuiCol_FrameBgActive]        = colors::surface_active;

    c[ImGuiCol_TitleBg]              = colors::bg;
    c[ImGuiCol_TitleBgActive]        = colors::surface;
    c[ImGuiCol_TitleBgCollapsed]     = colors::bg;

    c[ImGuiCol_Border]               = colors::border;
    c[ImGuiCol_BorderShadow]         = ImVec4(0, 0, 0, 0);

    c[ImGuiCol_Text]                 = colors::text;
    c[ImGuiCol_TextDisabled]         = colors::text_dim;
    c[ImGuiCol_TextSelectedBg]       = ImVec4(colors::primary.x, colors::primary.y, colors::primary.z, 0.35f);

    c[ImGuiCol_Button]               = colors::primary;
    c[ImGuiCol_ButtonHovered]        = colors::primary_hover;
    c[ImGuiCol_ButtonActive]         = colors::primary_active;

    c[ImGuiCol_CheckMark]            = colors::primary_light;
    c[ImGuiCol_SliderGrab]           = colors::primary;
    c[ImGuiCol_SliderGrabActive]     = colors::primary_hover;

    c[ImGuiCol_Header]               = colors::surface_hover;
    c[ImGuiCol_HeaderHovered]        = colors::primary;
    c[ImGuiCol_HeaderActive]         = colors::primary_hover;

    c[ImGuiCol_Tab]                  = colors::surface;
    c[ImGuiCol_TabHovered]           = colors::primary_hover;
    c[ImGuiCol_TabActive]            = colors::primary;
    c[ImGuiCol_TabUnfocused]         = colors::bg;
    c[ImGuiCol_TabUnfocusedActive]   = colors::surface_hover;

    c[ImGuiCol_Separator]            = colors::border;
    c[ImGuiCol_SeparatorHovered]     = colors::primary_light;
    c[ImGuiCol_SeparatorActive]      = colors::primary;

    c[ImGuiCol_ResizeGrip]           = ImVec4(colors::primary.x, colors::primary.y, colors::primary.z, 0.25f);
    c[ImGuiCol_ResizeGripHovered]    = colors::primary_hover;
    c[ImGuiCol_ResizeGripActive]     = colors::primary_active;

    c[ImGuiCol_ScrollbarBg]          = colors::bg;
    c[ImGuiCol_ScrollbarGrab]        = colors::surface_hover;
    c[ImGuiCol_ScrollbarGrabHovered] = colors::surface_active;
    c[ImGuiCol_ScrollbarGrabActive]  = colors::border_strong;

    c[ImGuiCol_TableHeaderBg]        = colors::surface_hover;
    c[ImGuiCol_TableBorderLight]     = colors::border;
    c[ImGuiCol_TableBorderStrong]    = colors::border;
    c[ImGuiCol_TableRowBg]           = ImVec4(0, 0, 0, 0);
    c[ImGuiCol_TableRowBgAlt]        = ImVec4(colors::surface.x, colors::surface.y, colors::surface.z, 0.40f);

    c[ImGuiCol_DockingPreview]       = ImVec4(colors::primary.x, colors::primary.y, colors::primary.z, 0.60f);
    c[ImGuiCol_DockingEmptyBg]       = colors::bg;

    c[ImGuiCol_PlotLines]            = colors::primary_light;
    c[ImGuiCol_PlotLinesHovered]     = colors::primary;
    c[ImGuiCol_PlotHistogram]        = colors::primary_light;
    c[ImGuiCol_PlotHistogramHovered] = colors::primary;

    c[ImGuiCol_DragDropTarget]       = colors::primary_light;
    c[ImGuiCol_NavHighlight]         = colors::primary_light;
    c[ImGuiCol_NavWindowingHighlight]= ImVec4(1, 1, 1, 0.70f);
    c[ImGuiCol_NavWindowingDimBg]    = ImVec4(0, 0, 0, 0.40f);
    c[ImGuiCol_ModalWindowDimBg]     = ImVec4(0, 0, 0, 0.55f);

    // ---------- Radius ----------
    s.WindowRounding    = radius::md;
    s.ChildRounding     = radius::md;
    s.PopupRounding     = radius::md;
    s.FrameRounding     = radius::md;
    s.GrabRounding      = radius::sm;
    s.ScrollbarRounding = radius::md;
    s.TabRounding       = radius::sm;

    // ---------- Spacing & padding ----------
    s.WindowPadding    = ImVec2(spacing::md, spacing::md);
    s.FramePadding     = ImVec2(spacing::sm, spacing::xs + 2.0f);
    s.CellPadding      = ImVec2(spacing::sm, spacing::xs);
    s.ItemSpacing      = ImVec2(spacing::sm, spacing::sm);
    s.ItemInnerSpacing = ImVec2(spacing::xs, spacing::xs);
    s.IndentSpacing    = spacing::lg;
    s.ScrollbarSize    = 14.0f;
    s.GrabMinSize      = 12.0f;

    // ---------- Borders ----------
    s.WindowBorderSize = 1.0f;
    s.ChildBorderSize  = 1.0f;
    s.PopupBorderSize  = 1.0f;
    s.FrameBorderSize  = 1.0f; // Borde sutil en inputs/combos/sliders para que destaquen sobre el bg
    s.TabBorderSize    = 0.0f;

    // ---------- Viewports (multi-OS-window) ----------
    // Cuando ImGuiConfigFlags_ViewportsEnable esta activo las ventanas pueden
    // salirse del monitor principal; en ese caso WindowRounding debe ser 0
    // y alpha 1.0 para que los compositors no muestren esquinas raras.
    // app_base gestiona este caso; aqui dejamos los valores para single-viewport.

    // ---------- ImPlot ----------
#ifdef FN_HAS_IMPLOT
    if (ImPlot::GetCurrentContext() != nullptr) {
        ImPlotStyle& p = ImPlot::GetStyle();
        p.Colors[ImPlotCol_FrameBg]      = colors::surface;
        p.Colors[ImPlotCol_PlotBg]       = colors::bg;
        p.Colors[ImPlotCol_PlotBorder]   = colors::border;
        p.Colors[ImPlotCol_LegendBg]     = colors::surface;
        p.Colors[ImPlotCol_LegendBorder] = colors::border;
        p.Colors[ImPlotCol_LegendText]   = colors::text;
        p.Colors[ImPlotCol_TitleText]    = colors::text;
        p.Colors[ImPlotCol_InlayText]    = colors::text_muted;
        p.Colors[ImPlotCol_AxisText]     = colors::text_muted;
        p.Colors[ImPlotCol_AxisGrid]     = colors::border;
        p.Colors[ImPlotCol_AxisTick]     = colors::border;
        p.Colors[ImPlotCol_AxisBg]       = ImVec4(0, 0, 0, 0);
        p.Colors[ImPlotCol_Selection]    = colors::primary_light;
        p.Colors[ImPlotCol_Crosshairs]   = colors::text_muted;
        p.PlotPadding     = ImVec2(spacing::sm, spacing::sm);
        p.LabelPadding    = ImVec2(spacing::xs, spacing::xs);
        p.LegendPadding   = ImVec2(spacing::sm, spacing::sm);
    }
#endif
}

} // namespace fn_tokens
