using System.Text.Json;
using System.Text.Json.Serialization;

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,
                DownloadThreads = 1,
                Scale = 2
            };
            file.Colors = new Colors()
            {
                Background = new ConfigColor()
                {
                    R = 0.1568627450980392f,
                    G = 0.1568627450980392f,
                    B = 0.1568627450980392f,
                    A = 1f
                },
                Progress_bars = new ProgressBars()
                {
                    Backcolor = new ConfigColor()
                    {
                        R = 1.0f,
                        G = 1.0f,
                        B = 1.0f,
                        A = 1f
                    },
                    Fillcolor = new ConfigColor()
                    {
                        R = 0.0392156862745098f,
                        G = 0.8509803921568627f,
                        B = 1f,
                        A = 1f
                    }
                },
                Text = new ConfigColor()
                {
                    R = 1.0f,
                    G = 1.0f,
                    B = 1.0f,
                    A = 1f
                }
            };
            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 int DownloadThreads { get; set; }
    public float Scale { get; set; }
}

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

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

