using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Prism.Commands;
using System.Windows.Controls;
using ComPDFKit.PDFAnnotation;
using PDF_Master.Helper;
using System.Diagnostics;
using PDF_Master.Model;
using PDF_Master.ViewModels.Tools;
using Microsoft.Office.Interop.Excel;
using Point = System.Windows.Point;
using static Dropbox.Api.UsersCommon.AccountType;
using PDF_Master.Model.AnnotPanel;
using PDFSettings;
using PDF_Master.ViewModels.Tools.AnnotManager;
using Prism.Common;

namespace PDF_Master.ViewModels.FillAndSign.PropertyPanel
{
    public class ShapFillPropertyViewModel : BindableBase, INavigationAware
    {
        public AnnotAttribEvent AnnotEvent { get; set; }
        private AnnotHandlerEventArgs Annot;
        private AnnotTransfer PropertyPanel;
        private FillAndSignContentViewModel fillAndSignContentViewModel;
        public List<List<Point>> updatapath = new List<List<Point>>();
        private Geometry dataPath = null;
        private bool IsFillAndSign = false;
        public string Shape = "";
        public Geometry DataPath
        {
            get { return dataPath; }
            set
            {
                SetProperty(ref dataPath, value);
            }
        }

        private void InitColorItems()
        {
        }

