using ComPDFKit.PDFAnnotation;
using ComPDFKit.Tool;
using Compdfkit_Tools.Common;
using Compdfkit_Tools.Data;
using Compdfkit_Tools.PDFControl;
using ComPDFKitViewer;
using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace Compdfkit_Tools.PDFControlUI
{
    public partial class CPDFMarkupUI : UserControl
    {
        private CPDFAnnotationType currentAnnotationType;
        private AnnotParam annotParam;
        private CPDFMarkupAnnotation markupAnnot;
        private PDFViewControl viewControl;
        public event EventHandler<CPDFAnnotationData> PropertyChanged;

        public CPDFMarkupUI()
        {
            InitializeComponent();
            ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
            CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
        }

        private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetMarkupData());
            }
            else
            {
                if(markupAnnot!=null && markupAnnot.IsValid())
                {
                    markupAnnot.SetTransparency((byte)(CPDFOpacityControl.OpacityValue / 100.0 * 255));
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
        }

        private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetMarkupData());
            }
            else
            {
                SolidColorBrush colorBrush = ColorPickerControl.Brush as SolidColorBrush;
                if (markupAnnot != null && markupAnnot.IsValid() && colorBrush!=null)
                {
                    markupAnnot.SetColor(new byte[3]
                    {
                        colorBrush.Color.R,
                        colorBrush.Color.G,
                        colorBrush.Color.B
                    });
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
        }

        private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetMarkupData());
            }
            else
            {
                if (markupAnnot != null && markupAnnot.IsValid())
                {
                    markupAnnot.SetContent(NoteTextBox.Text);
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
        }

        public CPDFMarkupData GetMarkupData()
        {
            CPDFMarkupData pdfMarkupData = new CPDFMarkupData();
            pdfMarkupData.AnnotationType = currentAnnotationType;
            pdfMarkupData.Color = ((SolidColorBrush)ColorPickerControl.Brush).Color;
            pdfMarkupData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
            pdfMarkupData.Note = NoteTextBox.Text;
            return pdfMarkupData;
        }

        public void SetPresentAnnotAttrib(AnnotParam param,CPDFMarkupAnnotation markup,PDFViewControl view)
        {
            this.annotParam = null;
            this.markupAnnot = null;
            this.viewControl = null;

            if(param!=null)
            {
                if(param is HighlightParam)
                {
                    HighlightParam highlightParam = (HighlightParam)param;
                    Color newColor = Color.FromRgb(
                        highlightParam.HighlightColor[0],
                        highlightParam.HighlightColor[1],
                        highlightParam.HighlightColor[2]);

                    ColorPickerControl.Brush = new SolidColorBrush(newColor);
                    ColorPickerControl.SetCheckedForColor(newColor);
                }
               
                if(param is UnderlineParam)
                {
                    UnderlineParam underlineParam = (UnderlineParam)param;
                    Color newColor = Color.FromRgb(
                        underlineParam.UnderlineColor[0],
                        underlineParam.UnderlineColor[1],
                        underlineParam.UnderlineColor[2]);

                    ColorPickerControl.Brush = new SolidColorBrush(newColor);
                    ColorPickerControl.SetCheckedForColor(newColor);
                }

                if (param is StrikeoutParam)
                {
                    StrikeoutParam strikeoutParam = (StrikeoutParam)param;
                    Color newColor = Color.FromRgb(
                       strikeoutParam.StrikeoutColor[0],
                       strikeoutParam.StrikeoutColor[1],
                       strikeoutParam.StrikeoutColor[2]);

                    ColorPickerControl.Brush = new SolidColorBrush(newColor);
                    ColorPickerControl.SetCheckedForColor(newColor);
                }

                if(param is SquigglyParam)
                {
                    SquigglyParam squigglyParam= (SquigglyParam)param;
                    Color newColor = Color.FromRgb(
                      squigglyParam.SquigglyColor[0],
                      squigglyParam.SquigglyColor[1],
                      squigglyParam.SquigglyColor[2]);

                    ColorPickerControl.Brush = new SolidColorBrush(newColor);
                    ColorPickerControl.SetCheckedForColor(newColor);
                }
                CPDFOpacityControl.OpacityValue = (int)(param.Transparency / 255D * 100);
                NoteTextBox.Text = param.Content;
            }
          
            this.annotParam = param;
            this.markupAnnot = markup;
            this.viewControl = view;
          
            CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
        }

        public void InitWithAnnotationType(CPDFAnnotationType annotationType)
        {
            currentAnnotationType = annotationType;
            switch (annotationType)
            {
                case CPDFAnnotationType.Highlight:
                    TitleTextBlock.Text = "Highlight";
                    break;
                case CPDFAnnotationType.Underline:
                    TitleTextBlock.Text = "Underline";
                    break;
                case CPDFAnnotationType.Strikeout:
                    TitleTextBlock.Text = "Strikethrough";
                    break;
                case CPDFAnnotationType.Squiggly:
                    TitleTextBlock.Text = "Squiggly";
                    break;
                default:
                    throw new ArgumentException("Not Excepted Argument");
            }
            CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
        }
    }
}
