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

namespace Luski.GUI.MainScreen.UI.LuskiControls.SettingsMenuBase;

public class SettingsMenu : UserControl
{
    private string BehindName;
    public PageTab? Selected;
    public FlowLayout fl;

    public SettingsMenu(string Name)
    {
        base.BackgroundColor = new(34, 34, 34, 255);
        base.Size = Globals.ms.ClientSize;
        BehindName = Globals.ms.Title;
        Globals.ms.Title = Name;
        fl = new()
        {
            BackgroundColor = new(20, 20, 20, 255),
            Size = new(307.ScaleInt(), base.Size.Y),
            Anchor = ObjectAnchor.Top | ObjectAnchor.Left | ObjectAnchor.Bottom
        };
        Anchor = ObjectAnchor.All;
        Controls.Add(fl);
        Rectangle closebtn = new(Globals.ms.TextureManager.GetTextureResource("close.png"))
        {
            Location = new(Globals.ms.ClientSize.X - 40.ScaleInt(), 8.ScaleInt()),
            Size = new(32.ScaleInt()),
            Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context],
            BackgroundColor = Color4.Gray,
            Anchor = ObjectAnchor.Top | ObjectAnchor.Right
        };
        closebtn.MouseEnter += _ =>
        {
            closebtn.BackgroundColor = Color4.White;
            return Task.CompletedTask;
        };
        closebtn.MouseLeave += _ =>
        {
            if (bl) BlockDraw = true;
            closebtn.BackgroundColor = Color4.Gray;
            if (bl)
            {
                BlockDraw = false;
                bl = false;
            }
            return Task.CompletedTask;
        };
        closebtn.Clicked += ClosebtnOnClicked;
        closebtn.ForceDistanceUpdate(this);
        Controls.Add(closebtn);
    }

    private bool bl;
    
    private Task ClosebtnOnClicked(IRenderObject arg)
    {
        bl = true;
        Globals.ms.Controls.Remove(this, false);
        Globals.ms.Title = BehindName;
        Globals.ms.DrawFrame();
        return Task.CompletedTask;
    }
}