using ComPDFKit.PDFDocument;
using ComPDFKit.Tool;
using ComPDFKitViewer;
using Nager.Country.Currencies;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Compdfkit_Tools.PDFControl
{
    public partial class PDFViewControl : UserControl
    {
        static CPDFViewerTool ViewerTool = null;
        static CPDFToolManager pDFToolManager = null;
        public CPDFToolManager PDFToolManager = null;
        #region Properties
        public CPDFViewerTool PDFViewTool { get; set; }

        public bool CustomSignHandle { get; set; }

        private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
        #endregion
        public PDFViewControl()
        {
            InitializeComponent();
            if (ViewerTool==null)
            {
                PDFViewTool = ViewerTool= new CPDFViewerTool();
                PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
                PDFViewTool.SizeChanged += PDFViewTool_SizeChanged;
                if (pDFToolManager == null)
                {
                    PDFToolManager= pDFToolManager = new CPDFToolManager(PDFViewTool);
                }
                else
                {
                    pDFToolManager.Remove();
                    PDFToolManager= pDFToolManager = new CPDFToolManager(PDFViewTool);
                }
            }
            else
            {
                PDFViewTool = ViewerTool;
                PDFToolManager = pDFToolManager;
            }
            Content = PDFViewTool;
            //PDFToolManager.SetToolType(CPDFToolManager.ToolType.Viewer);
        }

        private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            PDFViewTool.GetCPDFViewer().UpDataRenderFrame();
        }

        public void InitDocument(string Path)
        {
            CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
            if (pdfDoc != null)
            {
                PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);

                PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
            }
        }

        private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                e.IsZoom = true;
                PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
                double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
                PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
                PDFViewTool.GetCPDFViewer().UpDataRenderFrame();
            }
        }

        #region Private Command Methods
        private double CheckZoomLevel(double zoom, bool IsGrowth)
        {
            zoom += (IsGrowth ? 0.01 : -0.01);
            double standardZoom = 100;
            if (zoom <= 0.01)
            {
                return 0.01;
            }
            if (zoom >= 10)
            {
                return 10;
            }

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


    }
}
