using Godot;
using ReactorMaintenance.Godot.Controls;
using ReactorMaintenance.Godot.Data;

namespace ReactorMaintenance.Godot.Screens;

public partial class LevelScreen : ScreenBase
{
    public void Configure(AppController app, CampaignLevel level, int levelNumber, int levelCount)
    {
        m_App = app;
        m_Level = level;

        var body = CreatePage(string.Empty);
        var header = new LevelHeader();
        body.AddChild(header);
        header.SetLevel(level, levelNumber, levelCount, "Stable");

        var flavor = CreateBodyText(level.FlavorText);
        flavor.HorizontalAlignment = HorizontalAlignment.Left;
        body.AddChild(flavor);

        var split = new HSplitContainer { SizeFlagsVertical = SizeFlags.ExpandFill };
        body.AddChild(split);

        split.AddChild(CreateGridPlaceholder());
        split.AddChild(CreateSidePanel(level));

        body.AddChild(new InventoryStrip());
        body.AddChild(CreateActionBar());

        m_OverlayLayer = new();
        AddChild(m_OverlayLayer);
    }

    private static PanelContainer CreateGridPlaceholder()
    {
        var panel = new PanelContainer { CustomMinimumSize = new(560, 360), SizeFlagsHorizontal = SizeFlags.ExpandFill, SizeFlagsVertical = SizeFlags.ExpandFill };
        var label = new Label {
            Text = "Level Grid Placeholder\nRobot marker, terrain, props, hazards, and underground overlays will render here.",
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            AutowrapMode = TextServer.AutowrapMode.WordSmart
        };
        panel.AddChild(label);
        return panel;
    }

    private static VBoxContainer CreateSidePanel(CampaignLevel level)
    {
        var sidePanel = new VBoxContainer { CustomMinimumSize = new(300, 0) };
        sidePanel.AddChild(new Label { Text = "Inspector" });
        sidePanel.AddChild(new CellInspector());
        sidePanel.AddChild(new ForecastList());
        sidePanel.AddChild(new Label { Text = $"Level JSON: {level.LevelPath}", AutowrapMode = TextServer.AutowrapMode.WordSmart });
        return sidePanel;
    }

    private HBoxContainer CreateActionBar()
    {
        var actions = new HBoxContainer();
        actions.AddChild(CreateButton("Move", () => { }, "Quick action placeholder"));
        actions.AddChild(CreateButton("Interact", () => { }, "Lengthy action placeholder"));
        actions.AddChild(CreateButton("Repair", () => { }, "Lengthy action placeholder"));
        actions.AddChild(CreateButton("Trigger Win", ShowWinOverlay));
        actions.AddChild(CreateButton("Trigger Lose", ShowLoseOverlay));
        actions.AddChild(CreateButton("Main Menu", () => m_App?.ShowMainMenu()));
        return actions;
    }

    private void ShowLoseOverlay()
    {
        if (m_OutcomeVisible || m_App is null || m_Level is null || m_OverlayLayer is null)
            return;

        m_OutcomeVisible = true;
        var overlay = new LoseOverlay();
        overlay.Configure(m_Level, m_App.RetryCurrentLevel, m_App.ShowGameOver, m_App.ShowMainMenu);
        AddCenteredOverlay(overlay);
    }

    private void ShowWinOverlay()
    {
        if (m_OutcomeVisible || m_App is null || m_Level is null || m_OverlayLayer is null)
            return;

        m_OutcomeVisible = true;
        var overlay = new WinOverlay();
        overlay.Configure(m_Level, m_App.CompleteCurrentLevel, m_App.ShowMainMenu);
        AddCenteredOverlay(overlay);
    }

    private void AddCenteredOverlay(Control overlay)
    {
        var center = new CenterContainer {
            AnchorRight = 1,
            AnchorBottom = 1
        };
        center.AddChild(overlay);
        m_OverlayLayer?.AddChild(center);
    }

    private AppController? m_App;
    private CampaignLevel? m_Level;
    private bool m_OutcomeVisible;
    private CanvasLayer? m_OverlayLayer;
}