using Newtonsoft.Json;
using System;
using System.Globalization;
using System.Linq;
using Windows.ApplicationModel;
using Windows.Storage;

namespace FoxTube
{
    public enum MatureState { Blocked, Allowed }

    public class SettingsContainer
    {
        public string videoQuality = "remember";
        public string rememberedQuality = "1080p";

        public bool videoNotifications = true;
        public bool devNotifications = true;

        public bool checkConnection = true;
        public bool autoplay = true;
        public int volume = 100;

        public string language = (new[] { "ua", "ru", "by", "kz", "kg", "md", "lv", "ee" }).Contains(CultureInfo.InstalledUICulture.TwoLetterISOLanguageName) ? "ru-RU" : "en-US";
        public string relevanceLanguage = (new[] { "ua", "ru", "by", "kz", "kg", "md", "lv", "ee" }).Contains(CultureInfo.InstalledUICulture.TwoLetterISOLanguageName) ? "ru" : "en";
        public string region = (new[] { "ua", "ru", "by", "kz", "kg", "md", "lv", "ee" }).Contains(CultureInfo.InstalledUICulture.TwoLetterISOLanguageName) ? "ru" : "us";
        public int safeSearch = 0;

        public bool hasAccount = false;
        public int theme = 2;
    }

    public static class SettingsStorage
    {
        public static string VideoQuality
        {
            get { return Container.videoQuality; }
            set
            {
                Container.videoQuality = value;
                SaveData();
            }
        }
        public static string RememberedQuality
        {
            get { return Container.rememberedQuality; }
            set
            {
                Container.rememberedQuality = value;
                SaveData();
            }
        }

        public static bool VideoNotifications
        {
            get { return Container.videoNotifications; }
            set
            {
                Container.videoNotifications = value;
                SaveData();
            }
        }
        public static bool DevNotifications
        {
            get { return Container.devNotifications; }
            set
            {
                Container.devNotifications = value;
                SaveData();
            }
        }

        public static bool CheckConnection
        {
            get { return Container.checkConnection; }
            set
            {
                Container.checkConnection = value;
                SaveData();
            }
        }
        public static bool Autoplay
        {
            get { return Container.autoplay; }
            set
            {
                Container.autoplay = value;
                SaveData();
            }
        }
        public static int Volume
        {
            get { return Container.volume; }
            set
            {
                Container.volume = value;
                SaveData();
            }
        }

        public static string Language
        {
            get { return Container.language; }
            set
            {
                Container.language = value;
                SaveData();
            }
        }
        public static string RelevanceLanguage
        {
            get { return Container.relevanceLanguage; }
            set
            {
                Container.relevanceLanguage = value;
                SaveData();
            }
        }
        public static string Region
        {
            get { return Container.region; }
            set
            {
                Container.region = value;
                SaveData();
            }
        }
        public static int SafeSearch
        {
            get { return Container.safeSearch; }
            set
            {
                Container.safeSearch = value;
                SaveData();
            }
        }
        public static bool HasAccount
        {
            get { return Container.hasAccount; }
            set
            {
                Container.hasAccount = value;
                SaveData();
            }
        }
        public static int Theme
        {
            get { return Container.theme; }
            set
            {
                Container.theme = value;
                SaveData();
            }
        }

        public static string Version
        {
            get
            {
                if (storage.Values["ver"] == null)
                {
                    PackageVersion ver = Package.Current.Id.Version;
                    storage.Values["version"] = $"{ver.Major}.{ver.Minor}";
                    return $"{ver.Major}.{ver.Minor}";
                }
                else return (string)storage.Values["version"];
            }
            set
            {
                storage.Values["version"] = value;
            }
        }
        public static MatureState Mature
        {
            get
            {
                if (storage.Values["mature"] == null)
                {
                    storage.Values["mature"] = MatureState.Blocked;
                    return MatureState.Blocked;
                }
                else return (MatureState)storage.Values["mature"];
            }
            set
            {
                storage.Values["mature"] = value;
            }
        }

        //Settings storage
        private static readonly ApplicationDataContainer storage = ApplicationData.Current.RoamingSettings;
        private static SettingsContainer Container;

        public static void LoadData()
        {
            try
            {
                Container = JsonConvert.DeserializeObject<SettingsContainer>(storage.Values["settings"] as string);
            }
            catch
            {
                Container = new SettingsContainer();
                SaveData();
            }
        }

        public static void SaveData()
        {
            storage.Values["settings"] = JsonConvert.SerializeObject(Container);
        }
    }
}
