using System;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FoxTube
{
    public enum LoadingState { Loadnig, Loaded, Error, Blocked }

    /// <summary>
    /// Loading sreen. Represents loading ring or error
    /// </summary>
    public sealed partial class LoadingPage : Page
    {
        ResourceLoader resources = ResourceLoader.GetForCurrentView("LoadingPage");

        public event RoutedEventHandler RefreshPage;
        public LoadingState State { get; private set; } = LoadingState.Loadnig;

        public LoadingPage()
        {
            InitializeComponent();
        }

        public void Error(string exceptionId = "Unknown", string details = "N/A", bool isWifiTrouble = false)
        {
            ring.IsActive = false;
            trouble.Visibility = Visibility.Collapsed;
            wifiTrouble.Visibility = Visibility.Collapsed;
            blockIcon.Visibility = Visibility.Collapsed;

            if (isWifiTrouble)
            {
                wifiException.Text = $"{resources.GetString("/LoadingPage/ex")}: {exceptionId}";
                wifiMessage.Text = $"{resources.GetString("/LoadingPage/details")}: {details}";

                wifiTrouble.Visibility = Visibility.Visible;
            }
            else
            {
                exception.Text = $"{resources.GetString("/LoadingPage/ex")}: {exceptionId}";
                message.Text = $"{resources.GetString("/LoadingPage/details")}: {details}";

                trouble.Visibility = Visibility.Visible;
            }

            State = LoadingState.Error;
        }

        public void Block()
        {
            Visibility = Visibility.Visible;
            ring.IsActive = false;
            trouble.Visibility = Visibility.Collapsed;
            wifiTrouble.Visibility = Visibility.Collapsed;

            blockIcon.Visibility = Visibility.Visible;

            State = LoadingState.Blocked;
        }

        private async void openWifi_Click(object sender, RoutedEventArgs e)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:network"));
        }

        private async void openTroubleshoot_Click(object sender, RoutedEventArgs e)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:troubleshoot"));
        }

        private async void feedback_Click(object sender, RoutedEventArgs e)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("feedback-hub:"));
        }

        private void wifiRefresh_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
            RefreshPage.Invoke(this, null);
        }

        public void Refresh()
        {
            Visibility = Visibility.Visible;
            ring.IsActive = true;
            trouble.Visibility = Visibility.Collapsed;
            wifiTrouble.Visibility = Visibility.Collapsed;
            blockIcon.Visibility = Visibility.Collapsed;

            State = LoadingState.Loadnig;
        }

        public void Close()
        {
            Visibility = Visibility.Collapsed;

            State = LoadingState.Loaded;
        }
    }
}
