using Godot;
using Theriapolis.GodotHost.UI;

namespace Theriapolis.GodotHost.Scenes;

/// <summary>
/// M7.1 placeholder for the play screen. WorldGenProgressScreen swaps
/// here on success; M7.2 will replace this with the real PlayScreen
/// (walking character, chunk-streamed tactical view, HUD, save layer).
///
/// Reads <see cref="GameSession.Ctx"/> and <see cref="GameSession.PendingCharacter"/>
/// so the play-test confirms the M7.1 hand-off chain end-to-end:
/// Title → Wizard → CharacterAssembler → WorldGenProgress → here.
/// </summary>
public partial class PlayScreenStub : Control
{
    public override void _Ready()
    {
        Theme = CodexTheme.Build();
        SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);

        var bg = new Panel { MouseFilter = MouseFilterEnum.Ignore };
        AddChild(bg);
        bg.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);
        MoveChild(bg, 0);

        var center = new CenterContainer { MouseFilter = MouseFilterEnum.Ignore };
        AddChild(center);
        center.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);

        var col = new VBoxContainer { CustomMinimumSize = new Vector2(640, 0) };
        col.AddThemeConstantOverride("separation", 14);
        center.AddChild(col);

        var session = GameSession.From(this);

        col.AddChild(new Label
        {
            Text = "PLAYSCREEN STUB · M7.1",
            ThemeTypeVariation = "Eyebrow",
            HorizontalAlignment = HorizontalAlignment.Center,
        });
        col.AddChild(new Label
        {
            Text = "World generation complete.",
            ThemeTypeVariation = "H2",
            HorizontalAlignment = HorizontalAlignment.Center,
        });

        var ctx = session.Ctx;
        if (ctx is not null)
        {
            var w = ctx.World;
            col.AddChild(new Label
            {
                Text = $"Seed 0x{w.WorldSeed:X}  ·  rivers {w.Rivers.Count}  "
                     + $"roads {w.Roads.Count}  rails {w.Rails.Count}  "
                     + $"settlements {w.Settlements.Count}  bridges {w.Bridges.Count}",
                HorizontalAlignment = HorizontalAlignment.Center,
                AutowrapMode = TextServer.AutowrapMode.WordSmart,
            });
        }
        else
        {
            col.AddChild(new Label
            {
                Text = "(No WorldGenContext on session — this stub was entered out-of-band.)",
                HorizontalAlignment = HorizontalAlignment.Center,
                ThemeTypeVariation = "Eyebrow",
            });
        }

        var character = session.PendingCharacter;
        if (character is not null)
        {
            string hybridTag = character.Hybrid is not null ? "yes" : "no";
            col.AddChild(new Label
            {
                Text = $"Character: {session.PendingName}  ·  HP {character.MaxHp}  "
                     + $"·  class {character.ClassDef.Id}  ·  hybrid: {hybridTag}  "
                     + $"·  skills: {character.SkillProficiencies.Count}",
                HorizontalAlignment = HorizontalAlignment.Center,
                AutowrapMode = TextServer.AutowrapMode.WordSmart,
            });
        }
        else
        {
            col.AddChild(new Label
            {
                Text = "(No character attached — load path will fill this in once M7.3 ships.)",
                HorizontalAlignment = HorizontalAlignment.Center,
                ThemeTypeVariation = "Eyebrow",
            });
        }

        col.AddChild(new Label
        {
            Text = "PlayScreen with walking character + chunk-streamed tactical view lands in M7.2.",
            HorizontalAlignment = HorizontalAlignment.Center,
            AutowrapMode = TextServer.AutowrapMode.WordSmart,
        });

        var titleBtn = new Button
        {
            Text = "← Title",
            CustomMinimumSize = new Vector2(220, 44),
            SizeFlagsHorizontal = SizeFlags.ShrinkCenter,
        };
        titleBtn.Pressed += BackToTitle;
        col.AddChild(titleBtn);
    }

    private void BackToTitle()
    {
        var session = GameSession.From(this);
        session.ClearPending();
        session.Ctx = null;

        var parent = GetParent();
        if (parent is null) return;
        foreach (Node sibling in parent.GetChildren())
            if (sibling != this) sibling.QueueFree();
        parent.AddChild(new TitleScreen());
        QueueFree();
    }
}
