using Microsoft.AppCenter.Analytics;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.UI.Xaml.Controls;

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 double volume = 100;

        public string language = GetLanguage();
        public string relevanceLanguage = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName;
        public string region = CultureInfo.InstalledUICulture.Name.Split('-')[1];
        public int safeSearch = 0;

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

        public TimeSpan uptime = TimeSpan.FromSeconds(0);
        public bool promptReview = true;
        public bool promptFeedback = true;

        public bool processClipboard = true;
        public bool minimizeCommandbar = false;

        private static string GetLanguage()
        {
            if (App.AvailableLanguages.Contains(CultureInfo.InstalledUICulture.Name))
                return CultureInfo.InstalledUICulture.Name;
            else if ((new[] { "ua", "ru", "by", "kz", "kg", "md", "lv", "ee" }).Contains(CultureInfo.InstalledUICulture.TwoLetterISOLanguageName))
                return "ru-RU";
            else
                return "en-US";
        }
    }

    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 double 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["version"] == 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"] = (int)MatureState.Blocked;
                    return MatureState.Blocked;
                }
                else return (MatureState)storage.Values["mature"];
            }
            set
            {
                storage.Values["mature"] = value;
            }
        }

        public static TimeSpan Uptime
        {
            get { return Container.uptime; }
            set
            {
                Container.uptime = value;
                SaveData();
            }
        }
        public static bool PromptReview
        {
            get { return Container.promptReview; }
            set
            {
                Container.promptReview = value;
                SaveData();
            }
        }
        public static bool PromptFeedback
        {
            get { return Container.promptFeedback; }
            set
            {
                Container.promptFeedback = value;
                SaveData();
            }
        }

        public static bool ProcessClipboard
        {
            get => Container.processClipboard;
            set
            {
                Container.processClipboard = value;
                SaveData();
            }
        }

        public static AppBarClosedDisplayMode AppBarClosedMode
        {
            get => Container.minimizeCommandbar ? AppBarClosedDisplayMode.Minimal : AppBarClosedDisplayMode.Compact;
            set
            {
                Container.minimizeCommandbar = value == AppBarClosedDisplayMode.Minimal;
                SaveData();
            }
        }

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

        public static void LoadData()
        {
            try
            {
                Container = JsonConvert.DeserializeObject<SettingsContainer>(storage.Values["settings"] as string);
            }
            catch (Exception e)
            {
                SaveData();
                if (storage.Values["settings"] != null)
                    Analytics.TrackEvent("Failed to retrieve settings", new Dictionary<string, string>
                    {
                        { "Exception", e.GetType().ToString() },
                        { "Message", e.Message },
                        { "StackTrace", e.StackTrace }
                    });
            }
        }

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