using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.UserDataTasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 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 AdminCenterPage : Page
    {
        public AdminCenterPage()
        {
            this.InitializeComponent();
        }
        private string _currentSelectedNavItemTag;

        private void NavigationViewItem_Invoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            var item = args.InvokedItemContainer as NavigationViewItem;
            var itemTag = item.Tag.ToString();

            if (_currentSelectedNavItemTag == itemTag)
            {
                return;
            }

            _currentSelectedNavItemTag = itemTag;

            switch (itemTag)
            {
                case "Categories":
                    ContentFrame.Navigate(typeof(CategoriesViewPage));
                    break;
                case "Files":
                    ContentFrame.Navigate(typeof(FileManagerPage));
                    break;
                case "Users":
                    ContentFrame.Navigate(typeof(UserListPage));
                    break;
                case "Logs":
                    ContentFrame.Navigate(typeof(LogInformationPage));
                    break;
            }
            sender.IsPaneOpen = true;
        }

        private void AdminCenterPage_BackRequested(object sender, BackRequestedEventArgs e)
        {
            if (ContentFrame.CanGoBack)
            {
                ContentFrame.GoBack();
                e.Handled = true; 
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().BackRequested += Page_BackRequested;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            SystemNavigationManager.GetForCurrentView().BackRequested -= Page_BackRequested;
        }

        private void Page_BackRequested(object sender, BackRequestedEventArgs e)
        {
            Frame rootFrame = this.Frame;
            if (rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
                e.Handled = true;
            }
        }
    }
}
