using Compdfkit_Tools.PDFControlUI;
using ComPDFKitViewer;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Compdfkit_Tools.PDFControl
{
    public partial class CPDFDisplaySettingsControl : UserControl
    {
        /// <summary>
        /// PDFViewer
        /// </summary>
        private PDFViewControl ViewControl;

        public CPDFDisplaySettingsControl()
        {
            InitializeComponent();
            Loaded += CPDFDisplaySettingsControl_Loaded;
        }

        private void CPDFDisplaySettingsControl_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModeUI.ViewModeChanged += ViewModeUI_ViewModeChanged;
            //ViewModeUI.SplitModeChanged += ViewModeUI_SplitModeChanged;
            ViewModeUI.CropModeChanged += ViewModeUI_CropModeChanged;
            DrawModeUI.DrawModeChanged += DrawModeUI_DrawModeChanged;
        }

        private void ViewModeUI_CropModeChanged(object sender, bool e)
        {
            //ViewControl?.SetCropMode(e);
        }

        private void DrawModeUI_DrawModeChanged(object sender, DrawModeData e)
        {
            if(ViewControl!=null && ViewControl.PDFViewTool!=null)
            {
                CPDFViewer pdfViewer=ViewControl.PDFViewTool.GetCPDFViewer();
                if(pdfViewer!=null)
                {
                    if (e.DrawMode != DrawModess.Custom)
                    {
                        pdfViewer?.SetDrawModes(e.DrawMode);
                        pdfViewer?.UpDataRenderFrame();
                    }
                    else
                    {
                        pdfViewer?.SetDrawModes(e.DrawMode);
                        pdfViewer?.SetPDFBackground(e.CustomColor);
                        pdfViewer?.UpDataRenderFrame();
                    }
                }
            }
        }

        //private void ViewModeUI_SplitModeChanged(object sender, SplitMode e)
        //{
        //    ViewControl?.SetSplitMode(e);
        //}

        private void ViewModeUI_ViewModeChanged(object sender, ViewModes e)
        {
            if (ViewControl != null && ViewControl.PDFViewTool != null)
            {
                CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
                if (pdfViewer != null)
                {
                    pdfViewer?.SetViewMode(e);
                }
            }   
        }

        public void InitWithPDFViewer(PDFViewControl viewControl)
        {
            ViewControl = viewControl;
            if(ViewControl != null && ViewControl.PDFViewTool!=null)
            {
               CPDFViewer pdfViewer=  viewControl.PDFViewTool.GetCPDFViewer();
                if(pdfViewer != null)
                {
                    ViewModeUI.SetViewModeUI(pdfViewer.GetViewMode());
                    // ViewModeUI.SetSplitModeUI(ViewControl.Mode);
                    //ViewModeUI.SetCropUI(ViewControl.IsCropMode());
                }
            }
        }

        public void SetVisibilityWhenContentEdit(Visibility visible)
        {
            ViewModeUI?.SetSplitContainerVisibility(visible);
            ViewModeUI?.SetCropContainerVisibility(visible);
        }
    }
}
