using ComPDFKit.PDFAnnotation;
using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.AnnotPanel;
using PDF_Office.ViewModels.Tools;
using PDFSettings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
{
    public class DashStyleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DashStyle)
            {
                var dash = value as DashStyle;
                if (dash.Dashes == null || dash.Dashes.Count == 0 || dash.Dashes[0] == 0)
                {
                    return DashStyles.Solid.Dashes;
                }
                else
                {
                    DashStyle dashx = new DashStyle();
                    dashx.Dashes.Add(1);
                    dashx.Dashes.Add(1);
                    return dashx.Dashes;
                }
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class SharpsAnnotPropertyViewModel : BindableBase, INavigationAware
    {
        #region 属性
        private AnnotCommon _basicVm = new AnnotCommon();
        public AnnotCommon BasicVm
        {
            get { return _basicVm; }
            set => SetProperty(ref _basicVm, value);
        }

        private Geometry dataPath = null;
        public Geometry DataPath
        {
            get { return dataPath; }
            set
            {
                SetProperty(ref dataPath, value);
            }
        }

        private DashStyle dash = new DashStyle();
        public DashStyle Dash
        {
            get { return dash; }
            set
            {
                SetProperty(ref dash, value);
            }
        }

        private bool _isLineAnnot = false;
        public bool IsLineAnnot
        {
            get { return _isLineAnnot; }
            set
            {
                SetProperty(ref _isLineAnnot, value);
            }
        }

        private void UpdateDash(AnnotAttribEvent attribEvent)
        {
            if (Dash.Dashes != null && Dash.Dashes.Count > 0)
            {

                if (Dash.Dashes[0] == 0)
                {
                    attribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
                }
                else
                {
                    attribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dash);
                }

            }
            else
            {
                attribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
            }

            attribEvent?.UpdateAnnot();
        }

        #endregion

        public DelegateCommand<object> SelectedThickCommand { get; set; }
        public DelegateCommand<object> SelectedBorderColorCommand { get; set; }

        public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
        public DelegateCommand<object> SelectedFillColorCommand { get; set; }

        public DelegateCommand<object> LineStyleCommand { get; set; }

        public DelegateCommand<object> SharpsTypeCommand { get; set; }
        public DelegateCommand<object> ThicknessChangedCommand { get; set; }
        public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }


        public event EventHandler<object> LoadPropertyHandler;
        public SharpsAnnotPropertyViewModel()
        {
            InitColorItems();
            InitFillColorItems();

            SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
            SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity_Command);
            SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);

            SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
            SelectedBorderColorCommand = new DelegateCommand<object>(SelectedBorderColor);
            SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);

            ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
            LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
        }

        private void InitColorItems()
        {
            BasicVm.ColorItems = AnnotColorList.GetColorList(ColorSelectorType.Border);
        }

        private void InitFillColorItems()
        {
            BasicVm.FillColorItems = AnnotColorList.GetColorList(ColorSelectorType.Fill);
        }

        private void ThicknessChanged_Command(object obj)
        {
            if (obj != null)
            {
                var item = (ComboBoxItem)obj;
                var content = (string)item.Content;
                if (content != null)
                {
                    var intData = int.Parse(content);
                    BasicVm.AnnotThickness = intData;
                }
            }
        }

        private void SharpsType_Command(object obj)
        {
            if (obj != null)
            {
                var tag = (string)obj;
                SharpsType(tag);
            }
        }

        private void SharpsType(string tag, bool isFromToolsBtn = false)
        {
            Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
            switch (tag)
            {
                case "Rect":
                    RectangleGeometry rectPath = new RectangleGeometry();
                    rectPath.Rect = new Rect(0, 5, 28, 22);
                    DataPath = rectPath;
                    changeData[AnnotArgsType.AnnotSquare] = tag;
                    IsLineAnnot = false;
                    break;

                case "Circle":
                    EllipseGeometry circlePath = new EllipseGeometry();
                    circlePath.RadiusX = 14;
                    circlePath.RadiusY = 14;
                    circlePath.Center = new Point(14, 14);
                    DataPath = circlePath;
                    changeData[AnnotArgsType.AnnotCircle] = tag;
                    IsLineAnnot = false;
                    break;

                case "Arrow":
                    {

                        ArrowHelper arrowLine = new ArrowHelper();
                        arrowLine.ArrowLength = 8;
                        arrowLine.LineStart = new Point(8, 24);
                        arrowLine.LineEnd = new Point(24, 8);
                        arrowLine.StartSharp = C_LINE_TYPE.LINETYPE_NONE;
                        arrowLine.EndSharp = C_LINE_TYPE.LINETYPE_ARROW;
                        DataPath = arrowLine.BuildArrowBody();
                        changeData[AnnotArgsType.AnnotLine] = tag;
                        IsLineAnnot = true;
                        //  changeData[AnnotArgsType.AnnotLine] = tag;
                    }

                    break;

                case "Line":
                    {
                        ArrowHelper arrowLine = new ArrowHelper();
                        arrowLine.LineStart = new Point(0, 32);
                        arrowLine.LineEnd = new Point(32, 0);
                        DataPath = arrowLine.BuildArrowBody();
                        changeData[AnnotArgsType.AnnotLine] = tag;
                        IsLineAnnot = true;
                    }

                    break;
            }

            if (isFromToolsBtn == false)
                PropertyPanel.AnnotTypeChangedInvoke(this, changeData);

            BasicVm.SetOtherTag(tag);
        }

        private void LineStyle_Command(object obj)
        {
            if (obj != null)
            {
                var tag = obj as string;
                if (tag == "Solid")
                {
                    DashStyle dashAnnot = new DashStyle();
                    dashAnnot.Dashes.Add(0);
                    dashAnnot.Dashes.Add(0);
                    Dash = dashAnnot;
                    PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
                }
                else
                {
                    DashStyle dashAnnot = new DashStyle();
                    dashAnnot.Dashes.Add(1);
                    dashAnnot.Dashes.Add(1);
                    Dash = dashAnnot;
                    PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, dash);
                }

                if (BasicVm.IsMultiSelected == false)
                    PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
            }
        }



        private void SelectedFillColor_Command(object obj)
        {
            if (obj != null && PropertyPanel != null)
            {
                var colorValue = (Color)obj;
                if (colorValue != null)
                {
                    BasicVm.FillColor = new SolidColorBrush(colorValue);
                    BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
                    PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);

                    if (BasicVm.IsMultiSelected == false)
                        PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
                }
            }


        }

        private void SelectedFillOpacity_Command(object obj)
        {
            if (obj != null)
            {
                BasicVm.FillOpacity = (double)obj;
                BasicVm.FillColor.Opacity = BasicVm.FillOpacity;

                PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
                PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);

            }

        }

        private void SelectedOpacityValue(object obj)
        {
            if (obj != null)
            {
                BasicVm.FillOpacity = (double)obj;
                BasicVm.FillColor.Opacity = BasicVm.FillOpacity;

                PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
                PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);
            }

        }



        private void SelectedBorderColor(object obj)
        {

            if (obj != null && PropertyPanel != null)
            {
                var colorValue = (Color)obj;
                if (colorValue != null)
                {
                    BasicVm.BorderColor = new SolidColorBrush(colorValue);
                    BasicVm.BorderColor.Opacity = BasicVm.BorderOpacity;
                    PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);

                    if (BasicVm.IsMultiSelected == false)
                        PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
                }
            }

        }

        private void SelectedThick_Command(object obj)
        {
            if (obj is double)
            {
                var tran = (double)obj;
                BasicVm.AnnotThickness = tran;
                PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, tran);

                if (BasicVm.IsMultiSelected == false)
                    PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            BasicVm.IsMultiSelected = false;
        }

        private void SetAnnotType()
        {
            switch (BasicVm.AnnotType)
            {
                case AnnotArgsType.AnnotCircle:
                    BasicVm.AnnotTypeTitle = "圆";
                    break;
                case AnnotArgsType.AnnotSquare:
                    BasicVm.AnnotTypeTitle = "矩形";
                    break;
                case AnnotArgsType.AnnotLine:

                    var annotLine = Annot as LineAnnotArgs;
                    if (annotLine != null)
                    {
                        if (annotLine.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annotLine.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
                            BasicVm.AnnotTypeTitle = "箭头";
                        else
                            BasicVm.AnnotTypeTitle = "线";
                    }
                    break;
            }

        }

        public AnnotAttribEvent AnnotEvent { get; set; }
        private AnnotHandlerEventArgs Annot;
        private AnnotPropertyPanel PropertyPanel;
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
            if (PropertyPanel != null)
            {
                AnnotEvent = PropertyPanel.AnnotEvent;
                BasicVm.AnnotType = PropertyPanel.annot.EventType;
                Annot = PropertyPanel.annot;
                BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
                if (BasicVm.IsMultiSelected)
                {
                    IsAttributeEquals();
                }
                else
                {
                    GetAnnotProperty();
                }

                LoadPropertyHandler?.Invoke(null, Annot);
            }

        }

        private List<SquareAnnotArgs> ConvertLists()
        {
            List<SquareAnnotArgs> FreeTextLists = new List<SquareAnnotArgs>();
            foreach (var item in PropertyPanel.annotlists)
            {
                var itemFreeText = item as SquareAnnotArgs;
                if (itemFreeText != null)
                {
                    FreeTextLists.Add(itemFreeText);
                }
            }

            if (FreeTextLists.Count != PropertyPanel.annotlists.Count)
                return null;
            else
                return FreeTextLists;
        }
        private void IsAttributeEquals()
        {
            var list = ConvertLists();
            if (list != null)
            {
                var temp = list[0];
                Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
                isNoEqualsDir.Add("FillColor", false);
                isNoEqualsDir.Add("LineColor", false);
                isNoEqualsDir.Add("Thickness", false);
                isNoEqualsDir.Add("ThickSolidDashStyle", false);

                foreach (var item in list)
                {
                    if (item == list[0])
                        continue;

                    if (isNoEqualsDir["FillColor"] == false)
                    {
                        if (temp.BgColor.A != item.BgColor.A || temp.BgColor.R != item.BgColor.R || temp.BgColor.G != item.BgColor.G || temp.BgColor.B != item.BgColor.B)
                        {
                            BasicVm.FillColor = new SolidColorBrush(Colors.Transparent);
                            isNoEqualsDir["FillColor"] = true;
                        }
                    }

                    if (isNoEqualsDir["LineColor"] == false)
                    {
                        if (temp.LineColor.A != item.LineColor.A || temp.LineColor.R != item.LineColor.R || temp.LineColor.G != item.LineColor.G || temp.LineColor.B != item.LineColor.B)
                        {
                            BasicVm.BorderColor = new SolidColorBrush(Colors.Transparent);
                            isNoEqualsDir["LineColor"] = true;
                        }
                    }


                    if (isNoEqualsDir["Thickness"] == false)
                    {
                        isNoEqualsDir["Thickness"] = true;
                        if (temp.LineWidth > item.LineWidth)
                        {
                            BasicVm.AnnotThickness = temp.LineWidth;
                        }
                        else
                        {
                            BasicVm.AnnotThickness = item.LineWidth;
                        }
                    }

                    if (isNoEqualsDir["ThickSolidDashStyle"] == false)
                    {
                        if (IsSolidStyle(temp) != IsSolidStyle(item))
                        {
                            isNoEqualsDir["ThickSolidDashStyle"] = true;
                        }
                    }
                }

                ////以下是多选注释的属性相等时
                if (isNoEqualsDir["FillColor"] == false)
                {
                    BasicVm.FillColor = new SolidColorBrush(temp.BgColor);

                }

                if (isNoEqualsDir["LineColor"] == false)
                {
                    BasicVm.BorderColor = new SolidColorBrush(temp.LineColor);

                }

                if (isNoEqualsDir["Thickness"] == false)
                {
                    BasicVm.AnnotThickness = temp.LineWidth;
                }

                if (isNoEqualsDir["ThickSolidDashStyle"] == true)
                {
                    var isSolid = IsSolidStyle(temp);
                    BasicVm.IsSolidLine = isSolid;
                    BasicVm.IsDashLine = !isSolid;
                }
                else
                {
                    BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
                }

            }
        }


        private bool IsSolidStyle(AnnotHandlerEventArgs annot)
        {
            bool isSolid = true;
            if (annot is SquareAnnotArgs)
            {
                var Square = Annot as SquareAnnotArgs;
                isSolid = AnnotPropertyPanel.IsSolidStyle(Square.LineDash);
            }
            else if (annot is CircleAnnotArgs)
            {
                var Circle = Annot as CircleAnnotArgs;
                isSolid = AnnotPropertyPanel.IsSolidStyle(Circle.LineDash);
            }
            else if (annot is LineAnnotArgs)
            {
                var line = Annot as LineAnnotArgs;
                isSolid = AnnotPropertyPanel.IsSolidStyle(line.LineDash);
            }

            return isSolid;
        }

        private void GetAnnotProperty()
        {
            if (Annot != null)
            {
                bool isSolid = true;
                switch (Annot.EventType)
                {
                    case AnnotArgsType.AnnotSquare:
                        if (Annot is SquareAnnotArgs)
                        {
                            var Square = Annot as SquareAnnotArgs;
                            BasicVm.BorderColor = new SolidColorBrush(Square.LineColor);
                            BasicVm.FillColor = new SolidColorBrush(Square.BgColor);
                            BasicVm.BorderOpacity = Square.Transparency;
                            BasicVm.FillOpacity = Square.Transparency;
                            BasicVm.AnnotThickness = Square.LineWidth;
                            Dash = Square.LineDash;
                            SharpsType("Rect", true);
                            BasicVm.AnnotTypeTitle = "矩形";
                            IsLineAnnot = false;
                        }
                        break;

                    case AnnotArgsType.AnnotCircle:
                        if (Annot is CircleAnnotArgs)
                        {
                            var Circle = Annot as CircleAnnotArgs;
                            BasicVm.BorderColor = new SolidColorBrush(Circle.LineColor);
                            BasicVm.FillColor = new SolidColorBrush(Circle.BgColor);
                            BasicVm.BorderOpacity = Circle.Transparency;
                            BasicVm.FillOpacity = Circle.Transparency;
                            BasicVm.AnnotThickness = Circle.LineWidth;
                            Dash = Circle.LineDash;
                            SharpsType("Circle", true);
                            BasicVm.AnnotTypeTitle = "圆";
                            IsLineAnnot = false;
                        }
                        break;

                    case AnnotArgsType.AnnotLine:
                        if (Annot is LineAnnotArgs)
                        {
                            var line = Annot as LineAnnotArgs;
                            BasicVm.BorderColor = new SolidColorBrush(line.LineColor);
                            BasicVm.FillColor = new SolidColorBrush(line.LineColor);
                            BasicVm.BorderOpacity = line.Transparency;
                            BasicVm.FillOpacity = line.Transparency;
                            BasicVm.AnnotThickness = line.LineWidth;
                            Dash = line.LineDash;

                            if (line.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && line.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
                            {
                                SharpsType("Arrow", true);
                                BasicVm.AnnotTypeTitle = "箭头";
                            }
                            else
                            {
                                SharpsType("Line", true);
                                BasicVm.AnnotTypeTitle = "线条";
                            }

                            IsLineAnnot = true;

                        }

                        break;
                }

                isSolid = IsSolidStyle(Annot);
                BasicVm.IsSolidLine = isSolid;
                BasicVm.IsDashLine = !isSolid;
            }

        }


    }
}