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 CPDFNoteUI : UserControl
    {
        public event EventHandler<CPDFAnnotationData> PropertyChanged;
        internal StickyNoteParam stickyParam { get;private set; }
        private CPDFTextAnnotation textAnnot;
        private PDFViewControl viewControl;

        public CPDFNoteUI()
        {
            InitializeComponent();
            ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
            CPDFAnnotationPreviewerControl.DrawNotePreview(GetNoteData());
        }

        private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (stickyParam == null)
            {
                PropertyChanged?.Invoke(this, GetNoteData());
            }
            else
            {
                SolidColorBrush colorBrush= ColorPickerControl.Brush as SolidColorBrush;
                if (textAnnot!=null && textAnnot.IsValid() && colorBrush!=null)
                {
                    textAnnot.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.DrawNotePreview(GetNoteData());
        }

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

        public void SetPresentAnnotAttrib(StickyNoteParam param,CPDFTextAnnotation annot,PDFViewControl view)
        {
            this.stickyParam = null;
            this.viewControl = null;
            this.textAnnot = null;

            if(param != null)
            {
                ColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
                    param.StickyNoteColor[0],
                    param.StickyNoteColor[1],
                    param.StickyNoteColor[2]));
                NoteTextBox.Text = param.Content;
                ColorPickerControl.SetCheckedForColor(Color.FromRgb(
                    param.StickyNoteColor[0],
                    param.StickyNoteColor[1],
                    param.StickyNoteColor[2]));
            }
           
            this.stickyParam = param;
            this.textAnnot=annot;
            this.viewControl = view;
        }

        public CPDFNoteData GetNoteData()
        {
            CPDFNoteData pdfNoteData = new CPDFNoteData();
            pdfNoteData.AnnotationType = CPDFAnnotationType.Note;
            pdfNoteData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
            pdfNoteData.Note = NoteTextBox.Text;
            return  pdfNoteData;
        }
    }
}
