using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
{
    public class ReadViewContentViewModel : BindableBase, INavigationAware
    {
        public string ReadRegionName { get; set; }
        public string ReadModeRegionName { get; set; }

        public CPDFViewer PDFViewer { get; set; }

        private bool canSave;

        /// <summary>
        /// 是否可以保存
        /// </summary>
        public bool CanSave
        {
            get { return canSave; }
            set
            {
                SetProperty(ref canSave, value);
            }
        }

        private bool canUndo;

        /// <summary>
        /// 是否可以进行Undo
        /// </summary>
        public bool CanUndo
        {
            get { return canUndo; }
            set
            {
                SetProperty(ref canUndo, value);
            }
        }

        private bool canRedo;

        /// <summary>
        /// 是否可以进行Redo
        /// </summary>
        public bool CanRedo
        {
            get { return canRedo; }
            set
            {
                SetProperty(ref canRedo, value);
            }
        }

        private Visibility readModeContentVisibility = Visibility.Collapsed;

        public Visibility ReadModeContentVisibility
        {
            get { return readModeContentVisibility; }
            set
            {
                SetProperty(ref readModeContentVisibility, value);
            }
        }

        private Visibility rectangleReadModeVisibility = Visibility.Collapsed;

        public Visibility RectangleReadModeVisibility
        {
            get { return rectangleReadModeVisibility; }
            set
            {
                SetProperty(ref rectangleReadModeVisibility, value);
            }
        }

        public IRegionManager region;

        public IDialogService dialogs;

        /// <summary>
        /// 鼠标滚轮缩放的缩放值
        /// </summary>
        private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };

        public DelegateCommand Load { get; set; }

        public ReadViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            region = regionManager;
            dialogs = dialogService;
            ReadRegionName = RegionNames.ReadRegionName;
            ReadModeRegionName = RegionNames.ReadModePageRegionName;
            ReadModeContentVisibility = Visibility.Visible;
            RectangleReadModeVisibility = Visibility.Collapsed;
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
            if (pdfview != null)
            {
                PDFViewer = pdfview;
                LoadFile();
            }
        }

        /// <summary>
        /// 将PDFViwer添加到Region
        /// </summary>
        private void LoadFile()
        {
            PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
            PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
            CanSave = PDFViewer.UndoManager.CanSave;
            CanUndo = PDFViewer.UndoManager.CanUndo;
            CanRedo = PDFViewer.UndoManager.CanRedo;

            if (region.Regions.ContainsRegionWithName(ReadRegionName))
            {
                if (region.Regions[ReadRegionName].Views.Contains(PDFViewer))
                {
                    var contentRegion = region.Regions[ReadRegionName];
                    contentRegion.Remove(PDFViewer);
                }
                region.AddToRegion(ReadRegionName, PDFViewer);
            }

            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                NavigationParameters parameters = new NavigationParameters();
                parameters.Add(ParameterNames.PDFViewer, PDFViewer);
                parameters.Add(ParameterNames.ViewContentViewModel, this);

                region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
            }
             ));
        }

        public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
        {
            double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
            PDFViewer.Zoom(newZoom);
        }

        private double CheckZoomLevel(double zoom, bool IsGrowth)
        {
            double standardZoom = 100;
            if (zoom <= 0.01)
            {
                return 0.01;
            }
            if (zoom >= 10)
            {
                return 10;
            }

            zoom *= 100;
            for (int i = 0; i < zoomLevel.Length - 1; i++)
            {
                if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
                {
                    standardZoom = zoomLevel[i + 1];
                    break;
                }
                if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
                {
                    standardZoom = zoomLevel[i];
                    break;
                }
            }
            return standardZoom / 100;
        }

        private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //不处于页面编辑模式下时，根据PDFVIewer的undo redo状态来更新按钮状态
            //页面编辑模式下，按钮状态根据页面编辑的undo redo来显示
            if (e.PropertyName == "CanUndo")
            {
                CanUndo = PDFViewer.UndoManager.CanUndo;
            }
            if (e.PropertyName == "CanRedo")
            {
                CanRedo = PDFViewer.UndoManager.CanRedo;
            }
            if (e.PropertyName == "CanSave")
            {
                CanSave = PDFViewer.UndoManager.CanSave;
            }
        }
    }
}