
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using static ModelViewerSettings;

public class ModelViewerSettings
{
    public enum SettingType
    {
        Text,
        FilePath,
        FolderPath
    }

    public class Setting
    {
        public SettingType Type;
        public string Value;
        public bool Editable;

        public Setting(string value, SettingType type = SettingType.Text, bool editable = true)
        {
            Value = value;
            Editable = editable;
            Type = type;
        }
    }

    public static ModelViewerSettings Instance;

    public Dictionary<string, Setting> Settings = new Dictionary<string, Setting>();

    public static System.Action<ModelViewerSettings> OnSettingsSaved;

    public void SetValues(Dictionary<string, Setting> defaultSettings)
    {
        foreach (var keyvalue in defaultSettings)
        {
            Set(keyvalue.Key, keyvalue.Value);
        }
        Save();
    }

    public static string Get(string key, string defaultValue = "")
    {
        var settings = Instance;
        if (!settings.Settings.ContainsKey(key))
        {
            settings.Settings[key] = new Setting(defaultValue);
        }
        return settings.Settings[key].Value;
    }

    public static string Get(string key, Setting defaultSetting)
    {
        var settings = Instance;
        if (!settings.Settings.ContainsKey(key))
        {
            settings.Settings[key] = defaultSetting;
        }
        return settings.Settings[key].Value;
    }

    public static void Set(string key, Setting setting, bool save = false)
    {
        Instance.Settings[key] = setting;
        if (save) Save();
    }

    public static void Set(string key, string value, bool save = false)
    {
        Debug.Log("Updating " + key + " to " + value);
        if (Instance.Settings.ContainsKey(key))
        {
            Instance.Settings[key].Value = value;
        }
        else
        {
            Instance.Settings[key] = new Setting(value);
        }
        if (save) Save();
    }

    public static void Load()
    {
        if (Instance != null) return;

        var filePath = Application.dataPath + "/../_settings.json";
        if(File.Exists(filePath))
        {
            var settings = File.ReadAllText(filePath);
            try
            {
                Instance = JsonConvert.DeserializeObject<ModelViewerSettings>(settings);
            }
            catch
            {
                Instance = new ModelViewerSettings();
            }
        }
        else
        {
            Instance = new ModelViewerSettings();
            Save();
        }
    }

    public static void Save()
    {
        if(Instance == null)
        {
            Debug.LogError("Settings have not been initialized!");
            return;
        }
        var filePath = Application.dataPath + "/../_settings.json";
        File.WriteAllText(filePath, JsonConvert.SerializeObject(Instance));
        OnSettingsSaved?.Invoke(Instance);
    }
}