using Microsoft.Extensions.DependencyInjection;
using PCUT.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
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 LoginPage : Page
    {
        public static readonly Size Size = new Size(1000, 724);
        public LoginViewModel LoginModel { get; set; }
        public LoginPage()
        {
            this.InitializeComponent();
            var container = ((App)Application.Current).AppContainer;
            LoginModel = ActivatorUtilities.GetServiceOrCreateInstance(container, typeof(LoginViewModel)) as LoginViewModel;
        }

        private async void Page_Loading(FrameworkElement sender, object args)
        {
            ApplicationView.PreferredLaunchViewSize = Size;
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            var view = ApplicationView.GetForCurrentView();
            view.TryResizeView(Size);
            await view.TryEnterViewModeAsync(ApplicationViewMode.Default);
        }

        private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
                if (FocusManager.GetFocusedElement() == UsernameInput)
                    FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
                else
                    LoginModel.NavigateMainCommand.Execute(null);
        }

        private void ShowPassword_Toggled(object sender, RoutedEventArgs e)
        {
            if (ShowPasswordToggle.IsOn)
            {
                PasswordInput.PasswordRevealMode = PasswordRevealMode.Visible;
            }
            else
            {
                PasswordInput.PasswordRevealMode = PasswordRevealMode.Hidden;
            }
        }
    }
}
