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;
using System.Windows.Controls;
using System.Windows.Media;
using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;

namespace Compdfkit_Tools.PDFControlUI
{
    public partial class CPDFFreeTextUI : UserControl
    {
        public event EventHandler<CPDFAnnotationData> PropertyChanged;
        private CPDFFreeTextAnnotation textAnnot;
        private FreeTextParam textParam;
        private PDFViewControl viewControl;
        public CPDFFreeTextUI()
        {
            InitializeComponent();
            ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
            CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
            CPDFFontControl.FontFamilyChanged += CPDFFontControl_FontFamilyChanged;
            CPDFFontControl.FontStyleChanged += CPDFFontControl_FontStyleChanged;
            CPDFFontControl.FontAlignChanged += CPDFFontControl_FontAlignChanged;
            CPDFFontControl.FontSizeChanged += CPDFFontControl_FontSizeChanged;
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

        private void CPDFFontControl_FontSizeChanged(object sender, EventArgs e)
        {
            if (textAnnot == null)
            {
                PropertyChanged?.Invoke(this, GetFreeTextData());
            }
            else
            {
                if(textAnnot!=null &&textAnnot.IsValid())
                {
                    CTextAttribute textAttr = textAnnot.FreeTextDa;
                    textAttr.FontSize = CPDFFontControl.FontSizeValue;
                    textAnnot.SetFreetextDa(textAttr);
                    textAnnot.UpdateAp();
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        if (pdfViewer != null)
                        {
                            pdfViewer.UpDataAnnotFrame();
                        }
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

        private void CPDFFontControl_FontAlignChanged(object sender, EventArgs e)
        {
            if (textAnnot == null)
            {
                PropertyChanged?.Invoke(this, GetFreeTextData());
            }
            else
            {
                if (textAnnot != null && textAnnot.IsValid())
                {
                   switch(CPDFFontControl.TextAlignment)
                    {
                        case TextAlignment.Left:
                            textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
                            break;
                        case TextAlignment.Center:
                            textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
                            break;
                        case TextAlignment.Right:
                            textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
                            break;
                        default:
                            break;
                    }
                    textAnnot.UpdateAp();
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        if (pdfViewer != null)
                        {
                            pdfViewer.UpDataAnnotFrame();
                        }
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

        private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
        {
            if (textAnnot == null)
            {
                PropertyChanged?.Invoke(this, GetFreeTextData());
            }
            else
            {
                if (textAnnot != null && textAnnot.IsValid())
                {
                    CTextAttribute textAttr = textAnnot.FreeTextDa;
                    FontType fontType = GetFontType(textAttr.FontName);
                    textAttr.FontName = ObtainFontName(fontType, CPDFFontControl.IsBold, CPDFFontControl.IsItalic);
                    textAnnot.SetFreetextDa(textAttr);

                    textAnnot.UpdateAp();
                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        if (pdfViewer != null)
                        {
                            pdfViewer.UpDataAnnotFrame();
                        }
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

        private void CPDFFontControl_FontFamilyChanged(object sender, EventArgs e)
        {
            if (textAnnot == null)
            {
                PropertyChanged?.Invoke(this, GetFreeTextData());
            }
            else
            {
                CTextAttribute textAttr = textAnnot.FreeTextDa;
                bool isBold = IsBold(textAttr.FontName);
                bool isItalic = IsItalic(textAttr.FontName);
                FontType fontType = GetFontType(CPDFFontControl.FontFamilyValue);
                textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
                textAnnot.SetFreetextDa(textAttr);
                textAnnot.UpdateAp();
                if (viewControl != null && viewControl.PDFViewTool != null)
                {
                    CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                    if (pdfViewer != null)
                    {
                        pdfViewer.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

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

        private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (textAnnot == null)
            {
                PropertyChanged?.Invoke(this, GetFreeTextData());
            }
            else
            {
                Color fontColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
                CTextAttribute textAttr = textAnnot.FreeTextDa;
                textAttr.FontColor = new byte[3]
                {
                    fontColor.R,
                    fontColor.G, 
                    fontColor.B,
                };
                textAnnot.SetFreetextDa(textAttr);
                textAnnot.UpdateAp();
                if (viewControl != null && viewControl.PDFViewTool != null)
                {
                    CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                    if (pdfViewer != null)
                    {
                        pdfViewer.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
        }

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

        public void SetPresentAnnotAttrib(FreeTextParam textParam,CPDFFreeTextAnnotation annot,PDFViewControl view)
        {
            this.textAnnot = null;
            if(textParam!=null)
            {
                ColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
                    textParam.FontColor[0],
                    textParam.FontColor[1],
                    textParam.FontColor[2]));
                CPDFOpacityControl.OpacityValue = (int)(textParam.Transparency / 255D * 100);
                CPDFFontControl.FontFamilyValue = textParam.FontName;
                CPDFFontControl.FontSizeValue = (int)textParam.FontSize;
                CPDFFontControl.IsBold = textParam.IsBold;
                CPDFFontControl.IsItalic = textParam.IsItalic;
                switch(textParam.Alignment)
                {
                    case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
                        CPDFFontControl.TextAlignment =TextAlignment.Left;
                        break;
                    case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
                        CPDFFontControl.TextAlignment=TextAlignment.Center;
                        break;
                    case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
                        CPDFFontControl.TextAlignment = TextAlignment.Right;
                        break;
                    default:
                        break;
                }
                NoteTextBox.Text = textParam.Content;
                ColorPickerControl.SetCheckedForColor(Color.FromRgb(
                    textParam.FontColor[0],
                    textParam.FontColor[1],
                    textParam.FontColor[2]));
            }
           
            this.textAnnot = annot;
            this.textParam = textParam;
            this.viewControl = view;
        }

        public CPDFFreeTextData GetFreeTextData()
        {
            CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
            pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
            pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
            pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
            pdfFreeTextData.FontFamily = CPDFFontControl.FontFamilyValue;
            pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
            pdfFreeTextData.IsBold = CPDFFontControl.IsBold;
            pdfFreeTextData.IsItalic = CPDFFontControl.IsItalic;
            pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
            pdfFreeTextData.Note = NoteTextBox.Text;
            return pdfFreeTextData;
        }
    }
}
