using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Office.Interop.Word;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Views;
using PDFSettings.Settings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
{
    public class ViewModularContentViewModel : BindableBase, INavigationAware
    {
        private IRegionManager region;

        private IDialogService dialogs;

        //记录 单页 双页  书本模式
        private ViewMode viewmode = ViewMode.Single;

        public CPDFViewer PDFViewer { get; set; }
        public BottomToolContentViewModel BottomToolContentViewModel { get; set; }
        public ViewContentViewModel ViewContentViewModel { get; set; }
        public string SplitScreenViewRegionName { get; set; }
        public string ThemesContentName { get; set; }
        private Visibility splitScreenViewVisible = Visibility.Collapsed;

        /// <summary>
        /// 控制Content的显示 用于显示分屏的模块
        /// </summary>
        public Visibility SplitScreenViewVisible
        {
            get { return splitScreenViewVisible; }
            set
            {
                SetProperty(ref splitScreenViewVisible, value);
            }
        }

        private bool isContinue;

        public bool IsContinue
        {
            get { return isContinue; }
            set
            {
                SetProperty(ref isContinue, value);
                if (value)
                {
                    SetModeView();
                }
            }
        }

        private bool isPagesBreak = true;

        public bool IsPagesBreak
        {
            get { return isPagesBreak; }
            set
            {
                SetProperty(ref isPagesBreak, value);
            }
        }

        private bool isSingleView;

        public bool IsSingleView
        {
            get { return isSingleView; }
            set
            {
                SetProperty(ref isSingleView, value);
                if (value)
                {
                    SetModeView();
                }
            }
        }

        private bool isTwoPageView;

        public bool IsTwoPageView
        {
            get { return isTwoPageView; }
            set
            {
                SetProperty(ref isTwoPageView, value);
                if (value)
                {
                    SetModeView();
                }
            }
        }

        private bool isBookModeView;

        public bool IsBookModeView
        {
            get { return isBookModeView; }
            set
            {
                SetProperty(ref isBookModeView, value);
                if (value)
                {
                    SetModeView();
                }
            }
        }

        public DelegateCommand<object> VerticalSplitScreenCommand { get; set; }
        public DelegateCommand<object> DisableCommand { get; set; }

        public DelegateCommand SetViewModeCommand { get; set; }
        public DelegateCommand<object> ContinueCommand { get; set; }
        public DelegateCommand<object> PagesBreakCommand { get; set; }

        public DelegateCommand<object> RotateCommand { get; set; }

        public ViewModularContentViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            region = regionManager;
            dialogs = dialogService;
            //未显示时无法注册上Region名称，所以需要短暂显示
            SplitScreenViewVisible = Visibility.Visible;
            SplitScreenViewRegionName = RegionNames.SplitScreenViewRegionName;
            ThemesContentName = RegionNames.ThemesContentName;
            SplitScreenViewVisible = Visibility.Collapsed;

            VerticalSplitScreenCommand = new DelegateCommand<object>(VerticalSplitScreenEvent);
            DisableCommand = new DelegateCommand<object>(DisableEvent);
            SetViewModeCommand = new DelegateCommand(SetModeView);
            ContinueCommand = new DelegateCommand<object>(ContinueEvent);
            PagesBreakCommand = new DelegateCommand<object>(PagesBreakEvent);
            RotateCommand = new DelegateCommand<object>(RotateEvent);

            //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                NavigationParameters parameters = new NavigationParameters();
                parameters.Add(ParameterNames.PDFViewer, PDFViewer);
                region.RequestNavigate(RegionNames.ThemesContentName, "ThemesContent", parameters);
            }
            ));
        }

        /// <summary>
        /// 旋转
        /// </summary>
        /// <param name="obj"></param>
        private void RotateEvent(object obj)
        {
            bool right = Convert.ToBoolean(obj);

            PDFViewer.RotatePage(PageRotate.Rotate90, right, PDFViewer.CurrentIndex);

            //后续需要添加，缩略图旋转改变

            PDFViewer.UndoManager.CanSave = true;
        }

        /// <summary>
        /// 分页符
        /// </summary>
        /// <param name="obj"></param>
        private void PagesBreakEvent(object obj)
        {
            if (IsPagesBreak)
            {
                PDFViewer.SetPageSpacing(8);
                IsPagesBreak = true;
            }
            else
            {
                PDFViewer.SetPageSpacing(0);
                IsPagesBreak = false;
            }
            SaveMode(IsPagesBreak);
        }

        /// <summary>
        /// 设置显示模式
        /// </summary>
        private void SetModeView()
        {
            if (PDFViewer != null)
            {
                if (IsContinue)
                {
                    if (IsSingleView)
                    {
                        PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
                    }
                    else if (IsTwoPageView)
                    {
                        PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
                    }
                    else
                    {
                        PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
                    }
                }
                else
                {
                    if (IsSingleView)
                    {
                        PDFViewer.ChangeViewMode(ViewMode.Single);
                    }
                    else if (IsTwoPageView)
                    {
                        PDFViewer.ChangeViewMode(ViewMode.Double);
                    }
                    else
                    {
                        PDFViewer.ChangeViewMode(ViewMode.Book);
                    }
                }
            }
            SaveMode(PDFViewer.ModeView);
        }

        /// <summary>
        /// 连续滚动
        /// </summary>
        /// <param name="obj"></param>
        private void ContinueEvent(object obj)
        {
            SetModeView();
        }

        /// <summary>
        /// 保存模式到本地
        /// </summary>
        /// <param name="modeView"></param>
        private void SaveMode(object modeView)
        {
            OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
            if (fileInfo != null)
            {
                if (modeView is SplitMode)
                {
                    fileInfo.LastSplitMode = PDFViewer.Mode;
                }
                else if (modeView is ViewMode)
                {
                    fileInfo.LastViewMode = PDFViewer.ModeView;
                }
                else if (modeView is bool)
                {
                    fileInfo.LastPageSpace = (bool)modeView;
                }
                SettingHelper.SetFileInfo(fileInfo);
            }
        }

        /// <summary>
        /// 分屏视图-单屏
        /// </summary>
        /// <param name="obj"></param>
        private void DisableEvent(object obj)
        {
            SplitScreenViewVisible = Visibility.Collapsed;
        }

        /// <summary>
        /// 分屏视图-垂直
        /// </summary>
        /// <param name="obj"></param>
        private void VerticalSplitScreenEvent(object obj)
        {
            SplitScreenViewVisible = Visibility.Visible;
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            region.RequestNavigate(RegionNames.SplitScreenViewRegionName, "SplitScreenContent", param);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            CPDFViewer pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
            BottomToolContentViewModel btnTool = navigationContext.Parameters["BottomToolContentViewModel"] as BottomToolContentViewModel;
            if (pdfview != null && btnTool != null)
            {
                BottomToolContentViewModel = btnTool;
                PDFViewer = pdfview;
            }
            else
            {
                return;
            }
            
            IsSingleView = BottomToolContentViewModel.IsSingleView;
            IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
            IsBookModeView = BottomToolContentViewModel.IsBookMode;
            IsContinue = BottomToolContentViewModel.IsContinue;
            PDFViewer.InfoChanged += PDFViewer_InfoChanged;
        }

        /// <summary>
        /// 和底部工具栏联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
        {
            if (e.Key == "ViewMode")
            {
                GetModeView((ViewMode)e.Value);
            }
        }

        private void GetModeView(ViewMode mode)
        {
            if ((int)mode % 2 == 0)
            {
                if (!IsContinue)
                {
                    IsContinue = true;
                }
            }
            else
            {
                if (IsContinue)
                {
                    IsContinue = false;
                }
            }

            if ((int)mode <= 2)
            {
                if (!isSingleView)
                {
                    IsSingleView = true;
                }
            }
            else if ((int)mode <= 4)
            {
                if (!isTwoPageView)
                {
                    IsTwoPageView = true;
                }
            }
            else
            {
                if (!isBookModeView)
                {
                    IsBookModeView = true;
                }
            }
        }
    }
}