using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Ui;
using MLEM.Ui.Elements;
using TouchyTickets.Attractions;

namespace TouchyTickets; 

public class Tutorial {

    private static readonly Step[] Steps = {
        // introduction
        new(_ => true, "Tutorial1"),
        new(g => g.Tickets >= AttractionType.Carousel.InitialPrice, "Tutorial2"),
        new(g => g.DrawMap && g.Map.PlacingAttraction?.Type == AttractionType.Carousel, "Tutorial3"),
        new(g => g.Map.GetAttractionAmount(AttractionType.Carousel) > 0, "Tutorial4", "Tutorial5", "Tutorial6"),
        // modifier tutorial
        new(g => g.Map.GetAttractionAmount(null) >= 3, "Tutorial11"),
        new(g => g.Map.PlacingModifier != null, "Tutorial12"),
        new(g => g.Map.GetModifierAmount(null) > 0, "Tutorial13"),
        // star tutorial
        new(g => g.Tickets >= g.GetStarPrice(), "Tutorial7", "Tutorial8"),
        new(g => g.Stars > 0, "Tutorial9", "Tutorial10")
    };
    public int CurrentStep;
    private int currentStepMessage;

    public void Update(GameImpl game) {
        // stop if the tutorial is finished
        if (this.CurrentStep >= Tutorial.Steps.Length)
            return;
        // don't do anything while a tutorial box is already displaying
        if (game.UiSystem.Get("TutorialBox") != null)
            return;

        var step = Tutorial.Steps[this.CurrentStep];
        if (step.ShouldContinue(game)) {
            var infoBox = new Group(Anchor.TopLeft, Vector2.One, false) {
                OnDrawn = (e2, _, batch, _) => batch.Draw(batch.GetBlankTexture(), e2.DisplayArea, Color.Black * 0.35F)
            };
            var panel = infoBox.AddChild(new Panel(Anchor.Center, new Vector2(0.8F), Vector2.Zero, true));
            panel.AddChild(new Paragraph(Anchor.AutoLeft, 1, Localization.Get(step.Content[this.currentStepMessage])));
            panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 30), Localization.Get("Okay")) {
                OnPressed = e2 => {
                    game.UiSystem.Remove(e2.Root.Name);
                    this.currentStepMessage++;
                    if (this.currentStepMessage >= step.Content.Length) {
                        this.currentStepMessage = 0;
                        this.CurrentStep++;
                    }
                }
            });
            game.UiSystem.Add("TutorialBox", infoBox);
        }
    }

    private class Step {

        public readonly ContinueDelegate ShouldContinue;
        public readonly string[] Content;

        public Step(ContinueDelegate shouldContinue, params string[] content) {
            this.ShouldContinue = shouldContinue;
            this.Content = content;
        }

        public delegate bool ContinueDelegate(GameImpl game);

    }

}