using System.Numerics;
using Dalamud.Bindings.ImGui;
using Dalamud.Interface;
using Dalamud.Interface.Utility.Raii;
using HellionChat.Resources;
using HellionChat.Util;

namespace HellionChat.Ui;

internal sealed class SettingsOverview
{
    private readonly SettingsWindow _window;

    // Card order matches the Tabs index in SettingsWindow 1:1.
    private static (FontAwesomeIcon Icon, string Title, string Subtext)[] BuildCardDefs() =>
        [
            (
                FontAwesomeIcon.SlidersH,
                HellionStrings.Settings_Card_General_Title,
                HellionStrings.Settings_Card_General_Subtext
            ),
            (
                FontAwesomeIcon.Palette,
                HellionStrings.Settings_Card_Appearance_Title,
                HellionStrings.Settings_Card_Appearance_Subtext
            ),
            (
                FontAwesomeIcon.Comments,
                HellionStrings.Settings_Card_Chat_Title,
                HellionStrings.Settings_Card_Chat_Subtext
            ),
            (
                FontAwesomeIcon.WindowMaximize,
                HellionStrings.Settings_Card_Window_Title,
                HellionStrings.Settings_Card_Window_Subtext
            ),
            (
                FontAwesomeIcon.FolderTree,
                HellionStrings.Settings_Card_Tabs_Title,
                HellionStrings.Settings_Card_Tabs_Subtext
            ),
            (
                FontAwesomeIcon.Database,
                HellionStrings.Settings_Card_DataManagement_Title,
                HellionStrings.Settings_Card_DataManagement_Subtext
            ),
            (
                FontAwesomeIcon.InfoCircle,
                HellionStrings.Settings_Card_Information_Title,
                HellionStrings.Settings_Card_Information_Subtext
            ),
        ];

    public SettingsOverview(SettingsWindow window)
    {
        _window = window;
    }

    public void Draw()
    {
        var avail = ImGui.GetContentRegionAvail();
        var columns = avail.X >= 700f ? 3 : 2;
        var cardWidth = (avail.X - (columns - 1) * 8f) / columns;
        // 110f accommodates two-line subtexts; wrap width is matched in DrawCard.
        var cardHeight = 110f;

        // One draw-list lookup per frame instead of one per card.
        var drawList = ImGui.GetWindowDrawList();
        var cardDefs = BuildCardDefs();
        for (var i = 0; i < cardDefs.Length; i++)
        {
            var (icon, title, subtext) = cardDefs[i];
            DrawCard(i, icon, title, subtext, cardWidth, cardHeight, drawList);

            if ((i + 1) % columns != 0 && i != cardDefs.Length - 1)
                ImGui.SameLine();
        }
    }

    private void DrawCard(
        int index,
        FontAwesomeIcon icon,
        string title,
        string subtext,
        float w,
        float h,
        ImDrawListPtr drawList
    )
    {
        // BeginGroup makes the card a single layout item so SameLine works
        // in the caller loop -- without it ImGui tracks each child separately.
        ImGui.BeginGroup();

        var cursorBefore = ImGui.GetCursorScreenPos();
        var clicked = ImGui.InvisibleButton($"##settings-card-{index}", new Vector2(w, h));
        var hovered = ImGui.IsItemHovered();
        var bgColor = hovered ? 0xFF22303Fu : 0xFF1A2538u;

        drawList.AddRectFilled(cursorBefore, cursorBefore + new Vector2(w, h), bgColor, 4f);

        var iconPos = cursorBefore + new Vector2(16f, 12f);
        var titlePos = cursorBefore + new Vector2(16f, 40f);
        var subtextPos = cursorBefore + new Vector2(16f, 62f);

        var titleColor = ColourUtil.RgbaToAbgr(0xE6F4F1FFu);
        var subtextColor = ColourUtil.RgbaToAbgr(0x8FA3B5FFu);

        using (_window.Plugin.FontManager.FontAwesome.Push())
        {
            drawList.AddText(iconPos, titleColor, icon.ToIconString());
        }

        drawList.AddText(titlePos, titleColor, title);

        // Subtext wraps at card inner width (16px padding each side) via DrawList
        // to avoid expanding the group bounds and breaking SameLine in the card row.
        var subtextWrapWidth = w - 32f;
        drawList.AddText(
            ImGui.GetFont(),
            ImGui.GetFontSize(),
            subtextPos,
            subtextColor,
            subtext,
            subtextWrapWidth
        );

        ImGui.EndGroup();

        if (clicked)
            _window.OpenSection(index);
    }
}
