using Microsoft.Win32;
using PDF_Office.CustomControl.SystemControl;
using PDF_Office.Model;
using PDF_Office.EventAggregators;
using PDF_Office.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using ComPDFKitViewer.PdfViewer;
using Dragablz;
using System.Windows;
using Prism.Ioc;

namespace PDF_Office.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        /// <summary>
        /// 当前选中项
        /// </summary>
        //public MainContent SelectedItem { get; set; }

        private MainContent selectedItem;

        public MainContent SelectedItem
        {
            get { return selectedItem; }
            set {
                SetProperty(ref selectedItem, value);
                if (SelectedItem!=null)
                {
                    var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
                    if(pdfviewer!=null)
                    {
                        CurrentPDFViewer = pdfviewer;
                    }
                }
            }
        }

        private CPDFViewer pdfViewer;

        /// <summary>
        /// 当前页签的PdfViewer对象
        /// </summary>
        public CPDFViewer CurrentPDFViewer
        {
            get { return pdfViewer; }
            set { pdfViewer = value; }
        }


        public DelegateCommand AddTab { get; set; }


        public IRegionManager region;

        public IEventAggregator eventer;

        private IContainerProvider containerProvider;

        public IInterTabClient InterTabClient { get; }

        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container)
        {
            App.mainWindowViewModel = this;
            region = regionManager;

            eventer = eventAggregator;

            containerProvider = container;

            InterTabClient = new InterTabClient(container);

            AddTab = new DelegateCommand(AddTabItem);

            //第一次打开时需要自动加载Home页
            if (App.IsFirstOpen)
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                {
                    region.RequestNavigate(RegionNames.MainRegion, "MainContent");
                }
                ));
                App.IsFirstOpen = false;
            }
        }

        /// <summary>
        /// 单击+号时添加页签
        /// </summary>
        private void AddTabItem()
        {
            region.RequestNavigate(RegionNames.MainRegion, "MainContent");
        }

        /// <summary>
        /// 选择多文档打开时
        /// </summary>
        /// <param name="filePath"></param>
        public void AddTabItem(string filePath)
        {
            NavigationParameters parameters = new NavigationParameters
            {
                { ParameterNames.FilePath, filePath }
            };
            region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
        }

        public void CloseTabItem(object item)
        {
            if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
            {
                region.Regions[RegionNames.MainRegion].Remove(item);
            }
            else
            {
                App.Current.MainWindow.Close();
            }
            return;
        }

        public void SelectItem(string filepath)
        {
            var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
            if (item == null)
                return;

            region.Regions[RegionNames.MainRegion].Activate(item);
            var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
            if(behavior!=null)
            {
                behavior.ActivateView(item);
            }
        }
    }
}
