using ComPDFKit.PDFAnnotation;
using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Properties;
using PDF_Office.ViewModels.PropertyPanel;
using PDF_Office.Views.PropertyPanel.AnnotPanel;
using PDFSettings;
using Prism.Commands;
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.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace PDF_Office.ViewModels.Tools
{
    public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
    {
        public AnnotToolContentViewModel(IRegionManager regionManager)
        {
            region = regionManager;

            MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
            PropertyRegionName = Guid.NewGuid().ToString();
            BindingEvent();
            InitDefaultValue();
        }

        private Dictionary<string, bool> ToolExpandDict = new Dictionary<string, bool>();

        public void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
        {
            //不创建注释，属于注释模板
            bool isTemplateAnnot = false;
            AnnotHandlerEventArgs annotArgs = null;

            if (annotBtn.IsChecked == true)
            {
                switch (annotBtn.Tag.ToString())
                {
                    case "SnapshotEdit":
                        break;

                    case "HighLight":
                        annotArgs = GetHighLight();
                        break;

                    case "UnderLine":
                        annotArgs = GetUnderLine();
                        break;

                    case "Squiggly":
                        annotArgs = GetSquiggly();
                        break;

                    case "Strikeout":
                        annotArgs = GetStrikeout();
                        break;

                    case "Freehand":
                        annotArgs = GetFreehand();
                        break;

                    case "Freetext":
                        annotArgs = GetFreetext();
                        break;

                    case "StickyNote":
                        annotArgs = GetStickyNote();
                        break;

                    case "Rect":
                        annotArgs = GetRect();
                        break;

                    case "Circle":
                        annotArgs = GetCircle();
                        break;

                    case "Arrow":
                    case "Line":
                        annotArgs = GetArrowLine(annotBtn.Tag.ToString());
                        break;

                    case "Stamp":
                        annotArgs = GetStamp();
                        isTemplateAnnot = true;
                        break;

                    case "Image":
                        annotArgs = GetImage(annotBtn);
                        break;

                    case "Signature":
                        annotArgs = GetSignature();
                        isTemplateAnnot = true;
                        break;

                    case "Link":
                        annotArgs = GetLink();
                        break;
                }

                if (annotArgs != null)
                {
                    annotArgs.Author = Settings.Default.AppProperties.Description.Author;
                    PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
                    PDFViewer.SetToolParam(annotArgs);
                }
            }

            //当不是注释模板，且无创建注释时，属性面板显示为空内容
            if (isTemplateAnnot == false && annotArgs == null)
            {
                PDFViewer.SetMouseMode(MouseModes.PanTool);
                viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
            }

            ShowPropertyPanel((bool)annotBtn.IsChecked);
        }

        private void AnnotProperty_DefaultStored(object sender, object e)
        {
        }

        private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
        {
            if (e != null)
            {
                AnnotHandlerEventArgs annotArgs = null;
                foreach (AnnotArgsType argsType in e.Keys)
                {
                    switch (argsType)
                    {
                        case AnnotArgsType.AnnotSquare:
                            annotArgs = GetRect();
                            break;

                        case AnnotArgsType.AnnotCircle:
                            annotArgs = GetCircle();
                            break;

                        case AnnotArgsType.AnnotLine:
                            var LineTag = e[argsType] as string;
                            annotArgs = GetArrowLine(LineTag);
                            break;
                    }
                    if (annotArgs != null)
                    {
                        annotArgs.Author = Settings.Default.AppProperties.Description.Author;
                        PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
                        PDFViewer.SetToolParam(annotArgs);
                    }
                    ShowPropertyPanel();
                }
            }
        }

        private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
        {
            if (e != null)
            {
                foreach (AnnotArgsType argsType in e.Keys)
                {
                    switch (argsType)
                    {
                        case AnnotArgsType.AnnotHighlight:
                            if (e[argsType] is Color)
                            {
                                HighLightColor = new SolidColorBrush((Color)e[argsType]);
                            }
                            if (e[argsType] is double)
                            {
                                HighLightOpacity = (double)e[argsType];
                            }
                            break;

                        case AnnotArgsType.AnnotUnderline:
                            if (e[argsType] is Color)
                            {
                                UnderLineColor = new SolidColorBrush((Color)e[argsType]);
                            }
                            if (e[argsType] is double)
                            {
                                underLineOpacity = (double)e[argsType];
                            }
                            break;

                        case AnnotArgsType.AnnotSquiggly:
                            if (e[argsType] is Color)
                            {
                                SquigglyColor = new SolidColorBrush((Color)e[argsType]);
                            }
                            if (e[argsType] is double)
                            {
                                SquigglyOpacity = (double)e[argsType];
                            }
                            break;

                        case AnnotArgsType.AnnotStrikeout:
                            if (e[argsType] is Color)
                            {
                                StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
                            }
                            if (e[argsType] is double)
                            {
                                StrikeoutOpacity = (double)e[argsType];
                            }
                            break;

                        case AnnotArgsType.AnnotFreehand:
                            if (e[argsType] is Color)
                            {
                                // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
                            }
                            if (e[argsType] is double)
                            {
                                //  FreehandPath.Opacity = (double)e[argsType];
                            }
                            break;

                        case AnnotArgsType.AnnotErase:
                            if (e[argsType] is ToggleButton)
                            {
                                ToggleButton clickBtn = e[argsType] as ToggleButton;
                                //PDFViewerCtrl viewCtrl = GetCurrentViewer();
                                // ClearSelectedToolPanel(BtnFreeHand);
                                //     if (viewCtrl != null && viewCtrl.PdfViewer != null)
                                {
                                    if (clickBtn.IsChecked == true)
                                    {
                                        if (clickBtn.Tag.ToString() == "PenBtn")
                                        {
                                            CustomIconToggleBtn btn = new CustomIconToggleBtn();
                                            btn.Tag = "Freehand"; btn.IsChecked = true;
                                            BtnMyTools_Click(btn);
                                            break;
                                        }
                                        EraseArgs eraseArgs = new EraseArgs();
                                        eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
                                        eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
                                        eraseArgs.Thickness = 10;
                                        DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
                                        if (annotProperty != null)
                                        {
                                            eraseArgs.Thickness = annotProperty.Thickness;
                                        }

                                        PDFViewer.ClearSelectAnnots(false);
                                        PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
                                        PDFViewer.SetToolParam(eraseArgs);

                                        Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
                                        annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
                                        annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
                                        annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
                                        AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
                                        //清空选中注释
                                        //viewCtrl.PdfViewer.ClearSelectAnnots(false);
                                        //viewCtrl.PdfViewer.SetMouseMode(MouseModes.AnnotCreate);
                                        //viewCtrl.PdfViewer.SetToolParam(eraseArgs);
                                        //if (viewCtrl.PropPanel.Children.Count > 0)
                                        //{
                                        //    FreehandAnnotProperty freehandPropPanel = viewCtrl.PropPanel.Children[0] as FreehandAnnotProperty;
                                        //    if (freehandPropPanel == null)
                                        //    {
                                        //        viewCtrl.PropPanel.Children.Clear();
                                        //        freehandPropPanel = new FreehandAnnotProperty();
                                        //        freehandPropPanel.EraseSet = true;
                                        //        freehandPropPanel.SetInkThickness(eraseArgs.Thickness);
                                        //        freehandPropPanel.DataChanged += AnnotPropertyPanel_DataChanged;
                                        //        viewCtrl.PropPanel.Children.Add(freehandPropPanel);
                                        //    }
                                        //    Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
                                        //    annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
                                        //    annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
                                        //    annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
                                        //    freehandPropPanel.AnnotEvent = null;
                                        //    freehandPropPanel.SetEraseMode(true);
                                        //    freehandPropPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
                                        //}
                                        //else
                                        //{
                                        //    FreehandAnnotProperty freehandProperty = new FreehandAnnotProperty();
                                        //    freehandProperty.DataChanged += AnnotPropertyPanel_DataChanged;
                                        //    Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
                                        //    annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
                                        //    annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
                                        //    annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
                                        //    freehandProperty.SetInkThickness(eraseArgs.Thickness);
                                        //    freehandProperty.EraseSet = true;
                                        //    freehandProperty.SetEraseMode(true);
                                        //    freehandProperty.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
                                        //    viewCtrl.AddAnnotPropertyPanel(freehandProperty);

                                        //}
                                    }
                                    else
                                    {
                                        //  BtnFreeHand.IsChecked = true;
                                        //   BtnTool_Click(BtnFreeHand, new RoutedEventArgs());
                                    }
                                }
                            }
                            break;
                    }
                }
            }
        }

        public IRegionManager region;
        public string PropertyRegionName { get; set; }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);

            //  navigationContext.Parameters.TryGetValue<PropertyPanelContentViewModel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanelContentViewModel);

            if (PDFViewer != null)
            {
                PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
                PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;

                PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
                PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;

                PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
                PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;

                PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
                PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
            }
        }

        private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
        {
        }

        private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
        {
        }

        private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
        {
            if (e != null)
            {
                var annot = e.AnnotItemsList[0];
                if (annot != null)
                {
                    switch (annot.EventType)
                    {
                        case AnnotArgsType.AnnotHighlight:
                            e.IsAnnotCreateReset = false;
                            GetHighLight(annot as TextHighlightAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotUnderline:
                            GetUnderLine(annot as TextUnderlineAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotStrikeout:
                            GetStrikeout(annot as TextStrikeoutAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotSquiggly:
                            GetSquiggly(annot as TextSquigglyAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotFreehand:
                            GetFreehand(annot as FreehandAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotFreeText:
                            GetFreetext(annot as FreeTextAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotSquare:
                            GetRect(annot as SquareAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotCircle:
                            GetCircle(annot as CircleAnnotArgs);
                            break;

                        case AnnotArgsType.AnnotLine:
                            bool isLine = true;
                            if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
                            {
                                if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_NONE)
                                {
                                    isLine = false;
                                }
                            }
                            if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
                            {
                                if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_NONE)
                                {
                                    isLine = false;
                                }
                            }

                            if (isLine)
                                GetArrowLine("Line", annot as LineAnnotArgs);
                            else
                                GetArrowLine("Arrow", annot as LineAnnotArgs);

                            break;

                        case AnnotArgsType.AnnotLink:
                            GetLink(annot as LinkAnnotArgs, e);
                            break;
                    }

                    ShowPropertyPanel();
                }
            }
            else
            {
                // PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
                //  viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
            }
        }

        private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
        {
            if (e.AnnotEventArgsList == null)
                return;

            // var annotlist = e.AnnotEventArgsList;
            switch (e.CommandType)
            {
                case CommandType.Context:
                    if (e.AnnotEventArgsList.Count > 0)
                    {
                        e.PopupMenu = SelectAnnotContextMenu(sender);
                        if (e.PopupMenu != null)
                        {
                            e.Handle = true;
                        }
                    }
                    else
                    {
                        e.PopupMenu = NoneSelectAnnotContextMenu(sender);
                        if (e.PopupMenu != null)
                        {
                            e.Handle = true;
                        }
                    }

                    break;
            }
        }

        private ContextMenu SelectAnnotContextMenu(object sender)
        {
            var popMenu = App.Current.FindResource("SelectAnnotContextMenu") as ContextMenu;
            if (popMenu != null && popMenu.Items.Count == 4)
            {
                //剪切
                MenuItem menuItem = popMenu.Items[0] as MenuItem;
                menuItem.CommandTarget = (UIElement)sender;
                menuItem.Command = ApplicationCommands.Cut;
                //复制
                menuItem = popMenu.Items[1] as MenuItem;
                menuItem.CommandTarget = (UIElement)sender;
                menuItem.Command = ApplicationCommands.Copy;
                //删除
                menuItem = popMenu.Items[2] as MenuItem;
                menuItem.CommandTarget = (UIElement)sender;
                menuItem.Command = ApplicationCommands.Delete;

                //笔记
                //menuItem = popMenu.Items[2] as MenuItem;
                //menuItem.CommandTarget = (UIElement)sender;
                //menuItem.Command = ApplicationCommands.no;
            }
            return popMenu;
        }

        private ContextMenu NoneSelectAnnotContextMenu(object sender)
        {
            var popMenu = new ContextMenu();

            MenuItem menuItem = new MenuItem();
            menuItem.CommandTarget = (UIElement)sender;
            menuItem.Command = ApplicationCommands.Paste;
            popMenu.Items.Add(menuItem);

            menuItem = new MenuItem();
            menuItem.CommandTarget = (UIElement)sender;
            menuItem.Command = ApplicationCommands.Copy;
            popMenu.Items.Add(menuItem);

            return popMenu;
        }

        /// <summary>
        /// 展开显示属性面板
        /// </summary>
        private void ShowPropertyPanel(bool show = true)
        {
            viewContentViewModel.IsPropertyOpen = show;
        }
    }
}