using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Igdc.Scenes.UI.Controls;
using System.Collections.Generic;
using StoryEngine.Core.HistoryPoints;
using System.Linq;
using System.Diagnostics;

namespace Igdc.Scenes
{
    public class StoryScene : UiScene
    {
        private const int ButtonHeight = 20;

        public StoryScene(GraphicsDeviceManager manager, GraphicsDevice device, StoryPackage package) : base(manager, device, CreatePackage(package))
        {
        }

        private static UiPackage CreatePackage(StoryPackage package)
        {
            if (package.Story == null)
            {
                package.Story = IgdcGame.Current.GraphicsDevice
                    .ReadHistory(package.StoryLink)
                    .WithBinding("Добро", IgdcGame.Current.State.Player.Good, v => IgdcGame.Current.State.Player.Good = v)
                    .WithBinding("Зло", IgdcGame.Current.State.Player.Evil, v => IgdcGame.Current.State.Player.Evil = v);
            };
            var point = package.Story.GetCurrentPoint();
            var ui = new UiPackage();
            var controls = new List<ControlBase>
            {
                new Background { Color = Palette.Light }
            };

            var offset = 0;
            if (!point.Variants.Any())
            {
                controls.Add(new Button
                {
                    Font = IgdcGame.Current.BasicFont,
                    Texture = IgdcGame.Current.FlatTexture,
                    Content = "Далее",
                    Location = new Point(0, TargetHeight - offset - ButtonHeight),
                    Size = new Point(TargetWidth, ButtonHeight),
                    ActiveBackground = Palette.Blue2,
                    InactiveBackground = Palette.Blue1,
                    ActiveForeground = Palette.Light,
                    InactiveForeground = Palette.Tan2,
                    Command = () =>
                    {
                        if (point.NextPossiblePoints.Any())
                        {
                            package.Story.Step(-1);
                            IgdcGame.Current.Navigate<StoryScene>(package, false, false);
                            Debug.Write(package.Story.DebugInfo);
                        }
                        else
                        {
                            package.Callback?.Invoke();
                        }
                    }
                });
            }
            for (var i = point.Variants.Count - 1; i >= 0; i--)
            {
                var n = i;
                var variant = point.Variants[n];
                controls.Add(new Button
                {
                    Font = IgdcGame.Current.BasicFont,
                    Texture = IgdcGame.Current.FlatTexture,
                    Content = variant.Text,
                    Location = new Point(0, TargetHeight - offset - ButtonHeight),
                    Size = new Point(TargetWidth, ButtonHeight),
                    ActiveBackground = Palette.Blue2,
                    InactiveBackground = Palette.Blue1,
                    ActiveForeground = Palette.Light,
                    InactiveForeground = Palette.Tan2,
                    Command = () =>
                    {
                        package.Story.Step(n);
                        IgdcGame.Current.Navigate<StoryScene>(package, false, false);
                        Debug.Write(package.Story.DebugInfo);
                    }
                });
                offset += ButtonHeight;
            }

            switch (point)
            {
                case TextPoint t:
                    controls.Add(new TextBlock
                    {
                        Font = IgdcGame.Current.BasicFont,
                        BorderTexture = IgdcGame.Current.FlatTexture,
                        BorderColor = Palette.Dark,
                        Text = t.Text,
                        TextColor = Palette.Dark,
                        Size = new Point(TargetWidth, TargetHeight - offset)
                    });
                    break;
            }

            controls.Reverse();
            ui.Controls = controls.ToArray();
            return ui;
        }
    }
}