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

namespace Luski.GUI.MainScreen.UI.SettingsPanel;

public class Category : UserControl
{
    private Label Top;
    private static FontInteraction? fi;
    private Rectangle line;
    public string Name
    {
        get => Top.Text;
    }

    public Category(string Name)
    {
        if (fi is null)
        {
            fi = Globals.DefaultFont.Clone();
            fi.FontSize = FontSize.Bold;
        }
        Top = new(fi)
        {
            Location = new(5.ScaleInt(), 5.ScaleInt(), 0),
            Text = Name
        };
        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, 0);
        line = new()
        {
            Size = new(base.Size.X, 1.ScaleInt()),
            BackgroundColor = Color4.Gray,
            Location = new(0, base.Size.Y - 1.ScaleInt(), 0)
        };
        line.ForceDistanceUpdate(this);
        Controls.Add(line);
        Controls.Add(Top);
    }

    public void AddButton(CategoryButton cb)
    {
        Controls.Add(cb);
        int f = 5.ScaleInt();
        cb.Location = new (line.Location.X + f, line.Location.Y - f, 0);
        line.Location = new(line.Location.X, line.Location.Y + cb.Size.Y + f, 0);
        Size = new(Size.X, Size.Y + cb.Size.Y + f);
    }
    
    public void RemoveButton(CategoryButton cb)
    {
        int f = 5.ScaleInt();
        line.Location = new(line.Location.X, line.Location.Y - cb.Size.Y - f, 0);
        Controls.Remove(cb);
        Size = new(Size.X, Size.Y - cb.Size.Y + f);
        for (int i = 0; i < Controls.Length; i++)
        {
            ReportSizeUpdate(Controls[i]);
        }
        TryDraw();
    }
}