using GraphicsManager.Enums;
using GraphicsManager.Objects;
using GraphicsManager.Objects.Core;
using OpenTK.Mathematics;

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

public class SettingsCategory<TSettingsMenu> : UserControl where TSettingsMenu : SettingsMenu
{
    private Label Top;
    private static FontInteraction? fi;
    private Rectangle line;
    public string Name
    {
        get => Top.Text;
    }

    private TSettingsMenu ss;

    public SettingsCategory(string Name, TSettingsMenu SS)
    {
        ss = SS;
        if (fi is null)
        {
            fi = Globals.DefaultFont.Clone();
            fi.FontSize = FontSize.Bold;
        }
        Top = new(fi)
        {
            Location = new(5.ScaleInt(), 5.ScaleInt()),
            Text = Name
        };
        base.BackgroundColor = new(255, 255, 255, 0);
        base.Size = new(307.ScaleInt(), 20.ScaleInt() + Top.Size.Y);
        Top.Location = new((base.Size.X - Top.Size.X) / 2, Top.Location.Y);
        line = new()
        {
            Size = new(base.Size.X, 1.ScaleInt()),
            BackgroundColor = Color4.Gray,
            Location = new(0, base.Size.Y - 1.ScaleInt())
        };
        line.ForceDistanceUpdate(this);
        Controls.Add(line);
        Controls.Add(Top);
    }
    
    public PageTab AddPage<TPage>(TPage Page) where TPage : ISettingsPage
    {
        Page.Location = new(ss.fl.Size.X + 40.ScaleInt(), 0);
        Page.Size = new(Globals.ms.CS.X - ss.fl.Size.X - 80.ScaleInt(), Globals.ms.CS.Y);
        Page.AllowHoverFromBehind = true;
        Page.Anchor = ObjectAnchor.All;
        Page.BackgroundColor = ss.BackgroundColor;
        pagest.Add(typeof(TPage));
        pages.Add(Page.PageName);
        ss.Controls.Add(Page);
        if (Page is PageFlow pbf) pbf.HScrollPixels = Globals.Settings.PerScrollPixels;
        PageTab cb = new(Page.PageName, ss)
        {
            Page = Page
        };
        Controls.Add(cb);
        int f = 5.ScaleInt();
        cb.Location = new (line.Location.X + f, line.Location.Y - f);
        line.Location = new(line.Location.X, line.Location.Y + cb.Size.Y + f);
        Size = new(Size.X, Size.Y + cb.Size.Y + f);
        Page.Tag = cb;
        return cb;
    }

    private List<string> pages = new();
    private List<Type> pagest = new();

    public bool HasPage(string name)
    {
        return pages.Contains(name);
    }
    
    public bool HasPage(Type name)
    {
        return pagest.Contains(name);
    }
    
    public void RemovePage<TPage>(TPage Page) where TPage : ISettingsPage
    {
        if (Page.Tag is PageTab cb)
        {
            int f = 5.ScaleInt();
            line.Location = new(line.Location.X, line.Location.Y - cb.Size.Y - f);
            Size = new(Size.X, Size.Y - cb.Size.Y - f);
            Controls.Remove(cb);
        }
        ss.Controls.Remove(Page);
    }
}