using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using Luski.GUI.MainScreen.UI.LuskiSettings.Core;
using Luski.GUI.MainScreen.UI.LuskiSettings.Pages.AdvancedSettings;
using Luski.GUI.MainScreen.UI.LuskiSettings.Pages.AppSettings;
using OpenTK.Mathematics;

namespace Luski.GUI.MainScreen.UI.LuskiSettings;

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

    public SettingsMenu()
    {
        base.BackgroundColor = new(34, 34, 34, 255);
        base.Size = Globals.ms.ClientSize;
        BehindName = Globals.ms.Title;
        Globals.ms.Title = $"Settings - Luski";
        fl = new()
        {
            BackgroundColor = new(20, 20, 20, 255),
            Size = new(307.ScaleInt(), base.Size.Y),
            Anchor = ObjectAnchor.Top | ObjectAnchor.Left | ObjectAnchor.Bottom
        };
        SettingsCategory AppSettings = new("App Settings", this);
        SettingsCategory AdvancedSettings = new("Advanced Settings", this);
        PageTab cb = AppSettings.AddPage(new General());
        if (LuskiExperiments.Settings.Theme.IsEnabled()) _ = AppSettings.AddPage(a=new Appearance());
        LuskiExperiments.Settings.Theme.EventToggled += b =>
        {
            if (b)
            {
                _ = AppSettings.AddPage(a=new Appearance());
                Globals.ms.ForceUpdate(new(Globals.ms.CS));
            }
            else AppSettings.RemovePage(a!);
            return Task.CompletedTask;
        };
        _ = AdvancedSettings.AddPage(new ExperimentSettings());
        _ = AdvancedSettings.AddPage(new Updater());
        fl.Controls.Add(AppSettings);
        fl.Controls.Add(AdvancedSettings);
        cb.ToggleSelected().Wait();
        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(),0),
            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 += _ =>
        {
            closebtn.BackgroundColor = Color4.Gray;
            return Task.CompletedTask;
        };
        closebtn.Clicked += ClosebtnOnClicked;
        closebtn.ForceDistanceUpdate(this);
        Controls.Add(closebtn);
    }
    
    private Task ClosebtnOnClicked(IRenderObject arg)
    {
        Globals.ms.Controls.Remove(this);
        Globals.ms.Title = BehindName;
        Globals.ms.DrawFrame();
        return Task.CompletedTask;
    }
}