using System.Text.Json;
using System.Text.Json.Serialization;
using OpenTK.Mathematics;

namespace Updater;

public class Config
{
    public static ConfigFile MakeFile()
    {
        if (!File.Exists(Handler.JT + "/Updater/Config/Settings.json"))
        {
            ConfigFile file = new();
            file.Format = new Format()
            {
                DownloadAmount = ConfigFileSize.MegaByte,
                DownloadSpeed = ConfigFileSize.MegaByte,
                DrawingGaps = new()
                {
                    X = 0,
                    Y = 0,
                    Z = 0,
                    W = 0
                },
                DownloadThreads = 1,
                Scale = 1
            };
            file.Colors = new Colors()
            {
                Background = new ConfigColor()
                {
                    R = 40,
                    G = 40,
                    B = 40,
                    A = 255
                },
                Progress_bars = new ProgressBars()
                {
                    Backcolor = new ConfigColor()
                    {
                        R = 255,
                        G = 255,
                        B = 255,
                        A = 255
                    },
                    Fillcolor = new ConfigColor()
                    {
                        R = 10,
                        G = 217,
                        B = 255,
                        A = 255
                    }
                },
                Text = new ConfigColor()
                {
                    R = 255,
                    G = 255,
                    B = 255,
                    A = 255
                }
            };
            if (!Directory.Exists(Handler.JT)) Directory.CreateDirectory(Handler.JT);
            if (!Directory.Exists(Handler.JT + "/Updater")) Directory.CreateDirectory(Handler.JT + "/Updater" );
            if (!Directory.Exists(Handler.JT + "/Updater/Config")) Directory.CreateDirectory(Handler.JT + "/Updater/Config");
            FileStream ms = new(Handler.JT + "/Updater/Config/Settings.json", FileMode.Create);
            JsonSerializer.Serialize(new Utf8JsonWriter(ms),
                                     file,
                                     ConfigFileContext.Default.ConfigFile);
            ms.Close();
            return file;
        }
#pragma warning disable CS8603 // Possible null reference return.
        return null;
#pragma warning restore CS8603 // Possible null reference return.
    }

    public static string GetFile()
    {
        _ = MakeFile();
        return File.ReadAllText(Handler.JT + "/Updater/Config/Settings.json");
    }

    public static ConfigFile GetConfig()
    {
        try
        {
            ConfigFile? temp = JsonSerializer.Deserialize(GetFile(), ConfigFileContext.Default.ConfigFile);
            if (temp is null)
            {
                File.Delete(Handler.JT + "/Updater/Config/Settings.json");
                temp = MakeFile();
            }
            int max = Environment.ProcessorCount * 2;
            if (temp.Format.DownloadThreads > max)
            {
                temp.Format.DownloadThreads = max;
                FileStream ms = new(Handler.JT + "/Updater/Config/Settings.json", FileMode.Create);
                JsonSerializer.Serialize(new Utf8JsonWriter(ms),
                                         temp,
                                         ConfigFileContext.Default.ConfigFile);
            }
            if (temp.Format.DownloadThreads < 1)
            {
                temp.Format.DownloadThreads = 1;
                FileStream ms = new(Handler.JT + "/Updater/Config/Settings.json", FileMode.Create);
                JsonSerializer.Serialize(new Utf8JsonWriter(ms),
                                         temp,
                                         ConfigFileContext.Default.ConfigFile);
            }
            return temp;
        }
        catch
        {
            File.Delete(Handler.JT + "/Updater/Config/Settings.json");
            return MakeFile();
        }
    }

}

[JsonSerializable(typeof(ConfigFile))]
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default,
    PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = true)]
internal partial class ConfigFileContext : JsonSerializerContext;

public class ConfigFile
{
    public Colors Colors { get; set; } = default!;

    public Format Format { get; set; } = default!;
}

public class Colors
{
    public ConfigColor Background { get; set; } = default!;
    public ProgressBars Progress_bars { get; set; } = default!;
    public ConfigColor Text { get; set; } = default!;
}

public class ProgressBars
{
    public ConfigColor Backcolor { get; set; } = default!;
    public ConfigColor Fillcolor { get; set; } = default!;
}

public class Format
{
    public ConfigFileSize DownloadSpeed { get; set; }
    public ConfigFileSize DownloadAmount { get; set; }
    public ConfigVector DrawingGaps { get; set; } = default!;
    public int DownloadThreads { get; set; }
    public float Scale { get; set; }
}

public class ConfigColor
{
    public byte R { get; set; }
    public byte G { get; set; }
    public byte B { get; set; }
    public byte A { get; set; }
}

public class ConfigVector
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }
    public int W { get; set; }
}

public enum ConfigFileSize
{
    MegaByte,
    MegaBit,
    MebiByte,
    MebiBit,
}

