using System.Linq;
using Windows.ApplicationModel.Core;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using YoutubeExplode.Models.MediaStreams;
using System;
using Windows.Globalization;

namespace FoxTube.Pages.SettingsPages
{
    /// <summary>
    /// Settings page with general preferences
    /// </summary>
    public sealed partial class General : Page
    {
        public General()
        {
            InitializeComponent();

            language.SelectedItem = language.Items.Find(i => ((ComboBoxItem)i).Tag.ToString() == SettingsStorage.Language);
            safeSearch.SelectedIndex = SettingsStorage.SafeSearch;

            foreach (VideoQuality i in Enum.GetValues(typeof(VideoQuality)).ToReversedList())
                quality.Items.Add(new ComboBoxItem() { Tag = i.GetVideoQualityLabel(), Content = i.GetVideoQualityLabel() });
            quality.SelectedItem = quality.Items.ToList().Find(i => ((ComboBoxItem)i).Tag.ToString() == SettingsStorage.VideoQuality);

            clipboardProcessing.IsOn = SettingsStorage.ProcessClipboard;
            minimizedCB.IsOn = SettingsStorage.AppBarClosedMode == AppBarClosedDisplayMode.Minimal;

            mobileWarning.IsOn = SettingsStorage.CheckConnection;
            autoplay.IsOn = SettingsStorage.Autoplay;

            newVideo.IsOn = SettingsStorage.VideoNotifications;
            devNews.IsOn = SettingsStorage.DevNotifications;

            switch(SettingsStorage.Theme)
            {
                case 0:
                    light.IsChecked = true;
                    break;
                case 1:
                    dark.IsChecked = true;
                    break;
                case 2:
                    system.IsChecked = true;
                    break;
            }

            InitializeRegions();
        }

        async void InitializeRegions()
        {
            //Initialize regions
            I18nRegionsResource.ListRequest regRequest = SecretsVault.Service.I18nRegions.List("snippet");
            regRequest.Hl = SettingsStorage.Language;
            I18nRegionListResponse regResponse = await regRequest.ExecuteAsync();

            regResponse.Items.ForEach(i => region.Items.Add(new ComboBoxItem
            {
                Content = i.Snippet.Name,
                Tag = i.Snippet.Gl
            }));
            region.SelectedItem = region.Items.Find(i => ((ComboBoxItem)i).Tag as string == SettingsStorage.Region);

            //Initialize relevance language
            I18nLanguagesResource.ListRequest langRequest = SecretsVault.Service.I18nLanguages.List("snippet");
            langRequest.Hl = SettingsStorage.Language;
            I18nLanguageListResponse langResponse = await langRequest.ExecuteAsync();

            langResponse.Items.ForEach(i => relLanguage.Items.Add(new ComboBoxItem
            {
                Content = i.Snippet.Name,
                Tag = i.Snippet.Hl
            }));
            relLanguage.SelectedItem = relLanguage.Items.Find(i => ((ComboBoxItem)i).Tag as string == SettingsStorage.RelevanceLanguage);
        }

        private void language_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((language.SelectedItem as ComboBoxItem).Tag.ToString() == SettingsStorage.Language)
                return;
            ApplicationLanguages.PrimaryLanguageOverride = (language.SelectedItem as ComboBoxItem).Tag.ToString();
            SettingsStorage.Language = (language.SelectedItem as ComboBoxItem).Tag.ToString();
            restartNote.Visibility = Visibility.Visible;
        }

        private void quality_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SettingsStorage.VideoQuality = (quality.SelectedItem as ComboBoxItem).Tag as string;
        }

        private void mobileWarning_Toggled(object sender, RoutedEventArgs e)
        {
            SettingsStorage.CheckConnection = mobileWarning.IsOn;
        }

        private void autoplay_Toggled(object sender, RoutedEventArgs e)
        {
            SettingsStorage.Autoplay = autoplay.IsOn;
        }

        private void notification_IsEnabledChanged(object sender, RoutedEventArgs e)
        {
            SettingsStorage.VideoNotifications = newVideo.IsOn;
        }

        private void devNews_Toggled(object sender, RoutedEventArgs e)
        {
            SettingsStorage.DevNotifications = devNews.IsOn;
        }

        private void RelLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SettingsStorage.RelevanceLanguage = ((ComboBoxItem)relLanguage.SelectedItem).Tag.ToString();
        }

        private void region_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SettingsStorage.Region = ((ComboBoxItem)region.SelectedItem).Tag.ToString();
        }

        private void safeSearch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SettingsStorage.SafeSearch = safeSearch.SelectedIndex;
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (sender == light)
            {
                SettingsStorage.Theme = 0;
                Methods.MainPage.RequestedTheme = ElementTheme.Light;
            }
            else if (sender == dark)
            {
                SettingsStorage.Theme = 1;
                Methods.MainPage.RequestedTheme = ElementTheme.Dark;
            }
            else if (sender == system)
            {
                SettingsStorage.Theme = 2;
                if (new Windows.UI.ViewManagement.UISettings().GetColorValue(Windows.UI.ViewManagement.UIColorType.Background) == Colors.Black)
                    Methods.MainPage.RequestedTheme = ElementTheme.Dark;
                else
                    Methods.MainPage.RequestedTheme = ElementTheme.Light;
            }
            Methods.MainPage.SetTitleBar();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CoreApplication.Exit();
        }

        private void MinimizedCB_Toggled(object sender, RoutedEventArgs e)
        {
            SettingsStorage.AppBarClosedMode = minimizedCB.IsOn ? AppBarClosedDisplayMode.Minimal : AppBarClosedDisplayMode.Compact;
        }

        private void ClipboardProcessing_Toggled(object sender, RoutedEventArgs e)
        {
            SettingsStorage.ProcessClipboard = clipboardProcessing.IsOn;
        }
    }
}
