using Theriapolis.Core.Data;
using Theriapolis.Game.CodexUI.Core;
using Theriapolis.Game.CodexUI.Screens;
using Theriapolis.Game.CodexUI.Widgets;
using Theriapolis.Game.UI;

namespace Theriapolis.Game.CodexUI.Steps;

/// <summary>
/// Step IV — Background. Two-column grid of background cards: name + flavor
/// paragraph + named feature chip + sealed-skill chips.
/// </summary>
public static class StepBackground
{
    public static CodexWidget Build(CodexCharacterCreationScreen s, CodexAtlas atlas, CodexHoverPopover popover)
    {
        var col = new Column { Spacing = 14 };
        col.Add(StepCommon.PageIntro(
            "Folio IV — Of Histories",
            "Choose your Background",
            "Where the clade gives you body and the calling gives you craft, the background gives you a past — debts, contacts, scars, the way you sleep."));

        var grid = new Grid { Columns = 2 };
        foreach (var b in s.Backgrounds) grid.Add(BuildCard(s, atlas, popover, b));
        col.Add(grid);
        return col;
    }

    private static CodexWidget BuildCard(CodexCharacterCreationScreen s, CodexAtlas atlas, CodexHoverPopover popover, BackgroundDef b)
    {
        var content = new Column { Spacing = 8 };
        content.Add(new CodexLabel(b.Name, CodexFonts.DisplayMedium, CodexColors.Ink));
        if (!string.IsNullOrEmpty(b.Flavor))
            content.Add(new CodexLabel(b.Flavor, CodexFonts.SerifItalic, CodexColors.InkSoft));

        content.Add(new CodexLabel("FEATURE", CodexFonts.MonoTagSmall, CodexColors.InkMute));
        var featRow = new WrapRow();
        featRow.Add(new HoverableChip(atlas, popover, b.FeatureName, b.FeatureName, b.FeatureDescription, "FEATURE", ChipKind.BgFeature));
        content.Add(featRow);

        content.Add(new CodexLabel("SKILLS", CodexFonts.MonoTagSmall, CodexColors.InkMute));
        var skills = new WrapRow();
        foreach (var sk in b.SkillProficiencies)
            skills.Add(new HoverableChip(atlas, popover, CodexCopy.SkillName(sk), CodexCopy.SkillName(sk), CodexCopy.SkillDescription(sk), "BACKGROUND", ChipKind.SkillFromBg));
        content.Add(skills);

        return new CodexCard(atlas, content, s.Background == b,
            onClick: () => { s.Background = b; s.InvalidateLayout(); });
    }
}
