using System.Reflection;
using GraphicsManager.Enums;
using Luski.Classes;
using Luski.Classes.Attribs;
using Luski.Enums.Strings;
using Luski.GUI.MainScreen.UI.LuskiControls.SettingsMenuBase;
using Luski.GUI.MainScreen.UI.LuskiControls.SettingsMenuBase.Core;
using Luski.GUI.MainScreen.UI.LuskiSettings.Pages.AppSettings;
using Luski.Shared.GlobalAttributes;

namespace Luski.GUI.MainScreen.UI.LuskiSettings;

public class GlobalSettingsMenu : SettingsMenu
{
    private Dictionary<string, SettingsCategory<GlobalSettingsMenu>> categories = new();
    private PageTab? First;

    public GlobalSettingsMenu()
    :base("Settings - Luski")
    {
        LoadPages<Settings>();
        LoadPages<UpdaterSettings>();

        for (int i = 1; i < fl.Controls.Length; i++) // Fix the pos because why not and better than redrawing window
        {
            fl.Controls[i].SetLocation(fl.Controls[i].Location.X, fl.Controls[i-1].Size.Y + fl.Controls[i-1].Location.Y);
        }
        
        First!.ToggleSelected().Wait();
        Anchor = ObjectAnchor.All; 
    }

    private void LoadPages<TFile>()
    {
        foreach (PropertyInfo prop in typeof(TFile).GetProperties())
        {
            if (!prop.TryGetAnyAttribute(out SettingInfoAttribute? p)) continue;
            if (!categories.ContainsKey(p.Group))
            {
                categories.Add(p.Group, new SettingsCategory<GlobalSettingsMenu>(p.Group, this));
                fl.Controls.Add(categories[p.Group]);
            }

            if (!categories[p.Group].HasPage(SettingsPage.Pages[p.Page].Name))
            {
                PageTab tab = categories[p.Group].AddPage(p.CreatePage());
                if (First is null)
                {
                    First = tab;
                }
            }
        }
    }
}