using System.ComponentModel;
using System.Text.Json.Serialization;
using GraphicsManager.Objects;

namespace Luski.Classes;

public class Settings
{
    [JsonInclude]
    [JsonPropertyName("scale")]
    public double? Scale { get; set; } = null;
    [JsonInclude]
    [JsonPropertyName("perscrollpixels")]
    public uint PerScrollPixels { get; set; } = 20;
    [JsonInclude]
    [JsonPropertyName("multithreadpercent")]
    public uint MultiThreadPercent { get; set; } = 50;
    [JsonInclude]
    [JsonPropertyName("loadperchannel")]
    public int LoadPerChannel { get; set; } = 50;
    [JsonInclude]
    [JsonPropertyName("theme")]
    public string Theme { get; set; } = "Dark";
    [JsonInclude]
    [JsonPropertyName("experiments")]
    public List<ExperimentJson> Experiments { get; set; } = Array.Empty<ExperimentJson>().ToList();
    [JsonInclude]
    [JsonPropertyName("default_display")]
    public int Display { get; set; } = 0;
    /// <summary>
    /// Sets the log value for the console. Default value of -25 to enable all logs by default except for DrawFrames and InfoOpenGL even if new ones are added.
    /// </summary>
    [JsonInclude]
    [Description("Console Logs")]
    [JsonPropertyName("log")]
    public ConsoleLog Logs
    {
        get
        {
            return _Logs;
        }
        set
        {
            _Logs = value;
            if (Globals.ms is not null)
            {
                Globals.ms.LogFrames = (_Logs & ConsoleLog.DrawFrames) == ConsoleLog.DrawFrames;
                Globals.ms.ShowMissingChar = (_Logs & ConsoleLog.ShowMissingChar) == ConsoleLog.ShowMissingChar;
            }
        }
    }

    [JsonInclude]
    [Description("Scale Fonts")]
    [JsonPropertyName("scale_fonts")]
    public bool ScaleFonts
    {
        get
        {
            return _ScaleFonts;
        }
        set
        {
            _ScaleFonts = value;
            if (Globals.ms is not null)
            {
                Globals.DefaultFont.PixelHeight = Globals.Settings.DefaultFontPX.ScaleFont();
                Globals.MessageFont.PixelHeight = Globals.Settings.MessageFontPX.ScaleFont();
                Globals.TopTimeFont.PixelHeight = Globals.Settings.TopTimeFonttPX.ScaleFont();
                Globals.SmallTimeFont.PixelHeight = ((uint)11).ScaleFont();
                Label._characters[Globals.ms.Context][Globals.DefaultFont].Clear();
                Label._characters[Globals.ms.Context][Globals.MessageFont].Clear();
                Label._characters[Globals.ms.Context][Globals.TopTimeFont].Clear();
                Label._characters[Globals.ms.Context][Globals.SmallTimeFont].Clear();
                
                Globals.ms.ForceUpdate(new(Globals.ms.ClientSize));
                Globals.ms.DrawFrame();
            }
        }
    }
    [JsonInclude]
    [JsonPropertyName("default_font_px")]
    public uint DefaultFontPX { get; set; } = 20;
    [JsonInclude]
    [JsonPropertyName("top_time_font_px")]
    public uint TopTimeFonttPX { get; set; } = 12;
    [JsonInclude]
    [JsonPropertyName("message_font_px")]
    public uint MessageFontPX { get; set; } = 17;
    [JsonInclude]
    [JsonPropertyName("message_font_line_space_px")]
    public uint MessageFontLineSpacePX { get; set; } = 5;
    
    [JsonInclude]
    [Description("24 Hour Time")]
    [JsonPropertyName("24hour_time")]
    public bool DayTime
    {
        get
        {
            return _DayTime;
        }
        set
        {
            _DayTime = value;
            if (DayTimeChanged is not null) DayTimeChanged.Invoke();
        }
    }
    
    [JsonIgnore]
    private bool _ScaleFonts = true;
    [JsonIgnore]
    private ConsoleLog _Logs = (ConsoleLog)(-25);
    [JsonIgnore]
    private bool _DayTime = false;
    public event Func<Task>? DayTimeChanged;

}

[JsonSerializable(typeof(Settings))]
[JsonSourceGenerationOptions(
    GenerationMode = JsonSourceGenerationMode.Default,
    PropertyNamingPolicy = JsonKnownNamingPolicy.Unspecified,
    WriteIndented = true,
    DefaultIgnoreCondition = JsonIgnoreCondition.Never)]
internal partial class SettingsContext : JsonSerializerContext
{

}