﻿
using MvvmCross.Core.ViewModels;
using System.Windows.Controls;
using System;
using OpenLearningPlayer.Shared.DB;
using System.Windows.Media;
using OpenLearningPlayer.Shared.Domain;
using OpenLearningPlayer.Shared.ViewModels;
using MvvmCross.Wpf.Views;
using MvvmCross.Platform;
using System.Windows.Threading;
using Unclassified.TxLib;
using System.Reflection;
using EntityExtensionForORM;

namespace OpenLearningPlayer
{

    public class AppWnd_ViewModel : ViewModel_Base
    {

        INavigationService NavigationService;

        public MvxCommand NavigationBack { get; private set; }
 
        public bool CanNavigateBack { get { return CanNavigateBack_; } set { Set(ref CanNavigateBack_, value); } }private bool CanNavigateBack_;

        public MvxWpfView  CurrentView {
            get { return CurrentView_; }
            set {
                Set(ref CurrentView_, value);
                SetupUserControl();
            } }
        private MvxWpfView CurrentView_;

        public Menu         CurrentMenu     { get { return _CurrentMenu; } set {
                Set(ref _CurrentMenu, value);
            } } Menu _CurrentMenu;
        public UserControl  DialogView      { get { return DialogView_; } set { Set(ref DialogView_, value); } } UserControl DialogView_;
        public StackPanel   RightWindowMenu { get { return _RightWindowMenu; } set { Set(ref _RightWindowMenu, value); } } StackPanel _RightWindowMenu;
        public Brush        Background      { get { return _Background; } set { Set(ref _Background, value); } }  Brush _Background;

        public string WindowTitle { get { return _WindowTitle; } set { Set(ref _WindowTitle, value); } }   private string _WindowTitle;

        public ContentControl DialogContent {
            get {
                return _DialogContent; }
            set {
                Set(ref _DialogContent, value);
                RaisePropertyChanged(()=>IsDialogOpen);
            } }
        ContentControl _DialogContent;

        public bool IsDialogOpen { get { return DialogContent != null; } }

        private void SetupUserControl()
        {
            Menu menu = (Menu)CurrentView.TryFindResource("CurrentMenu");
            if (menu != null) menu.DataContext = CurrentView.DataContext;
            CurrentMenu = menu;
            
            RightWindowMenu = (StackPanel)CurrentView.TryFindResource("RightWndMenu");
            if (RightWindowMenu != null) RightWindowMenu.DataContext = CurrentView.DataContext;

            Background = (Brush) App.MainMetroWindow.TryFindResource(ShortClassName(CurrentView.GetType().ToString()) + "Background");
            if(Background == null) Background = (Brush)App.MainMetroWindow.TryFindResource("MainBackground");

            // a hack for show a menu
            // http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx
            CurrentView.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { int i = 4; }));

        }

        string ShortClassName(string classname)
        {
            int pos = classname.IndexOf(".");
            if(pos == -1)
            {
                return classname;
            }
            else
            {
                return ShortClassName(classname.Substring(pos + 1));
            }
        }

        public SynchronizedObservableCollection<ProgressBar_Model> LongTasks { get { return _LongTasks; } set { Set( ref _LongTasks, value); } }  private SynchronizedObservableCollection<ProgressBar_Model> _LongTasks;

        public AppWnd_ViewModel()
        {

            LongTasks = new SynchronizedObservableCollection<ProgressBar_Model>();

            if (!IsInDesignMode)
            {
                WindowTitle = Tx.T("AppWnd.ApplicationHeader") + " " + Assembly.GetExecutingAssembly().GetName().Version;
            }

            if (IsInDesignMode)
            {
                LongTasks.Add(new ProgressBar_Model() {
                    Text = "A very long task",
                    IsIndeterminate =true,
                    Title="This is a title"});
                WindowTitle = "Open learning player 1.1.1.2";   
            }
        }


        public void NavigateToInitialForm()
        {
            NavigationBack = new MvxCommand(NavigateBack_cmd);
            App.MainMetroWindow.Closing += MainMetroWidow_Closing;
            CommandDispatcher = new MvxCommand<string>(CmdDispatcher);

            NavigationService = Mvx.Resolve<INavigationService>();
            //NavigationService.NavigateForward(Views.Login);

            AppSetting AppSettings = EFDbContext.Context.FirstOrDefault<AppSetting>();
            NavigationService.NavigateForward(Views.MainWindow);
            if (AppSettings.isFirstStart)
            {
                NavigationService.OpenDialog(Views.InitialWizard,new InitialWizard_parameters());
            }

        }

        public override void CmdDispatcher(string cmd)
        {
            switch (cmd)
            {
                default:
                    throw new NotImplementedException();
            }
        }

        void MainMetroWidow_Closing(object sender, System.EventArgs e)
        {
            NavigationService.Stop();
        }

        public void NavigateBack_cmd()
        {
            NavigationService.NavigateBack();
        }

        override public void OnNavigationOpenPage(ParametersOfForms param)
        {
            RaisePropertyChanged("CanNavigateBack");
        }

        override public void OnNavigationClosePage()
        {
            EFDbContext.SaveChgs(); 
        }
    }
}
