using ComPDFKit.PDFAnnotation;
using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using Microsoft.Office.Interop.Word;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.BOTA;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Annotations;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;

namespace PDF_Office.ViewModels.Dialog.BOTA
{
    public class AddAnnotationDialogViewModel : BindableBase, IDialogAware
    {
        private string title;

        public string Title
        {
            get { return title; }
            set { SetProperty(ref title, value); }
        }

        public event Action<IDialogResult> RequestClose;

        private string markupContent;

        public string MarkupContent
        {
            get { return markupContent; }
            set
            {
                SetProperty(ref markupContent, value);
            }
        }

        private string annotationName;

        public string AnnotationName
        {
            get { return annotationName; }
            set
            {
                SetProperty(ref annotationName, value);
            }
        }

        public AnnotationHandlerEventArgs Annotation { get; set; }
        public AnnotAttribEvent AnnotEvent { get; set; }

        public DelegateCommand CreateCommnad { get; set; }
        public DelegateCommand CancelCommand { get; set; }

        public DelegateCommand<Object> LostFocusCommand { get; set; }

        public AddAnnotationDialogViewModel()
        {
            CancelCommand = new DelegateCommand(CancelEvent);
            CreateCommnad = new DelegateCommand(CreateEvent);
            LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
        }

        private void LostFocusEvent(object obj)
        {
            if (obj is CompositeCommandParameter composite)
            {
                if (composite.Parameter is TextBox textBox)
                {
                    Annotation.MarkupContent = textBox.Text;
                    Annotation.AnnotHandlerEventArgs.MarkupContent = textBox.Text;
                }
            }
        }

        private void CreateEvent()
        {
            DialogParameters valuePairs = new DialogParameters();
            valuePairs.Add(ParameterNames.Annotation, Annotation);
            valuePairs.Add(ParameterNames.AnnotEvent, AnnotEvent);
            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
        }

        private void CancelEvent()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            AnnotationHandlerEventArgs annotation;
            parameters.TryGetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation, out annotation);

            string title = "1";

            if (annotation != null)
            {
                Annotation = annotation;

                Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();

                switch (annotation.EventType)
                {
                    case AnnotArgsType.AnnotFreeText://文本
                        title = "文本";
                        //FreeTextAnnotArgs freetextArgs = annotation.AnnotHandlerEventArgs as FreeTextAnnotArgs; ;
                        //annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
                        //annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
                        //annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
                        //annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
                        //annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
                        //annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
                        //annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
                        //annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
                        //annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
                        //annotAttribsList[AnnotAttrib.TextAlign] = TextAlignment.Left;
                        //annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
                        //AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(freetextArgs, annotAttribsList);
                        break;

                    case AnnotArgsType.AnnotHighlight:
                        title = "高亮";
                        break;

                    case AnnotArgsType.AnnotFreehand:
                        title = "手绘";
                        break;

                    case AnnotArgsType.AnnotSquiggly://波浪线
                        title = "波浪线";
                        break;

                    case AnnotArgsType.AnnotStamp:
                        title = "图章";
                        break;

                    case AnnotArgsType.AnnotStrikeout://删除线
                        title = "删除线";
                        break;

                    case AnnotArgsType.AnnotSticky://便签
                        break;

                    case AnnotArgsType.AnnotUnderline:
                        title = "下划线";
                        break;

                    case AnnotArgsType.AnnotLine:
                        if ((annotation.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (annotation.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
                        {
                            title = "箭头";
                        }
                        else
                        {
                            title = "线";
                        }
                        break;

                    case AnnotArgsType.AnnotSquare:
                        title = "矩形";
                        break;

                    case AnnotArgsType.AnnotCircle:
                        title = "圆";
                        break;
                }
                if (!string.IsNullOrEmpty(annotation.MarkupContent))
                {
                    title = title + "-" + annotation.MarkupContent;
                }
                Title = title;
                MarkupContent = annotation.MarkupContent;
                AnnotationName = "页面" + (annotation.PageIndex + 1).ToString();
                return;
            }
        }
    }
}