using ComPDFKitViewer;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Compdfkit_Tools.PDFControl
{
    public partial class CPDFScalingControl : UserControl
    {
        public PDFViewControl ViewControl;

        public CPDFScalingControl()
        {
            InitializeComponent();
        }

        public void InitWithPDFViewer(PDFViewControl viewControl)
        {
            ViewControl = viewControl;
        }

        private void PDFScalingControl_Loaded(object sender, RoutedEventArgs e)
        {
            CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent;
            CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent;
            CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent;
            CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent;
        }

        private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e)
        {
            CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
            CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
            CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
            CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
        }

        private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e)
        {
            if (ViewControl == null || ViewControl.PDFViewTool == null)
            {
                return;
            }
            CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
            if (pdfViewer == null)
            {
                return;
            }
         
            if (pdfViewer.GetZoom() < 3)
            {
                double newZoom = Math.Max(0.01, pdfViewer.GetZoom() - 0.1);
                pdfViewer.SetZoom(newZoom);
                pdfViewer.UpDataRenderFrame();
            }
            else if (pdfViewer.GetZoom() < 6)
            {
                pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2);
                pdfViewer.UpDataRenderFrame();
            }
            else if (pdfViewer.GetZoom() >6)
            {
                pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3);
                pdfViewer.UpDataRenderFrame();
            }
            SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
        }

        private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
        {
            if (ViewControl == null || ViewControl.PDFViewTool == null)
            {
                return;
            }
            CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
            if (pdfViewer == null)
            {
                return;
            }

            if (pdfViewer.GetZoom() < 3)
            {
                pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1);
                pdfViewer.UpDataRenderFrame();
            }
            else if (pdfViewer.GetZoom() < 6)
            {
                pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2);
                pdfViewer.UpDataRenderFrame();
            }
            else if (pdfViewer.GetZoom() <= 10)
            {
                double newZoom = Math.Max(10, pdfViewer.GetZoom() + 0.3);
                pdfViewer.SetZoom(newZoom);
                pdfViewer.UpDataRenderFrame();
            }
            SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom()* 100)));
        }

        private void PDFScalingControl_SetScaleEvent(object sender, string e)
        {
            if (ViewControl == null || ViewControl.PDFViewTool == null)
            {
                return;
            }
            CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
            if (pdfViewer == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(e))
            {
                pdfViewer.SetZoom(double.Parse(e) / 100);
                pdfViewer.UpDataRenderFrame();
                SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
            }
        }

        private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
        {
            if (ViewControl == null || ViewControl.PDFViewTool == null)
            {
                return;
            }
            CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
            if (pdfViewer == null)
            {
                return;
            }
          
            if (e == "Actual size")
            {
                pdfViewer.SetFitMode(FitModes.FitOriginal);
            }
            else if (e == "Suitable width")
            {
                pdfViewer.SetFitMode(FitModes.FitWidth);
            }
            else if (e == "Single page size")
            {
                pdfViewer.SetFitMode(FitModes.FitHeight);
            }
            else
            {
                pdfViewer.SetZoom(double.Parse(e) / 100);
            }
            SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
            pdfViewer.UpDataRenderFrame();
        }

        public void SetZoomTextBoxText(string value)
        {
            CPDFScalingUI.SetZoomTextBoxText(value);
        }
         
        private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
        {
            if (ViewControl == null || ViewControl.PDFViewTool == null)
            {
                return;
            }
            CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
            if (pdfViewer == null)
            {
                return;
            }

            SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
        }
    }
}
