using System.Reflection;
using GraphicsManager.Enums;
using Luski.Classes;
using Luski.GUI.MainScreen.UI.LuskiControls;
using Luski.GUI.MainScreen.UI.LuskiSettings.Core;

namespace Luski.GUI.MainScreen.UI.LuskiSettings.Pages.AdvancedSettings;

public class Updater : PageFlow
{
    public Updater()
    {
        PageName = "Updater Config";
        TextBox t;
        Controls.Add(t =new TextBox()
        {
            Text = Globals.UpdaterSettings.Updater!,
            WatermarkText = "Updater File",
            TextureDisplay = TextureDisplay.Center,
            Size = new(base.Size.X, 34.ScaleInt()),
            TextLocation = TextLocation.LineCenter,
            AllowMultiLine = false
        });
        foreach (PropertyInfo prop in typeof(UpdaterSettings).GetProperties())
        {
            object PropVal = prop.GetValue(Globals.UpdaterSettings)!;
            Type PropType = prop.PropertyType;
            
            if (PropType.IsEnum)
            {
                IEnumerable<Enum> values = Enum.GetValues(PropType).Cast<Enum>();
                foreach (var val in values)
                {
                    try
                    {
                        Globals.AddBool(this, PropType, val, ((Enum)PropVal).HasFlag(val), bb =>
                        {
                            long va = Convert.ToInt64(val);
                            long v = Convert.ToInt64(PropVal);
                            if (bb)
                            {
                                object e = Enum.Parse(PropType, (v + va).ToString());
                                PropVal = e;
                                prop.SetValue(Globals.Settings, e);
                            }
                            else
                            {
                                var e = Enum.Parse(PropType, (v - va).ToString());
                                PropVal = e;
                                prop.SetValue(Globals.UpdaterSettings, e);
                            }
                            Globals.UpdaterSettings.SaveSettings(Path.Combine(Globals.LuskiPath, "UpdaterSettings.json"), UpdaterSettingsContext.Default.UpdaterSettings);
                        });
                    }
                    catch
                    {
                        //ignore
                    }
                }
            }
            if (PropType.FullName == typeof(bool).FullName)
            {
                try
                {
                    Globals.AddBool(this, prop, (bool)PropVal, b =>
                    {
                        prop.SetValue(Globals.UpdaterSettings, b);
                        Globals.UpdaterSettings.SaveSettings(Path.Combine(Globals.LuskiPath, "UpdaterSettings.json"), UpdaterSettingsContext.Default.UpdaterSettings);
                    });
                }
                catch
                {
                    // ignored
                }
            }
        }
        
        t.ForceDistanceUpdate(this);
        t.KeyPress += _ =>
        {
            Globals.UpdaterSettings.Updater = t.Text;
            Globals.UpdaterSettings.SaveSettings(Path.Combine(Globals.LuskiPath, "UpdaterSettings.json"), UpdaterSettingsContext.Default.UpdaterSettings);
            return Task.CompletedTask;
        };
        WindowLoaded += _ =>
        {
            Globals.ms.ForceUpdate(new(Globals.ms.CS));
            return Task.CompletedTask;
        };
    }
}