namespace Sonex.Client.Dialogs;

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

public static class Config
{
    public static readonly string _filePath =
        Path.Combine(AppContext.BaseDirectory, "..\\", "config.cfg");

    private static readonly Dictionary<string, string> _values = new();

    static Config()
    {
        Load();
    }

    // =========================
    // GET
    // =========================

    public static string GetStringValue(string key, string defaultValue = "")
    {
        if (_values.TryGetValue(key, out var v))
        {
            return v;
        }

        SetValue(key, defaultValue);
        return defaultValue;
    }

    public static int GetIntValue(string key, int defaultValue = 0)
    {
        var v = GetStringValue(key, defaultValue.ToString(CultureInfo.InvariantCulture));
        if (int.TryParse(v, out var r))
        {
            return r;
        }

        SetValue(key, defaultValue);
        return defaultValue;
    }

    public static double GetDoubleValue(string key, double defaultValue = 0)
    {
        var v = GetStringValue(key, defaultValue.ToString(CultureInfo.InvariantCulture));
        if (double.TryParse(v, NumberStyles.Float, CultureInfo.InvariantCulture, out var r))
        {
            return r;
        }

        SetValue(key, defaultValue);
        return defaultValue;
    }

    public static bool GetBoolValue(string key, bool defaultValue = false)
    {
        var v = GetStringValue(key, defaultValue.ToString());
        if (bool.TryParse(v, out var r))
        {
            return r;
        }

        SetValue(key, defaultValue);
        return defaultValue;
    }

    // =========================
    // SET
    // =========================

    public static void SetValue(string key, string value)
    {
        _values[key] = value;
        Save();
    }

    public static void SetValue(string key, int value)
        => SetValue(key, value.ToString());

    public static void SetValue(string key, double value)
        => SetValue(key, value.ToString(CultureInfo.InvariantCulture));

    public static void SetValue(string key, bool value)
        => SetValue(key, value.ToString());

    // =========================
    // LOAD / SAVE
    // =========================

    private static void Load()
    {
        if (!File.Exists(_filePath))
            return;

        foreach (var line in File.ReadAllLines(_filePath))
        {
            if (string.IsNullOrWhiteSpace(line))
                continue;

            var index = line.IndexOf('=');
            if (index <= 0)
                continue;

            var key = line[..index].Trim();
            var value = line[(index + 1)..].Trim();

            _values[key] = value;
        }
    }

    private static void Save()
    {
        var directory = Path.GetDirectoryName(_filePath);
        if (!string.IsNullOrWhiteSpace(directory))
            Directory.CreateDirectory(directory);

        using var writer = new StreamWriter(_filePath, false);

        foreach (var kv in _values)
            writer.WriteLine($"{kv.Key}={kv.Value}");
    }
}