        private Brush selectColor = new SolidColorBrush(Colors.GreenYellow);
        public Brush SelectColor
        {
            get { return selectColor; }
            set
            {
                SetProperty(ref selectColor, value);
                if (!fillAndSignContentViewModel.isFirst)
                {
                    fillAndSignContentViewModel.SelectColor = value;
                    //if (FillAndSignContentViewModel.IsEdit)
                    {
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * lineWidthMultiple);
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, (selectColor as SolidColorBrush).Color);
                    }
                    //else
                    {
                        if(IsFillAndSign) { fillAndSignContentViewModel.SetStamp(); }
                        
                    }

                }

            }
        }

        private double fillOpacity = 1;
        public double FillOpacity
        {
            get { return fillOpacity; }
            set
            {
                SetProperty(ref fillOpacity, value);
                if (!fillAndSignContentViewModel.isFirst)
                {
                    fillAndSignContentViewModel.FillOpacity = value;

                    //if (FillAndSignContentViewModel.IsEdit)
                    {
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, fillOpacity);
                    }

                    //else
                    {
                        if (IsFillAndSign) { fillAndSignContentViewModel.SetStamp(); }
                        
                    }

                }

            }
        }

        private bool _isMultiSelected = false;
        public bool IsMultiSelected
        {
            get { return _isMultiSelected; }
            set => SetProperty(ref _isMultiSelected, value);
        }

        private bool _isSelected = false;
        public bool IsSelected
        {
            get { return _isSelected; }
            set => SetProperty(ref _isSelected, value);
        }
        private double lineWidthMultiple=1;
        private double lineWidth = 1;
        public double LineWidth
        {
            get { return lineWidth; }
            set
            {
                SetProperty(ref lineWidth, value);
                if (!fillAndSignContentViewModel.isFirst)
                {
                    fillAndSignContentViewModel.LineWidth = value;

                    //if (FillAndSignContentViewModel.IsEdit)
                    {

                        //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Height, 60);
                        //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Width, 60);
                        //if (Shape == "DotShape") { lineWidthMultiple = 5; }

                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * lineWidthMultiple);
                        //fillAndSignContentViewModel.LineWidthMultiple = lineWidthMultiple;
                         //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);

                    }
                    // else
                    {
                        if (IsFillAndSign) { fillAndSignContentViewModel.SetStamp(); }
                        
                    }

                }
            }
        }
        public DelegateCommand<object> SelectedThickCommand { get; set; }
        public DelegateCommand<object> SelectedColorCommand { 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 ShapFillPropertyViewModel()
        {
            SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
            SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
            //LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
            SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
            ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
            SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
        }

        private void InitFillAndSignProperty()
        {
            if (fillAndSignContentViewModel.isFirst)
            {
                SelectColor = fillAndSignContentViewModel.SelectColor;
                FillOpacity = fillAndSignContentViewModel.FillOpacity;
                LineWidth = fillAndSignContentViewModel.LineWidth;
            }

            fillAndSignContentViewModel.isFirst = false;
        }

        private DefaultAnnotProperty GetAnnotDefault(AnnotArgsType annotArgsType)
        {
            var Annot = SettingHelper.GetAnnotDefaultProperty(annotArgsType, "FillAndSign");
            if (Annot == null)
            {
                Annot = new DefaultAnnotProperty();
                Annot.AnnotToolType = annotArgsType;
            }
            return Annot;
        }

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


        private void SharpsType(string tag, object obj, bool isFromToolsBtn = false)
        {
            fillAndSignContentViewModel.LineWidthMultiple = 1;
            Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
            lineWidthMultiple = 1;
            //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Width, 60);
            //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Height, 60);
            switch (tag)
            {
                case "HookShape":
                    var hookShape = new PathGeometry();
                    hookShape.AddGeometry(Geometry.Parse("M0.599976 7.0286L5.57775 11.8L13.4 1.40002"));
                    DataPath = hookShape;
                    //if (FillAndSignContentViewModel.IsEdit)
                    {
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * fillAndSignContentViewModel.LineWidthMultiple);
                        updatapath = new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };

                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);
                        if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count >= 1)
                        {
                            foreach (var annot in PropertyPanel.annotlists)
                            {

                                foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                                {
                                    if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                                    {
                                        point.Shape = "HookShape";
                                    }
                                }
                            }
                        }
                    }
                    //PropertyPanel.UpdateAnnotAAttrib();
                    //fillAndSignContentViewModel.ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };
                    //fillAndSignContentViewModel.SetStamp();
                    //changeData[AnnotArgsType.AnnotLine] = tag;
                    break;

                case "ForkShape":
                    var forkShape = new PathGeometry();
                    forkShape.AddGeometry(Geometry.Parse("M3.19995 3.20001L12.8 12.8 M12.8 3.20001L3.20005 12.8"));
                    DataPath = forkShape;
                    // if (FillAndSignContentViewModel.IsEdit)
                    {
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * fillAndSignContentViewModel.LineWidthMultiple);
                        updatapath = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(12.8, 12.8) }, new List<Point> { new Point(12.8, 3.20001), new Point(3.20005, 12.8) } };
                        PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);
                        if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count >= 1)
                        {
                            foreach (var annot in PropertyPanel.annotlists)
                            {

                                foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                                {
                                    if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                                    {
                                        point.Shape = "ForkShape";
                                    }
                                }
                            }
                        }

                    }
                    //fillAndSignContentViewModel.ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(12.8, 12.8) }, new List<Point> { new Point(12.8, 3.20001), new Point(3.20005, 12.8) } };
                    //fillAndSignContentViewModel.SetStamp();
                    //changeData[AnnotArgsType.AnnotLine] = tag;
                    break;

                case "RectShape":
                    {
                        RectangleGeometry rectPath = new RectangleGeometry();
                        rectPath.Rect = new Rect(0, 5, 28, 22);
                        DataPath = rectPath;
                        //if (FillAndSignContentViewModel.IsEdit)
                        {
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * fillAndSignContentViewModel.LineWidthMultiple);
                            updatapath = new List<List<Point>> { new List<Point> { new Point(5, 5), new Point(28, 5) }, new List<Point> { new Point(28, 5), new Point(28, 27) }, new List<Point> { new Point(28, 27), new Point(5, 27) }, new List<Point> { new Point(5, 27), new Point(5, 5) } };
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);
                            if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count >= 1)
                            {
                                foreach (var annot in PropertyPanel.annotlists)
                                {

                                    foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                                    {
                                        if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                                        {
                                            point.Shape = "RectShape";
                                        }
                                    }
                                }
                            }
                        }
                        //fillAndSignContentViewModel.ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.19995, 5), new Point(28, 5), new Point(28, 27), new Point(0.19995, 27), new Point(0.19995, 5) } };
                        //fillAndSignContentViewModel.SetStamp();
                        // changeData[AnnotArgsType.AnnotSquare] = tag;
                    }

                    break;
                case "LineShape":
                    {
                        var lineShape = new PathGeometry();
                        lineShape.AddGeometry(Geometry.Parse(" M0,10L20,10"));
                        DataPath = lineShape;
                        // if (FillAndSignContentViewModel.IsEdit)
                        {
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * fillAndSignContentViewModel.LineWidthMultiple);
                            updatapath = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(28, 3.20001) } };
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);
                            if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count >= 1)
                            {
                                foreach (var annot in PropertyPanel.annotlists)
                                {

                                    foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                                    {
                                        if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                                        {
                                            point.Shape = "LineShape";
                                        }
                                    }
                                }
                            }
                        }
                        //fillAndSignContentViewModel.ShapePoints = new List<List<Point>> { new List<Point> { new Point(12.19995, 10), new Point(36, 10) } };
                        //fillAndSignContentViewModel.SetStamp();
                        //changeData[AnnotArgsType.AnnotLine] = tag;
                    }

                    break;

                case "DotShape":
                    {
                        fillAndSignContentViewModel.LineWidthMultiple = 5;
                        EllipseGeometry circlePath = new EllipseGeometry();
                        circlePath.RadiusX = 2.4;
                        circlePath.RadiusY = 2.4;
                        circlePath.Center = new Point(2.4, 2.4);
                        DataPath = circlePath;
                        // if (FillAndSignContentViewModel.IsEdit)
                        {
                            lineWidthMultiple = 5;
                            updatapath = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.19995), new Point(3.19995, 3.19995) } };
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, LineWidth * 5);
                            PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Path, updatapath);
                            if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count >= 1)
                            {
                                foreach (var annot in PropertyPanel.annotlists)
                                {

                                    foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                                    {
                                        if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                                        {
                                            point.Shape = "DotShape";
                                        }
                                    }
                                }
                            }
                        }
                        //fillAndSignContentViewModel.ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.19995, 1.19995), new Point(0.19995, -1.19995) }, new List<Point> { new Point(-0.25995, 0.89995), new Point(0.25995, -0.89995) }, new List<Point> { new Point(-1.19995, 0.19995), new Point(0.19995, 1.19995) } };
                        //fillAndSignContentViewModel.SetStamp();
                        //changeData[AnnotArgsType.AnnotCircle] = tag;
                    }

                    break;
            }

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

        }

        private void InitSharpsType(string tag)
        {
            fillAndSignContentViewModel.LineWidthMultiple = 1;
            switch (tag)
            {
                case "HookShape":
                    var hookShape = new PathGeometry();
                    hookShape.AddGeometry(Geometry.Parse("M0.599976 7.0286L5.57775 11.8L13.4 1.40002"));
                    DataPath = hookShape;
                    break;

                case "ForkShape":
                    var forkShape = new PathGeometry();
                    forkShape.AddGeometry(Geometry.Parse("M3.19995 3.20001L12.8 12.8 M12.8 3.20001L3.20005 12.8"));
                    DataPath = forkShape;
                    break;

                case "RectShape":
                    {
                        RectangleGeometry rectPath = new RectangleGeometry();
                        rectPath.Rect = new Rect(0, 5, 28, 22);
                        DataPath = rectPath;
                    }

                    break;
                case "LineShape":
                    {
                        var lineShape = new PathGeometry();
                        lineShape.AddGeometry(Geometry.Parse(" M0,10L20,10"));
                        DataPath = lineShape;
                    }

                    break;

                case "DotShape":
                    {
                        fillAndSignContentViewModel.LineWidthMultiple = 5;
                        EllipseGeometry circlePath = new EllipseGeometry();
                        circlePath.RadiusX = 2.4;
                        circlePath.RadiusY = 2.4;
                        circlePath.Center = new Point(2.4, 2.4);
                        DataPath = circlePath;
                    }

                    break;
            }

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

        }

        private void SelectedThick_Command(object obj)
        {
            if (obj is double)
            {
                var tran = (double)obj;
                //BorderOpacity = tran;
                SelectColor.Opacity = tran;
                //AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, tran);
                //AnnotEvent?.UpdateAnnot();
            }
        }
        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);
                    LineWidth = intData;
                }
            }
        }

        private void SelectedColor_Command(object obj)
        {
            if (obj != null)
            {
                var colorValue = (Color)obj;
                if (colorValue != null)
                {

                    SelectColor = new SolidColorBrush(colorValue);
                    SelectColor.Opacity = FillOpacity;

                    Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                    changeData[AnnotArgsType.AnnotFreehand] = obj;
                    //PropertyPanel.DataChangedInvoke(this, changeData);
                }

            }

        }

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

                //AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
                //AnnotEvent?.UpdateAnnot();
                Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
                changeData[AnnotArgsType.AnnotFreehand] = FillOpacity;
                //PropertyPanel.DataChangedInvoke(this, changeData);
            }

        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            string shape = "";
            navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
            navigationContext.Parameters.TryGetValue<FillAndSignContentViewModel>("FillAndSignContentViewModel", out fillAndSignContentViewModel);
            navigationContext.Parameters.TryGetValue<string>("Shape", out shape);
            bool isFillAndSign = false;
            navigationContext.Parameters.TryGetValue<bool>("IsFillAndSign", out isFillAndSign);
            IsFillAndSign = isFillAndSign;
            InitSharpsType(shape);
            //FillAndSignContentViewModel.IsEdit = false;
            if (PropertyPanel != null)
            {
                AnnotEvent = PropertyPanel.AnnotEvent;
                Annot = PropertyPanel.annot;
                var stampAnnotArgs = Annot as StampAnnotArgs;
                if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
                {
                    IsMultiSelected = true;
                }
                else
                {
                    IsMultiSelected = false;
                }

                IsSelected = !FillAndSignContentViewModel.IsEdit;


                InitFillAndSignProperty();

                if (IsMultiSelected)
                {

                }
                else
                {
                    GetAnnotProperty(shape);
                }

            }
        }

        private void GetAnnotProperty(string shape)
        {
            if (Annot is FreehandAnnotArgs)
            {
                var annot = Annot as FreehandAnnotArgs;
                if (annot != null)
                {
                    //SelectColor = new SolidColorBrush(annot.InkColor);
                    //FillOpacity = annot.Transparency;
                    foreach (var point in fillAndSignContentViewModel.viewContentViewModel.FillAndSign)
                    {
                        if (annot.PageIndex == point.PageIndex && annot.AnnotIndex == point.AnnotIndex)
                        {
                            lineWidthMultiple = 1;
                            if (point.Shape == "DotShape") {
                                lineWidthMultiple = 5;
                            }
                            InitSharpsType(point.Shape);
                            Shape=point.Shape;
                        }
                    }
                }

            }
        }
    }
}