using ComPDFKit.PDFAnnotation;
using ComPDFKit.PDFAnnotation.Form;
using ComPDFKit.PDFDocument;
using ComPDFKit.Tool;
using ComPDFKit.Tool.Help;
using Compdfkit_Tools.Data;
using Compdfkit_Tools.PDFControl;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Compdfkit_Tools.PDFControlUI
{
    public partial class CPDFFreehandUI : UserControl
    {
        public event EventHandler<CPDFAnnotationData> PropertyChanged;
        public event EventHandler<bool> EraseClickHandler;
        public event EventHandler<double> EraseChangeHandler;

        private InkParam inkParam = null;
        private CPDFInkAnnotation cPDFAnnotation = null;
        private PDFViewControl pdfViewerControl = null;
        private CPDFDocument cPDFDocument = null;
        public CPDFFreehandUI()
        {
            InitializeComponent();
            ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
            CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
            CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
            CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
            EraseThickness.ThicknessChanged += EraseThickness_ThicknessChanged;
        }

        private void EraseThickness_ThicknessChanged(object sender, EventArgs e)
        {
            EraseChangeHandler?.Invoke(this, EraseThickness.Thickness);
            EraseCircle.Width = EraseThickness.Thickness * 6;
            EraseCircle.Height = EraseThickness.Thickness * 6;
        }

        private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
        {
            if (cPDFAnnotation == null)
            {
                PropertyChanged?.Invoke(this, GetFreehandData());
            }
            else
            {
                cPDFAnnotation.SetTransparency(Convert.ToByte( CPDFOpacityControl.OpacityValue / 100.0));
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
            CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());

        }

        private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
        {
            if (cPDFAnnotation == null)
            {
                PropertyChanged?.Invoke(this, GetFreehandData());
            }
            else
            {
                cPDFAnnotation.SetThickness(CPDFThicknessControl.Thickness);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
            CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());

        }

        private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (cPDFAnnotation == null)
            {
                PropertyChanged?.Invoke(this, GetFreehandData());

            }
            else
            {
                byte[] Color = new byte[3];
                Color[0] = ((SolidColorBrush)ColorPickerControl.Brush).Color.R;
                Color[1] = ((SolidColorBrush)ColorPickerControl.Brush).Color.G;
                Color[2] = ((SolidColorBrush)ColorPickerControl.Brush).Color.B;
                cPDFAnnotation.SetInkColor(Color);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
            CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());

        }

        private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cPDFAnnotation == null)
            {
                PropertyChanged?.Invoke(this, GetFreehandData());
            }
            else
            {
                byte[] Color = new byte[3];
                Color[0] = ((SolidColorBrush)ColorPickerControl.Brush).Color.R;
                Color[1] = ((SolidColorBrush)ColorPickerControl.Brush).Color.G;
                Color[2] = ((SolidColorBrush)ColorPickerControl.Brush).Color.B;
                cPDFAnnotation.SetContent(NoteTextBox.Text);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        public void SetPresentAnnotAttrib(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
        {
            inkParam = (InkParam)annotParam;
            cPDFAnnotation = (CPDFInkAnnotation)annotation;
            pdfViewerControl = cPDFViewer;
            cPDFDocument = doc;

            ColorPickerControl.Brush = new SolidColorBrush(ParamConverter.ConverterByteForColor(inkParam.InkColor));
            CPDFOpacityControl.OpacityValue = (int)(inkParam.Transparency * 100);
            CPDFThicknessControl.Thickness = Convert.ToInt16(inkParam.Thickness);
            ColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(inkParam.InkColor));
            NoteTextBox.Text = inkParam.Content;
        }

        public CPDFFreehandData GetFreehandData()
        {
            CPDFFreehandData pdfFreehandData = new CPDFFreehandData();
            pdfFreehandData.AnnotationType = CPDFAnnotationType.Freehand;
            pdfFreehandData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
            pdfFreehandData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
            pdfFreehandData.Thickness = CPDFThicknessControl.Thickness;
            pdfFreehandData.Note = NoteTextBox.Text;
            return pdfFreehandData;
        }

        public void SetEraseCheck(bool isCheck)
        {
            if(isCheck)
            {
                FreehandBtn.IsChecked = false;
                EraseBtn.IsChecked = true;
                FreehandPanel.Visibility = Visibility.Collapsed;
                ErasePanel.Visibility = Visibility.Visible;
                CPDFAnnotationPreviewerControl.Visibility = Visibility.Collapsed;
                EraseCirclePanel.Visibility = Visibility.Visible;
            }
            else
            {
                FreehandBtn.IsChecked = true;
                EraseBtn.IsChecked = false;
                FreehandPanel.Visibility = Visibility.Visible;
                ErasePanel.Visibility = Visibility.Collapsed;
                CPDFAnnotationPreviewerControl.Visibility = Visibility.Visible;
                EraseCirclePanel.Visibility=Visibility.Collapsed;
            }
        }

        internal void ClearAnnotAttribEvent()
        {
            cPDFAnnotation = null;
        }

        internal int GetEraseThickness()
        {
            return EraseThickness.Thickness;
        }

        private void FreehandBtn_Click(object sender, RoutedEventArgs e)
        {
            SetEraseCheck(false);
            EraseClickHandler?.Invoke(this, false);
        }

        private void EraseBtn_Click(object sender, RoutedEventArgs e)
        {
            SetEraseCheck(true);
            EraseClickHandler?.Invoke(this, true);
        }
    }
}
