using ComPDFKit.PDFAnnotation;
using ComPDFKit.Tool;
using Compdfkit_Tools.Common;
using Compdfkit_Tools.Data;
using Compdfkit_Tools.PDFControl;
using ComPDFKitViewer;
using ComPDFKitViewer.Annot;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Compdfkit_Tools.PDFControlUI
{
    public partial class CPDFShapeUI : UserControl
    {
        private CPDFAnnotationType currentAnnotationType;

        private AnnotParam annotParam;
        private CPDFAnnotation annotCore;
        private PDFViewControl viewControl;
        public event EventHandler<CPDFAnnotationData> PropertyChanged;

        public CPDFShapeUI()
        {
            InitializeComponent();
            BorderColorPickerControl.ColorChanged += BorderColorPickerControl_ColorChanged;
            CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
            CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
            CPDFLineStyleControl.LineStyleChanged += CPDFLineStyleControl_LineStyleChanged;

            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                SolidColorBrush borderBrush=BorderColorPickerControl.Brush as SolidColorBrush;
                if(annotCore!=null && annotCore.IsValid() && borderBrush!=null)
                {
                    if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
                    {
                        CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
                        squareAnnot?.SetLineColor(new byte[3]
                        {
                            borderBrush.Color.R,
                            borderBrush.Color.G,
                            borderBrush.Color.B
                        });
                    }
                   
                    if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
                    {
                        CPDFCircleAnnotation circleAnnot= annotCore as CPDFCircleAnnotation;
                        circleAnnot?.SetLineColor(new byte[3]
                        {
                            borderBrush.Color.R,
                            borderBrush.Color.G,
                            borderBrush.Color.B
                        });
                    }

                    if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                    {
                        CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
                        lineAnnot?.SetLineColor(new byte[3]
                        {
                            borderBrush.Color.R,
                            borderBrush.Color.G,
                            borderBrush.Color.B
                        });
                    }

                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                } 
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                SolidColorBrush fillBrush = FillColorPickerControl.Brush as SolidColorBrush;
                if (annotCore != null && annotCore.IsValid() && fillBrush != null)
                {
                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
                    {
                        CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
                        squareAnnot?.SetBgColor(new byte[3]
                        {
                            fillBrush.Color.R,
                            fillBrush.Color.G,
                            fillBrush.Color.B
                        });
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
                    {
                        CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
                        circleAnnot?.SetBgColor(new byte[3]
                        {
                            fillBrush.Color.R,
                            fillBrush.Color.G,
                            fillBrush.Color.B
                        });
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                    {
                        CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
                        lineAnnot?.SetBgColor(new byte[3]
                        {
                            fillBrush.Color.R,
                            fillBrush.Color.G,
                            fillBrush.Color.B
                        });
                    }

                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                
                if (annotCore != null && annotCore.IsValid())
                {
                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
                    {
                        CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
                        squareAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
                    {
                        CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
                        circleAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                    {
                        CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
                        lineAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
                    }

                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                double opacity = CPDFOpacityControl.OpacityValue / 100.0;
                if (opacity > 0 && opacity <= 1)
                {
                    opacity = opacity * 255;
                }
                if (annotCore != null && annotCore.IsValid())
                {
                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
                    {
                        CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
                        squareAnnot?.SetTransparency((byte)opacity);
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
                    {
                        CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
                        circleAnnot?.SetTransparency((byte)opacity);
                    }

                    if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                    {
                        CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
                        lineAnnot?.SetTransparency((byte)opacity);
                    }

                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                if (annotCore != null && annotCore.IsValid())
                {
                    float[] dashArray = null;
                    C_BORDER_STYLE borderStyle;
                    if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
                    {
                        dashArray = new float[0];
                        borderStyle = C_BORDER_STYLE.BS_SOLID;
                    }
                    else
                    {
                        List<float> floatArray = new List<float>();
                        foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
                        {
                            floatArray.Add((float)num);
                        }
                        dashArray = floatArray.ToArray();
                        borderStyle = C_BORDER_STYLE.BS_DASHDED;
                    }

                    if (dashArray != null)
                    {
                        if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
                        {
                            CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
                            squareAnnot?.SetBorderStyle(borderStyle, dashArray);
                        }

                        if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
                        {
                            CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
                            circleAnnot?.SetBorderStyle(borderStyle, dashArray);
                        }

                        if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                        {
                            CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
                            lineAnnot?.SetBorderStyle(borderStyle, dashArray);
                        }

                        if (viewControl != null && viewControl.PDFViewTool != null)
                        {
                            CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                            pdfViewer?.UpDataAnnotFrame();
                        }
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(PropertyChanged, GetShapeData());
            }
            else
            {
                if (annotCore != null && annotCore.IsValid())
                {
                    if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
                    {
                        CPDFLineAnnotation lineAnnot= annotCore as CPDFLineAnnotation;
                        if(lineAnnot!=null)
                        {
                            lineAnnot.SetLineType(CPDFArrowControl.LineType.HeadLineType, CPDFArrowControl.LineType.TailLineType);
                            if (viewControl != null && viewControl.PDFViewTool != null)
                            {
                                CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                                pdfViewer?.UpDataAnnotFrame();
                            }
                        }
                    }
                }
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (annotParam == null)
            {
                PropertyChanged?.Invoke(this, GetShapeData());
            }
            else
            {
                if (annotCore != null && annotCore.IsValid())
                {
                    annotCore.SetContent(NoteTextBox.Text);

                    if (viewControl != null && viewControl.PDFViewTool != null)
                    {
                        CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
                        pdfViewer?.UpDataAnnotFrame();
                    }
                }
            }
        }

        public CPDFAnnotationData GetShapeData()
        {
            if (currentAnnotationType == CPDFAnnotationType.Circle || currentAnnotationType == CPDFAnnotationType.Square)
            {
                CPDFShapeData pdfShapeData = new CPDFShapeData();
                pdfShapeData.AnnotationType = currentAnnotationType;
                pdfShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
                pdfShapeData.FillColor = ((SolidColorBrush)FillColorPickerControl.Brush).Color;
                pdfShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
                pdfShapeData.Thickness = CPDFThicknessControl.Thickness;
                pdfShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
                pdfShapeData.Note = NoteTextBox.Text;
                return pdfShapeData;
            }

            else
            {
                CPDFLineShapeData pdfLineShapeData = new CPDFLineShapeData();
                pdfLineShapeData.AnnotationType = currentAnnotationType;
                pdfLineShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
                pdfLineShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
                pdfLineShapeData.LineType = CPDFArrowControl.LineType;
                pdfLineShapeData.Thickness = CPDFThicknessControl.Thickness;
                pdfLineShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
                pdfLineShapeData.LineType = CPDFArrowControl.LineType;
                pdfLineShapeData.Note = NoteTextBox.Text;
                return pdfLineShapeData;
            }
        }

        public void SetPresentAnnotAttrib(AnnotParam param,CPDFAnnotation annot,PDFViewControl view)
        {
            this.annotParam = null;
            this.annotCore = null;
            this.viewControl = null;

            if(param != null)
            {
                if(param is SquareParam)
                {
                    SquareParam squareParam= (SquareParam)param;
                    CPDFThicknessControl.Thickness = (int)squareParam.LineWidth;

                    if (squareParam.LineColor != null)
                    {
                        BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
                            squareParam.LineColor[0],
                            squareParam.LineColor[1],
                            squareParam.LineColor[2]));
                        BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
                        squareParam.LineColor[0],
                        squareParam.LineColor[1],
                        squareParam.LineColor[2]));
                    }

                    if (squareParam.BgColor!=null)
                    {
                        FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
                           squareParam.BgColor[0],
                           squareParam.BgColor[1],
                           squareParam.BgColor[2]));
                    }
                    if (squareParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Solid;
                    }
                    else
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Dash;
                    }
                }

                if(param is CircleParam)
                {
                    CircleParam circleParam= (CircleParam)param;
                    CPDFThicknessControl.Thickness = (int)circleParam.LineWidth;
                    if (circleParam.LineColor != null)
                    {
                        BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
                           circleParam.LineColor[0],
                           circleParam.LineColor[1],
                           circleParam.LineColor[2]));
                        BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
                           circleParam.LineColor[0],
                           circleParam.LineColor[1],
                           circleParam.LineColor[2]));
                    }
                    if (circleParam.BgColor!=null)
                    {
                        FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
                           circleParam.BgColor[0],
                           circleParam.BgColor[1],
                           circleParam.BgColor[2]));
                    }
                    if (circleParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Solid;
                    }
                    else
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Dash;
                    }
                }

                if(param is LineParam)
                {
                    LineParam lineParam= (LineParam)param;
                    CPDFThicknessControl.Thickness = (int)lineParam.LineWidth;

                    if (lineParam.LineColor != null)
                    {
                        BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
                          lineParam.LineColor[0],
                          lineParam.LineColor[1],
                          lineParam.LineColor[2]));
                        BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
                       lineParam.LineColor[0],
                       lineParam.LineColor[1],
                       lineParam.LineColor[2]));
                    }
                    if (lineParam.BgColor != null)
                    {
                        FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
                       lineParam.BgColor[0],
                       lineParam.BgColor[1],
                       lineParam.BgColor[2]));
                    }
                    if(lineParam.BorderStyle==C_BORDER_STYLE.BS_SOLID)
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Solid;
                    }
                    else
                    {
                        CPDFLineStyleControl.DashStyle = DashStyles.Dash;
                    }

                    LineType lineType = new LineType()
                    {
                        HeadLineType = lineParam.HeadLineType,
                        TailLineType = lineParam.TailLineType
                    };
                    CPDFArrowControl.LineType = lineType;
                }

                CPDFOpacityControl.OpacityValue = (int)(param.Transparency/255 * 100);
                NoteTextBox.Text = param.Content;
            }

            this.annotParam = param;
            this.annotCore = annot;
            this.viewControl = view;
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }

        public void InitWhenRectAndRound()
        {
            FillColorStackPanel.Visibility = Visibility.Visible;
            ArrowStackPanel.Visibility = Visibility.Collapsed;

            FillColorPickerControl.ColorChanged += FillColorPickerControl_ColorChanged;
            CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
        }

        public void InitWhenArrowAndLine()
        {
            FillColorStackPanel.Visibility = Visibility.Collapsed;
            ArrowStackPanel.Visibility = Visibility.Visible;

            CPDFArrowControl.ArrowChanged += CPDFArrowControl_ArrowChanged;
            FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
            LineType lineType;

            if (currentAnnotationType == CPDFAnnotationType.Arrow)
            {
                lineType = new LineType()
                {
                    HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
                    TailLineType = C_LINE_TYPE.LINETYPE_ARROW
                };
            }
            else
            {
                lineType = new LineType()
                {
                    HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
                    TailLineType = C_LINE_TYPE.LINETYPE_NONE
                };
            }
            CPDFArrowControl.LineType = lineType;
        }


        public void InitWithAnnotationType(CPDFAnnotationType annotationType)
        {
            currentAnnotationType = annotationType;
            switch (annotationType)
            {
                case CPDFAnnotationType.Square:
                    TitleTextBlock.Text = "Rectangle";
                    InitWhenRectAndRound();
                    break;
                case CPDFAnnotationType.Circle:
                    TitleTextBlock.Text = "Circle";
                    InitWhenRectAndRound();
                    break;
                case CPDFAnnotationType.Arrow:
                    TitleTextBlock.Text = "Arrow";
                    InitWhenArrowAndLine();
                    break;
                case CPDFAnnotationType.Line:
                    TitleTextBlock.Text = "Line";
                    InitWhenArrowAndLine();
                    break;
                default:
                    throw new ArgumentException("Not Excepted Argument");
            }
            CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
        }
    }
}
