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

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

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

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

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

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

        public static double GetDoubleValue(string key, double defaultValue = 0)
        {
            var v = GetStringValue(key);
            if (double.TryParse(v, out var r))
            {
                return r;
            }
            else
            {
                SetValue(key, defaultValue);
                return defaultValue;
            }
        }

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

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

        public static void SetValue(string key, string value)
        {
            EnsureLoaded();
            _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 EnsureLoaded()
        {
            if (_loaded) return;
            Load();
            _loaded = true;
        }

        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];
                var value = line[(index + 1)..];

                _values[key] = value;
            }
        }

        private static void Save()
        {
            using var writer = new StreamWriter(_filePath, false);

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