using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Core;
using TaskIt.ViewModels;
using TaskIt.Views.Dialogs;

namespace TaskIt
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += OnLoaded;                        
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            CompositionInitializer.SatisfyImports(this);
            DataContext = ViewModel;

            ViewModel.ConfirmDeletion += OnConfirmDeletion;
            ViewModel.CloseConfirmDeletionWindow += OnCloseConfirmDeletionWindow;
            ViewModel.Error += OnError;
            ViewModel.ValidationError += OnValidationError;
            ViewModel.ValidationResult += OnValidationResult;
            ViewModel.CloseValidationWindow += OnCloseValidationWindow;
            ViewModel.CloseErrorWindow += OnCloseErrorWindow;
            ContentFrame.Navigated += OnNavigated;
            ContentFrame.NavigationFailed += OnNavigationFailed;

            if (Utils.CurrentApplication.IsRunningOutOfBrowser || HtmlPage.Plugin == null)
            {
                return;
            }

            HtmlPage.Plugin.Focus();
        }

        private void OnConfirmDeletion(object sender, RoutedEventArgs e)
        {
            ConfirmDeletionWindow.ShowDialog();
        }

        void OnCloseConfirmDeletionWindow(object sender, RoutedEventArgs e)
        {
            ConfirmDeletionWindow.Close();
        }

        void OnError(object sender, RoutedEventArgs e)
        {
            ErrorWindow.ShowDialog();
        }

        private void OnValidationError(object sender, RoutedEventArgs e)
        {
            ValidationErrorWindow.ShowDialog();
        }

        private void OnValidationResult(object sender, RoutedEventArgs e)
        {
            ValidationResultWindow.ShowDialog();
        }

        private void OnCloseValidationWindow(object sender, RoutedEventArgs e)
        {
            ValidationErrorWindow.Close();
        }

        private void OnCloseErrorWindow(object sender, RoutedEventArgs e)
        {
            ErrorWindow.Close();
        }

        private void OnNavigated(object sender, NavigationEventArgs e)
        {
            var uri = e.Uri.ToString();
            SelectedUri = uri;

            if (uri == "")
            {
                // This happens when the app first load up
                return;
            }

            if (SelectedUri == lnkHome.NavigateUri.ToString())
            {
                HideNavigationLinksStoryboard.Begin();
            }
            else
            {
                ShowNavigationLinksStoryboard.Begin();
            }
        }

        private static void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            Messenger.Default.Send(new OperationCompletedArgs());
            e.Handled = true;
            ErrorChildWindow.CreateNew(e.Exception);
        }

        public string SelectedUri { get; set; }

        [Import]
        public MainPageViewModel ViewModel { get; set; }
    }
}