using Serilog.Events;
using System;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace VeloeMinecraftLauncher.MinecraftLauncher;

internal static class Settings
{
    //public static readonly string JavaPath = "C:\\Program Files\\Microsoft\\jdk-11.0.12.7-hotspot\\bin\\java.exe";
    public static string javaPath = "";
    public static string minecraftForlderPath = Directory.GetCurrentDirectory() + '/';
    public static UInt32 maxRam = 2048;
    public static bool useCustomJava = false;
    public static bool setMaxRam = false;
    public static bool setPath = true;
    public static bool checkGameAssets = false;
    public static bool gameLogToLauncher = false;
    public static string username;
    public static string lastChosenVersion;
    public static Serilog.ILogger logger;
    public static Serilog.ILogger avaloniaLogger;
    public static bool setMaxLog = false;
    public static UInt32 maxLog = 1024;
    public static LogEventLevel consoleLogEventLevel = LogEventLevel.Debug;
    public static LogEventLevel fileLogEventLevel = LogEventLevel.Debug;

    public static void UpdateSettings(SettingsSerializable settings)
    {
        if (settings is null)
            return;
        javaPath = settings.JavaPath;
        minecraftForlderPath = settings.MinecraftForlderPath;
        maxRam = settings.MaxRam;
        useCustomJava = settings.UseCustomJava;
        setMaxRam = settings.SetMaxRam;
        setPath = settings.SetPath;
        checkGameAssets = settings.CheckGameAssets;
        gameLogToLauncher = settings.GameLogToLauncher;
        lastChosenVersion = settings.LastChosenVersion;
        username = settings.Username;
        setMaxLog = settings.SetMaxLog;
        maxLog = settings.MaxLog;
        consoleLogEventLevel = settings.ConsoleLogEventLevel;
        fileLogEventLevel = settings.FileLogEventLevel;

    }

    public static void LoadSettings()
    {
        if (!File.Exists($"settings.json"))
        {
            var file = File.Create($"settings.json");
            file.Close();
            file.Dispose();
        }

        var settings = File.ReadAllText($"settings.json");

        if (settings != String.Empty)
        {
            try
            {
                var settingsSerializable = JsonSerializer.Deserialize<SettingsSerializable>(settings, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

                Settings.UpdateSettings(settingsSerializable);
            }
            catch (JsonException ex)
            {
                //TODO log
            }
        }
    }

    public static void SaveSettings()
    {
        if(minecraftForlderPath == string.Empty)
            minecraftForlderPath = Directory.GetCurrentDirectory();

        if(minecraftForlderPath.Last() is not ('/' or '\\'))
            minecraftForlderPath = minecraftForlderPath + "/";

        if (!Directory.Exists(minecraftForlderPath))
            Directory.CreateDirectory(minecraftForlderPath);

        var settings = JsonSerializer.Serialize(new SettingsSerializable());

        File.WriteAllText($"settings.json", settings);
    }
}

public class SettingsSerializable
{
    public string JavaPath {get; set;}
    public string MinecraftForlderPath { get; set;}
    public UInt32 MaxRam { get; set; }
    public bool UseCustomJava { get; set; }
    public bool SetMaxRam { get; set; }
    public bool SetPath { get; set; }
    public bool CheckGameAssets { get; set; }

    public bool GameLogToLauncher { get; set; }

    public string Username { get; set; }

    public string LastChosenVersion { get; set; }

    public bool SetMaxLog { get; set; }
    public UInt32 MaxLog { get; set; }
    public LogEventLevel ConsoleLogEventLevel { get; set; }
    public LogEventLevel FileLogEventLevel { get; set; }

    public SettingsSerializable()
    {
        JavaPath = Settings.javaPath;
        MinecraftForlderPath= Settings.minecraftForlderPath;
        MaxRam= Settings.maxRam;
        UseCustomJava= Settings.useCustomJava;
        SetMaxRam= Settings.setMaxRam;
        SetPath = Settings.setPath;
        CheckGameAssets= Settings.checkGameAssets;
        GameLogToLauncher= Settings.gameLogToLauncher;
        Username = Settings.username;
        LastChosenVersion= Settings.lastChosenVersion;
        SetMaxLog= Settings.setMaxLog;
        MaxLog = Settings.maxLog;
        ConsoleLogEventLevel= Settings.consoleLogEventLevel;
        FileLogEventLevel= Settings.fileLogEventLevel;
    }
}
