using GraphicsManager.Enums;
using GraphicsManager.Objects;
using GraphicsManager.Objects.Core;
using Luski.Classes;
using Luski.GUI.MainScreen.UI.LuskiControls;
using OpenTK.Mathematics;

namespace Luski.GUI.MainScreen.UI.SettingsPanel;

public class ExperimentGUI : UserControl
{
    private Label Top;
    public Rectangle line;
    public DropDown<ExperimentDropButton> dd;
    private static Texture? TopOpen, BottomOpen;
    private ExperimentSelectorInfo? currentEnabled;

    private static ExperimentSelectorInfo DisabledESI = new()
    {
        Name = "Disabled"
    };

    public string Name
    {
        get => Top.Text;
    }

    public ExperimentGUI(ExperimentInfo ei)
    {
        TextureDisplay = TextureDisplay.Center;
        if (TopOpen is null)
        {
            TopOpen = Globals.ms.TextureManager.GetTextureResource("RoundedRectangleTop.png");
        }
        if (BottomOpen is null)
        {
            BottomOpen = Globals.ms.TextureManager.GetTextureResource("RoundedRectangleBottom.png");
        }
        Top = new(Globals.DefaultFont)
        {
            Location = new(5.ScaleInt(), 5.ScaleInt(), 0),
            Text = ei.DisplayName
        };
        Label n = new(Globals.MessageFont)
        {
            Text = ei.Name,
            Location = new(Top.Location.X, Top.Location.Y + Top.Size.Y + Top.Location.Y, 0),
            Color = Color4.Gray
        };
        Controls.Add(n);
        BackgroundColor = new(255, 255, 255, 0);
        ExperimentDropButton Disabled = new(DisabledESI)
        {
            LoadDisplay = () =>
            {
                Label ll = new Label(Globals.DefaultFont)
                {
                    Text = DisabledESI.Name,
                    Color = Color4.DarkGray,
                    IgnoreHover = true
                };
                ll.Location = new(10.ScaleInt(),
                    ((dd!.Size.Y - ll.Size.Y) / 2)
                    , 0);
                dd.Controls.Add(ll);
            },
            Size = new(297.ScaleInt(), 40.ScaleInt())
        };
        dd = new(CategoryButton.seltec!, new Rectangle()
        {
            Size = new(1.ScaleInt()),
            BackgroundColor = Color4.Gray
        }, Disabled)
        {
            Size = new(40.ScaleInt()),
            Location = new(Top.Location.X, n.Location.Y + n.Size.Y + Top.Location.Y, 0),
            BackgroundColor = new(40, 40, 40, 255),
            Anchor = ObjectAnchor.Right | ObjectAnchor.Left,
            DropDownParentOverride = this
        };
        dd.DropDownContainer.Textures.Add(BottomOpen);
        dd.DropDownContainer.TextureDisplay = TextureDisplay.Center;
        dd.DropDownContainer.Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context];
        dd.DropDownContainer.BackgroundColor = dd.BackgroundColor;
        dd.OpenStatusChanged += b =>
        {
            BlockDraw = true;
            Size = new(base.Size.X, base.Size.Y + ( b ? dd.DropDownContainer.Size.Y : -1 * dd.DropDownContainer.Size.Y));
            line!.Location = new(line.Location.X,
                line.Location.Y + (b ? dd.DropDownContainer.Size.Y : -1 * dd.DropDownContainer.Size.Y), 0);
            if (b)
            {
                dd.Textures[0] = TopOpen;
            }
            else
            {
                dd.Textures[0] = CategoryButton.seltec!;
            }

            BlockDraw = false;
            return Task.CompletedTask;
        };
        int i = -1;
        foreach (ExperimentSelectorInfo o in ei.Options)
        {
            i++;
            ExperimentDropButton oo = new ExperimentDropButton(o)
            {
                Tag = i,
                LoadDisplay = () =>
                {
                    Label ll = new Label(Globals.DefaultFont)
                    {
                        Text = o.Name,
                        Color = Color4.DarkGray,
                        IgnoreHover = true
                    };
                    ll.Location = new(10.ScaleInt(),
                        ((dd.Size.Y - ll.Size.Y) / 2)
                        , 0);
                    dd.Controls.Add(ll);
                }
            };
            dd.AddOption(oo);
            if (o.IsEnabled())
            {
                dd.SetSelected(oo);
                currentEnabled = o;
            }
        }
        dd.OptionSelected += DdOnOptionSelected;
        Controls.Add(dd);
        base.Size = new(Globals.ms.ClientSize.X -  307.ScaleInt() - 80.ScaleInt(), 15.ScaleInt() + dd.Size.Y + dd.Location.Y );
        dd.Size = new(base.Size.X - Top.Location.X - Top.Location.X, dd.Size.Y);
        dd.ForceDistanceUpdate(this);
        
        line = new()
        {
            Size = new(base.Size.X - 2, 1.ScaleInt()),
            BackgroundColor = Color4.Gray,
            Location = new(1, base.Size.Y - 1.ScaleInt(), 0),
            Anchor = ObjectAnchor.Left | ObjectAnchor.Right | ObjectAnchor.Bottom
        };
        line.ForceDistanceUpdate(this);
        Controls.Add(line);
        Controls.Add(Top);
    }

    private Task DdOnOptionSelected(ExperimentDropButton arg)
    {
        if (arg.ESI == DisabledESI)
        {
            currentEnabled!.Toggle();
            currentEnabled = null;
        }
        else
        {
            currentEnabled = arg.ESI;
            arg.ESI.Toggle();
        }
        return Task.CompletedTask;
    }
}