using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using PDF_Office.ViewModels.Tools;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using Prism.Commands;
using PDF_Office.Helper;
using Microsoft.Office.Interop.Word;
using static Dropbox.Api.Files.FileCategory;
using PDF_Office.Views.PropertyPanel.AnnotPanel;
using System.Windows.Controls;
using PDF_Office.CustomControl;
using System.Windows.Markup;
using ComPDFKitViewer;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Net;
using System.Diagnostics;
using static Dropbox.Api.TeamLog.TrustedTeamsRequestState;
using System.Windows.Input;

namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
{
    internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
    {
        public AnnotAttribEvent AnnotAttribEvent { get; set; }

        private AnnotPropertyPanel PropertyPanel;
        private AnnotArgsType annotType;

        private LinkAnnotArgs annotArgs;

        public LinkAnnotArgs AnnotArgs
        {
            get
            {
                return annotArgs;
            }
            set
            {
                if (annotArgs != null)
                {
                    annotArgs.LinkDrawFinished -= annotArgs_LinkDrawFinished;
                }

                annotArgs = value;

                if (annotArgs != null)
                {
                    annotArgs.LinkDrawFinished += annotArgs_LinkDrawFinished;
                }
            }
        }

        public AnnotArgsType AnnotType
        {
            get { return annotType; }
            set
            {
                SetProperty(ref annotType, value);
            }
        }

        #region 输入框的验证、设置

        private string pageNumTextContent;

        public string PageNumTextContent
        {
            get
            {
                if (CheckPageNumVaild(out int pageNum, pageNumTextContent))
                {
                    if (AnnotArgs != null)
                    {
                        AnnotArgs.DestIndex = pageNum - 1;
                        AnnotArgs.Content = string.Format($"To Page {AnnotArgs.DestIndex}");
                        //isTextChanged = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] != AnnotArgs.DestIndex ? true : false;
                        //SetImagePreview(AnnotArgs.DestIndex);
                    }
                }
                else
                {
                    ImagePreviewVisibility = Visibility.Collapsed;
                    BtnGOorBackVisibility = Visibility.Collapsed;
                }
                return pageNumTextContent;
            }
            set
            {
                SetProperty(ref pageNumTextContent, value);
            }
        }

        private string pageWebTextContent;

        public string PageWebTextContent
        {
            get
            {
                if (CheckPageWebVaild(pageWebTextContent))
                {
                    pageWebTextContent = pageWebTextContent.Trim().ToLower();
                    if (!pageWebTextContent.StartsWith("http://") && !pageWebTextContent.StartsWith("https://"))
                    {
                        pageWebTextContent = "http://" + pageWebTextContent;
                    }
                    if (AnnotArgs != null)
                    {
                        AnnotArgs.URI = pageWebTextContent;
                        AnnotArgs.Content = AnnotArgs.URI;
                        //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
                    }
                }

                return pageWebTextContent;
            }
            set
            {
                SetProperty(ref pageWebTextContent, value);
            }
        }

        private string pageMailTextContent;

        public string PageMailTextContent
        {
            get
            {
                if (CheckPageMailVaild(pageMailTextContent))
                {
                    if (!string.IsNullOrEmpty(pageMailTextContent) && AnnotArgs != null)
                    {
                        AnnotArgs.URI = "mailto:" + pageMailTextContent.Trim();
                        AnnotArgs.Content = AnnotArgs.URI;
                        //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
                    }
                }
                return pageMailTextContent;
            }
            set
            {
                SetProperty(ref pageMailTextContent, value);
            }
        }

        /// <summary>
        /// 设置预文本内容
        /// </summary>
        private string pageNumPlaceHoldText = "输入目标页面";

        public string PageNumPlaceHoldText
        {
            get { return pageNumPlaceHoldText; }
            set
            {
                SetProperty(ref pageNumPlaceHoldText, value);
            }
        }

        /// <summary>
        /// 是否显示输入框下方提示文案
        /// </summary>
        private Visibility showPageNumTip = Visibility.Collapsed;

        public Visibility ShowPageNumTip
        {
            get { return showPageNumTip; }
            set
            {
                SetProperty(ref showPageNumTip, value);
            }
        }

        /// <summary>
        /// 输入框下方提示文案
        /// </summary>
        private string pageNumTipText = "Page number error.";

        public string PageNumTipText
        {
            get { return pageNumTipText; }
            set
            {
                SetProperty(ref pageNumTipText, value);
            }
        }

        private bool pageNumTextIsEnabled = false;

        public bool PageNumTextIsEnabled
        {
            get { return pageNumTextIsEnabled; }
            set
            {
                SetProperty(ref pageNumTextIsEnabled, value);
            }
        }

        private bool pageWebTextIsEnabled = false;

        public bool PageWebTextIsEnabled
        {
            get { return pageWebTextIsEnabled; }
            set
            {
                SetProperty(ref pageWebTextIsEnabled, value);
            }
        }

        private string pageWebPlaceHoldText = "输入您要跳转的链接";

        public string PageWebPlaceHoldText
        {
            get { return pageWebPlaceHoldText; }
            set
            {
                SetProperty(ref pageWebPlaceHoldText, value);
            }
        }

        private Visibility showPageWebTip = Visibility.Collapsed;

        public Visibility ShowPageWebTip
        {
            get { return showPageWebTip; }
            set
            {
                SetProperty(ref showPageWebTip, value);
            }
        }

        private string pageWebTipText = "Invalid link.";

        public string PageWebTipText
        {
            get { return pageWebTipText; }
            set
            {
                SetProperty(ref pageWebTipText, value);
            }
        }

        private bool pageMailTextIsEnabled = false;

        public bool PageMailTextIsEnabled
        {
            get { return pageMailTextIsEnabled; }
            set
            {
                SetProperty(ref pageMailTextIsEnabled, value);
            }
        }

        private string pageMailPlaceHoldText = "输入您要跳转邮件地址";

        public string PageMailPlaceHoldText
        {
            get { return pageMailPlaceHoldText; }
            set
            {
                SetProperty(ref pageMailPlaceHoldText, value);
            }
        }

        private Visibility showPageMailTip = Visibility.Collapsed;

        public Visibility ShowPageMailTip
        {
            get { return showPageMailTip; }
            set
            {
                SetProperty(ref showPageMailTip, value);
            }
        }

        private string pageMailTipText = "The email is invalid.Please enter a valid email address.";

        public string PageMailTipText
        {
            get { return pageMailTipText; }
            set
            {
                SetProperty(ref pageMailTipText, value);
            }
        }

        #endregion 输入框的验证、设置

        #region 按钮、图片的设置

        private bool btnLocationIsEnabled = false;

        public bool BtnLocationIsEnabled
        {
            get { return btnLocationIsEnabled; }
            set
            {
                SetProperty(ref btnLocationIsEnabled, value);
            }
        }

        private WriteableBitmap previewImage;

        public WriteableBitmap PreviewImage
        {
            get { return previewImage; }
            set
            {
                SetProperty(ref previewImage, value);
            }
        }

        private int imagePreviewWidth = 196;

        public int ImagePreviewWidth
        {
            get { return imagePreviewWidth; }
            set
            {
                SetProperty(ref imagePreviewWidth, value);
            }
        }

        private int imagePreviewHeight = 256;

        public int ImagePreviewHeight
        {
            get { return imagePreviewHeight; }
            set
            {
                SetProperty(ref imagePreviewHeight, value);
            }
        }

        private Visibility imagePreviewVisibility = Visibility.Collapsed;

        public Visibility ImagePreviewVisibility
        {
            get { return imagePreviewVisibility; }
            set
            {
                SetProperty(ref imagePreviewVisibility, value);
            }
        }

        private Visibility btnGOorBackVisibility = Visibility.Collapsed;

        public Visibility BtnGOorBackVisibility
        {
            get { return btnGOorBackVisibility; }
            set
            {
                SetProperty(ref btnGOorBackVisibility, value);
            }
        }

        private string btnGOorBackContent = "GO";

        public string BtnGOorBackContent
        {
            get { return btnGOorBackContent; }
            set
            {
                SetProperty(ref btnGOorBackContent, value);
            }
        }

        private bool btnLinkPageIsChecked = true;

        public bool BtnLinkPageIsChecked
        {
            get { return btnLinkPageIsChecked; }
            set
            {
                SetProperty(ref btnLinkPageIsChecked, value);
            }
        }

        private bool btnLinkWebIsChecked = false;

        public bool BtnLinkWebIsChecked
        {
            get { return btnLinkWebIsChecked; }
            set
            {
                SetProperty(ref btnLinkWebIsChecked, value);
            }
        }

        private bool btnLinkMailIsChecked = false;

        public bool BtnLinkMailIsChecked
        {
            get { return btnLinkMailIsChecked; }
            set
            {
                SetProperty(ref btnLinkMailIsChecked, value);
            }
        }

        #endregion 按钮、图片的设置

        private bool isLoaded = false;
        private bool isMail = false;
        private bool isTextChanged = false;
        private string historyBtnGOorBackTag = string.Empty;
        private int totalPage = 0;
        private int backPageIndex = 0;
        private int historyPageIndex = 0;

        private CPDFViewer pdfViewer;
        private CPDFDocument document;
        private Button btnGOorBack = null;
        private LinkAnnotProperty linkAnnot;
        public DelegateCommand<object> LoadedCommand { get; set; }
        public DelegateCommand<object> PageNumTextLostFocusCommand { get; set; }

        public DelegateCommand<object> PageNumTextKeyDownCommand { get; set; }
        public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
        public DelegateCommand<object> LocationCommand { get; set; }

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

        public LinkAnnotPropertyViewModel()
        {
            LoadedCommand = new DelegateCommand<object>(Loaded);
            PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
            PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
            BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
            LocationCommand = new DelegateCommand<object>(LocationPage);
            ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
            isLoaded = true;
        }

        private void PageNumTextKeyDown(object obj)
        {
            if (obj is KeyEventArgs keyEventArgs)
            {
                if (keyEventArgs.Key == Key.Enter)
                {
                    PageNumTextLostFocus(obj);
                }
            }
        }
        private void PageNumTextLostFocus(object obj)
        {
            if (!string.IsNullOrEmpty(PageNumTextContent))
            {
                if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
                {
                    AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
                    AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
                    AnnotAttribEvent?.UpdateAnnot();

                    AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
                    //isTextChanged = false;
                    SetImagePreview(AnnotArgs.DestIndex);
                }
                else
                {
                    ImagePreviewVisibility = Visibility.Collapsed;
                    BtnGOorBackVisibility = Visibility.Collapsed;
                }
            }
        }

        private void ToggleButtonTabSelected(object obj)
        {
            if (obj is RadioButton radioButton)
            {
                switch (radioButton.Tag)
                {
                    case "Page":
                        AnnotArgs.LinkType = LINK_TYPE.GOTO;
                        AnnotArgs.URI = string.Empty;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
                        isMail = false;
                        if (btnGOorBack != null)
                        {
                            if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
                            {
                                ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
                            }
                            else
                            {
                                ChangeBtnGOorBack("GO", btnGOorBack);
                            }
                        }
                        BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
                        break;

                    case "Web":
                        AnnotArgs.LinkType = LINK_TYPE.URI;
                        AnnotArgs.URI = string.Empty;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
                        isMail = false;
                        if (btnGOorBack != null)
                        {
                            historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
                            ChangeBtnGOorBack("GO", btnGOorBack);
                        }

                        BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
                        break;

                    case "Mail":
                        AnnotArgs.LinkType = LINK_TYPE.URI;
                        AnnotArgs.URI = string.Empty;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
                        isMail = true;
                        if (btnGOorBack != null)
                        {
                            historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
                            ChangeBtnGOorBack("GO", btnGOorBack);
                        }
                        BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;

                        break;

                    default:
                        AnnotArgs.LinkType = LINK_TYPE.GOTO;
                        AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
                        isMail = false;
                        if (btnGOorBack != null)
                        {
                            if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
                            {
                                ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
                            }
                            else
                            {
                                ChangeBtnGOorBack("GO", btnGOorBack);
                            }
                        }
                        BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
                        break;
                }
            }
        }

        /// <summary>
        /// 定位
        /// </summary>
        /// <param name="obj"></param>
        private void LocationPage(object obj)
        {
            if (string.IsNullOrEmpty(PageNumTextContent))
            {
                //if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
                //{
                //    AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
                //    AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
                //    AnnotAttribEvent?.UpdateAnnot();

                //    AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
                //    //isTextChanged = false;
                //}
            }
        }

        /// <summary>
        /// 前往/返回按钮
        /// </summary>
        /// <param name="obj"></param>
        private void BtnGOorBackPageEvent(object obj)
        {
            if (obj is object[] arry)
            {
                btnGOorBack = arry[0] as Button;
                RadioButton btnLinkPage = arry[1] as RadioButton;
                RadioButton btnLinkWeb = arry[2] as RadioButton;
                RadioButton btnLinkMail = arry[3] as RadioButton;

                if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
                {
                    if (btnLinkPage.IsChecked == true)
                    {
                        LinkPageGoOrBack();
                    }

                    if (btnLinkWeb.IsChecked == true)
                    {
                        LinkWebSave();
                    }

                    if (btnLinkMail.IsChecked == true)
                    {
                        LinkMailSave();
                    }
                }
            }
        }

        private void LinkMailSave()
        {
            if (CheckPageMailVaild(PageMailTextContent))
            {
                AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
                AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
                AnnotAttribEvent?.UpdateAnnot();

                AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);

                Process.Start("mailto:?to=" + PageMailTextContent.Trim());
            }
        }

        private void LinkWebSave()
        {
            if (CheckPageWebVaild(PageWebTextContent))
            {
                AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
                PageWebTextContent = PageWebTextContent.Trim().ToLower();
                if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
                {
                    PageWebTextContent = "http://" + PageWebTextContent;
                }
                AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
                AnnotAttribEvent?.UpdateAnnot();
                AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);

                if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
                {
                    Process.Start(PageWebTextContent);
                }
            }
            else
            {
                BtnGOorBackVisibility = Visibility.Collapsed;
            }
        }

        private void LinkPageGoOrBack()
        {
            int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;

            if (btnGOorBack.Tag.ToString() == "GO")
            {
                //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
                //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
                //AnnotAttribEvent?.UpdateAnnot();

                historyPageIndex = AnnotArgs != null ? AnnotArgs.PageIndex : backPageIndex;
                pdfViewer.GoToPage(pageIndex);

                ChangeBtnGOorBack("BACK", btnGOorBack);
            }
            else
            {
                pdfViewer.GoToPage(historyPageIndex);
                ChangeBtnGOorBack("GO", btnGOorBack);
            }
        }

        /// <summary>
        /// 设置前往/返回按钮的文本
        /// </summary>
        /// <param name="flag"></param>
        /// <param name="button"></param>
        private void ChangeBtnGOorBack(string flag, Button button)
        {
            switch (flag)
            {
                case "BACK":
                    button.Tag = "BACK";
                    button.Content = "BACK";
                    break;

                case "GO":
                    button.Tag = "GO";
                    button.Content = "GO";
                    break;

                default:
                    break;
            }
        }

        private void PageNumTextChanged(object obj)
        {
            if (obj is TextChangedEventArgs textChangedEventArgs)
            {
                if (textChangedEventArgs.Source is TextBoxWithTip textBox)
                {
                    int pageNum = -1;
                    //if (CheckPageNumVaild(out pageNum))
                    //{
                    //    if (AnnotArgs != null)
                    //    {
                    //        AnnotArgs.DestIndex = pageNum - 1;
                    //        SetImagePreview();
                    //    }
                    //}
                }
            }
        }

        /// <summary>
        /// 页码格式验证
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        private bool CheckPageNumVaild(out int pageNum, string text)
        {
            pageNum = -1;
            if (string.IsNullOrEmpty(text))
            {
                return false;
            }
            if (text.Trim() != string.Empty)
            {
                if (!int.TryParse(text.Trim(), out pageNum))
                {
                    ShowPageNumTip = Visibility.Visible;
                    PageNumTipText = " Page number error.";
                    return false;
                }
            }
            if (pageNum < 1 || pageNum > totalPage)
            {
                ShowPageNumTip = Visibility.Visible;
                PageNumTipText = "  Page number out of range.";
                return false;
            }
            else
            {
                ShowPageNumTip = Visibility.Collapsed;
            }
            //BtnGOorBackVisibility = Visibility.Visible;

            return true;
        }

        private bool CheckPageWebVaild(string text)
        {
            BtnGOorBackVisibility = Visibility.Collapsed;
            if (string.IsNullOrEmpty(text))
            {
                return false;
            }

            string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
            if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
            {
                ShowPageWebTip = Visibility.Visible;
                PageWebTipText = "Invalid link.";
                return false;
            }
            string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
            if (matchText.Length != checkUrl.Length)
            {
                ShowPageWebTip = Visibility.Visible;
                PageWebTipText = "Invalid link.";
                return false;
            }
            else
            {
                ShowPageWebTip = Visibility.Collapsed;
            }
            BtnGOorBackVisibility = Visibility.Visible;
            return true;
        }

        private bool CheckPageMailVaild(string text)
        {
            BtnGOorBackVisibility = Visibility.Collapsed;
            if (string.IsNullOrEmpty(text))
            {
                return false;
            }
            if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
            {
                ShowPageMailTip = Visibility.Visible;
                PageMailTipText = "The email is invalid.Please enter a valid email address.";
                return false;
            }
            else
            {
                ShowPageMailTip = Visibility.Collapsed;
            }
            BtnGOorBackVisibility = Visibility.Visible;
            return true;
        }

        /// <summary>
        /// 设置图像
        /// </summary>
        /// <param name="pageIndex"></param>
        private void SetImagePreview(int pageIndex)
        {
            ImagePreviewVisibility = Visibility.Visible;
            BtnGOorBackVisibility = Visibility.Visible;

            int dpiHeight = (int)ImagePreviewHeight;
            int dpiWidth = (int)ImagePreviewWidth;
            if (dpiHeight <= 0 || dpiWidth <= 0)
            {
                return;
            }

            //int pageIndex = AnnotArgs.DestIndex;
            Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);

            System.Windows.Size pageSize = GetPageSize(document, pageIndex);
            double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
            scale = Math.Min(scale, 1);

            int cropWidth = (int)(scale * pageSize.Width);
            int cropHeight = (int)(scale * pageSize.Height);

            byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);

            WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
            WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);

            PreviewImage = WirteBitmap;
        }

        /// <summary>
        /// 链接画框完成时触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void annotArgs_LinkDrawFinished(object sender, bool e)
        {
            //linkAnnot.SetTextBoxEnableOrNot(e, totalPage);
            //AnnotArgs.DestIndex = -1;
            //AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] =- 1;
            SetTextBoxEnableOrNot(e, totalPage);

            //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
        }

        private void Loaded(object obj)
        {
            if (obj is CompositeCommandParameter composite)
            {
                if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
                {
                    linkAnnot = linkAnnotProperty;
                }
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)

        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);

            if (PropertyPanel != null && pdfViewer != null)
            {
                AnnotAttribEvent = PropertyPanel.AnnotEvent;
                AnnotType = PropertyPanel.annot.EventType;

                document = pdfViewer.Document;
                totalPage = pdfViewer.Document.PageCount;

                SetTextBoxEnableOrNot(false, totalPage);
                if (AnnotAttribEvent.IsAnnotCreateReset == false)
                {
                    GetAnnotAttribute();
                }

                if (isLoaded)
                {
                    if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
                    {
                        if (btnGOorBack != null)
                        {
                            ChangeBtnGOorBack("GO", btnGOorBack);
                        }
                        if (AnnotAttribEvent.IsAnnotCreateReset)
                        {
                            AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
                            if (AnnotArgs == null)
                            {
                                backPageIndex = pdfViewer.CurrentIndex;
                            }
                            else
                            {
                                AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
                            }
                        }
                    }
                }

                if (AnnotArgs == null)
                {
                    AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
                }
            }
        }

        private void GetAnnotAttribute()
        {
            if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
            {
                SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
            }
            if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
            {
                SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
                if (AnnotArgs == null)
                {
                    backPageIndex = pdfViewer.CurrentIndex;
                }
                else
                {
                    AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
                }
            }
            if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
            {
                string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
                if (!string.IsNullOrEmpty(linkUrl))
                {
                    if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
                    {
                        SetLinkType(LINK_TYPE.URI, true);
                        if (AnnotAttribEvent.IsAnnotCreateReset)
                        {
                            SetLinkEmail("");
                        }
                        else
                        {
                            SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
                        }
                    }
                    else
                    {
                        if (AnnotAttribEvent.IsAnnotCreateReset)
                        {
                            SetLinkUrl("");
                        }
                        else
                        {
                            SetLinkUrl(linkUrl);
                        }
                    }
                }
            }
        }

        private void SetLinkUrl(string url)
        {
            PageWebTextContent = url;
            PageWebTextIsEnabled = true;
        }

        private void SetLinkEmail(string email)
        {
            PageMailTextContent = email;
            PageMailTextIsEnabled = true;
        }

        /// <summary>
        /// 设置页码的数据
        /// </summary>
        /// <param name="pageNum"></param>
        private void SetLinkPageNum(int pageNum)
        {
            if (pageNum > 0 && pageNum <= totalPage)
            {
                PageNumTextContent = pageNum.ToString();
                BtnLocationIsEnabled = true;
                PageNumTextIsEnabled = true;
                ImagePreviewVisibility = Visibility.Visible;

                SetImagePreview(pageNum - 1);

                //if (btnGOorBack != null)
                //{
                //    ChangeBtnGOorBack("GO", btnGOorBack);
                //}
            }
        }

        /// <summary>
        /// 根据链接类型显示模块
        /// </summary>
        /// <param name="linkType"></param>
        /// <param name="isMail"></param>
        public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
        {
            BtnLinkPageIsChecked = false;
            BtnLinkMailIsChecked = false;
            BtnLinkWebIsChecked = false;
            switch (linkType)
            {
                case LINK_TYPE.GOTO:
                    BtnLinkPageIsChecked = true;
                    break;

                case LINK_TYPE.URI:
                    if (this.isMail || isMail)
                    {
                        BtnLinkMailIsChecked = true;
                    }
                    else
                    {
                        BtnLinkWebIsChecked = true;
                    }
                    break;

                default:
                    BtnLinkPageIsChecked = true;
                    break;
            }
        }

        private void SetTextBoxEnableOrNot(bool enable, int totalPage)
        {
            this.totalPage = totalPage;

            PageNumTextContent = string.Empty;
            PageWebTextContent = string.Empty;
            PageMailTextContent = string.Empty;

            PageNumTextIsEnabled = enable;
            PageWebTextIsEnabled = enable;
            PageMailTextIsEnabled = enable;

            PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
            PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
            PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";

            ShowPageNumTip = Visibility.Collapsed;

            ShowPageWebTip = Visibility.Collapsed;

            ShowPageMailTip = Visibility.Collapsed;

            BtnLocationIsEnabled = enable;
        }

        public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
        {
            if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
            {
                return null;
            }
            try
            {
                byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
                currentZoom = currentZoom * 96 / 72;
                int flag = 0;
                if (renderAnnot)
                {
                    flag = 1;
                }
                uint bgColor = 0xFFFFFFFF;
                CPDFPage page = document.PageAtIndex(pageIndex);
                if (page.IsValid())
                {
                    page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
                }
                return bmpData;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
        {
            CPDFPage currentPage = document.PageAtIndex(pageIndex);
            Rect boundBox = currentPage.GetBoundsForBox(displayBox);

            Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
            (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));

            return boundRect;
        }

        private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
        {
            System.Windows.Size pageSize = document.GetPageSize(pageIndex);
            pageSize.Width = pageSize.Width / 72.0 * 96;
            pageSize.Height = pageSize.Height / 72.0 * 96;

            return pageSize;
        }
    }
}