using System.IO;
using Newtonsoft.Json;
using RothenburgAR.UI;

namespace RothenburgAR.Common
{
    class SettingsManager : Singleton<SettingsManager>
    {
        public bool? IsInitialized { get; private set; }

        internal class Settings
        {
            public string Language { get; set; }
            public FontsizeSetting Fontsize { get; set; }
            public int ColorschemeID { get; set; }
        }

        private Settings settings;

        public void Start()
        {
            if (File.Exists(PathHelper.SettingsFilePath))
            {
                //load settings file into settings object if existent
                var json = File.ReadAllText(PathHelper.SettingsFilePath);
                settings = JsonConvert.DeserializeObject<Settings>(json);

                LanguageManager.Instance.CurrentLanguageKey = settings.Language;
                UIColorSchemeManager.Instance.SetUiColorScheme(settings.ColorschemeID);
                FontsizeManager.Instance.CurrentFontsizeSetting = settings.Fontsize;

                IsInitialized = true;
            }
            else
            {
                settings = new Settings();
                IsInitialized = false;
            }
        }

        public void PersistSettings()
        {
            settings.Language = LanguageManager.Instance.CurrentLanguageKey;
            settings.ColorschemeID = UIColorSchemeManager.Instance.CurrentColorSchemeID;
            settings.Fontsize = FontsizeManager.Instance.CurrentFontsizeSetting;

            var json = JsonConvert.SerializeObject(settings);
            File.WriteAllText(PathHelper.SettingsFilePath, json);
        }
    }
}