using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComPDFKitViewer.PdfViewer;
using Prism.Regions;
using DryIoc;
using System.Diagnostics;

namespace PDF_Office.ViewModels
{

    public class ViewContentViewModel : BindableBase, INavigationAware
    {
        public DelegateCommand LoadFile { get; set; }

        public DelegateCommand Load { get; set; }

        private CPDFViewer PDFViewer { get; set; }

        private MainContentViewModel mainViewModel { get; set; }

        public IRegionManager region;

        public string ViwerRegionName { get; set; }

        public string BOTARegionName { get; set; }

        public string PropertyRegionName { get; set; }

        private bool isOpenFile = false;

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

        public ViewContentViewModel(IRegionManager regionManager)
        {
            LoadFile = new DelegateCommand(loadFile);
            region = regionManager;

            ViwerRegionName = Guid.NewGuid().ToString();
            BOTARegionName = Guid.NewGuid().ToString();
            PropertyRegionName = Guid.NewGuid().ToString();

 
        }

        private void LoadControl()
        {
            //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                region.RequestNavigate(BOTARegionName, "BOTAContent");
            }
            ));
        }

        private void loadFile()
        {
            if (mainViewModel == null || string.IsNullOrEmpty(mainViewModel.FileName))
            {
                return;
            }
            string filepath = mainViewModel.FilePath;
            PDFViewer = new CPDFViewer();
            PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
            PDFViewer.InitDocument(filepath);
            PDFViewer.Load();

            mainViewModel.PDFViewer = PDFViewer;
            if(App.mainWindowViewModel!=null)
            {
                App.mainWindowViewModel.CurrentPDFViewer = PDFViewer;
            }
            App.OpenedFileList.Add(filepath);
            region.AddToRegion(ViwerRegionName, PDFViewer);
        }

        private 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;
        }


        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (isOpenFile)
                return;

            var mainVM = navigationContext.Parameters["MainViewModel"] as MainContentViewModel;
            if (mainVM != null)
            {
                mainViewModel = mainVM;
            }

            loadFile();
            isOpenFile = true;
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
