using Http.Core;
using PCUT.ViewModels;
using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using Http.Core.Extensions;
using static Http.Core.Constants.HttpConstants;
using Windows.Graphics.Display;
using Http.Core.Contexts;
using PCUT.Pages.UserGuide;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace PCUT.Pages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainMenuPage : Page
    {
        private MainMenuPageViewModel ViewModel { get; set; }
        public MainMenuPage()
        {
            this.InitializeComponent();
            ViewModel = DataContext as MainMenuPageViewModel;
        }

        private void Page_Loading(FrameworkElement sender, object args)
        {
            var view = ApplicationView.GetForCurrentView();
            var height = DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
            var width = DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
            var size = new Size(width - 15 - 1, height - 40 - 1);
            _ = view.TryResizeView(size);
            ViewModel.UserName = UserContext.Instance.Profile.UserName;
            ViewModel.Expiry = UserContext.Instance.Profile.ExpiredAt?.ToLocalTime().ToString("dd/MM/yyyy");
        }

        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel.Categories.Bind(CategoryList);
            await ViewModel.GetCategoriesAsync();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            NavigateContentFrame<DataCenterPage>(ViewModel);
        }

        private void NavigationViewItem_Invoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            var item = args.InvokedItemContainer as NavigationViewItem;
            var itemTag = item.Tag?.ToString();
            if (itemTag == "Menu")
                ViewModel.ToggleMenuDisplay();
            else
            {
                ViewModel.HideMenuDisplay();
                switch (itemTag)
                {
                    case "DataCenter":
                        NavigateContentFrame<DataCenterPage>(ViewModel);
                        break;
                    case "DesignCenter":
                        NavigateContentFrame<DesignCenterPage>();
                        break;
                    case "UserGuide":
                        NavigateContentFrame<UserGuidePage>();
                        break;
                    case "AdminCenter":
                        NavigateContentFrame<AdminCenterPage>();
                        break;
                }
            }
        }

        private void NavigateContentFrame<TPage>(object param = null)
        {
            var destPageType = typeof(TPage);
            if (ContentFrame.Content?.GetType() != destPageType)
            {
                ViewModel.IsMenuEnabled = destPageType == typeof(DataCenterPage);
                ContentFrame.Navigate(destPageType, param);
            }
        }

        private void CategoryList_Tapped(object sender, TappedRoutedEventArgs e)
        {
            ViewModel.Categories.SelectedIndex = CategoryList.SelectedIndex;
        }

        private void LogOutNavItem_Tapped(object sender, TappedRoutedEventArgs e)
        {
            ShowLogoutConfirmationDialog();
        }

        private async void ShowLogoutConfirmationDialog()
        {
            ContentDialog logoutConfirmationDialog = new ContentDialog
            {
                Title = "Confirm Logout",
                Content = "Are you sure you want to log out?",
                PrimaryButtonText = "Log Out",
                CloseButtonText = "Cancel"
            };
            logoutConfirmationDialog.PrimaryButtonStyle = Application.Current.Resources["PrimaryDialogButton"] as Style;
            logoutConfirmationDialog.CloseButtonStyle = Application.Current.Resources["CloseDialogButton"] as Style;

            ContentDialogResult result = await logoutConfirmationDialog.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {
                await PerformLogoutAsync();
            }
        }

        private async Task PerformLogoutAsync()
        {
            using (var httpClient = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                var loginSuccess = await httpClient.LogOutAsync();
            }
            Frame.Navigate(typeof(LoginPage));
        }

        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            if (e.SourcePageType == typeof(DesignCenterPage))
            {
                NavigationView.SelectedItem = DesignCenterNavItem;
            }
            else if (e.SourcePageType == typeof(DataCenterPage))
            {
                NavigationView.SelectedItem = DataCenterNavItem;
            }
            else if (e.SourcePageType == typeof(UserGuidePage))
            {
                NavigationView.SelectedItem = UserGuideNavItem;
            }
            else if (e.SourcePageType == typeof(AdminCenterPage))
            {
                NavigationView.SelectedItem = AdminCenterNavItem;
            }
            else
            {
                NavigationView.SelectedItem = null;
            }
        }
    }
}
