using ComPDFKit.PDFAnnotation;
using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using ComPDFKit.Tool;
using ComPDFKit.Tool.Help;
using ComPDFKitViewer;
using ComPDFKitViewer.BaseObject;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Annotations;
using System.Windows.Controls;
using static Compdfkit_Tools.PDFControlUI.CPDFAnnoationListUI;

namespace Compdfkit_Tools.PDFControl
{
    public partial class CPDFAnnotationListControl : UserControl
    {
        private List<C_ANNOTATION_TYPE> OmitList = new List<C_ANNOTATION_TYPE> 
        {
            C_ANNOTATION_TYPE.C_ANNOTATION_UNKOWN,
            C_ANNOTATION_TYPE.C_ANNOTATION_LINK,
            C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET,
            C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE,
            C_ANNOTATION_TYPE.C_ANNOTATION_SOUND,
            C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA
        };
        /// <summary>
        /// PDFViewer
        /// </summary>
        private PDFViewControl pdfViewer;

        public CPDFAnnotationListControl()
        {
            InitializeComponent();
            Loaded += CPDFAnnotationListControl_Loaded;
        }

        private void CPDFAnnotationListControl_Loaded(object sender, RoutedEventArgs e)
        {
            AnnotationList.DeleteItemHandler -= AnnotationList_DeleteItemHandler;
            AnnotationList.DeleteItemHandler += AnnotationList_DeleteItemHandler;
        }

        private void AnnotationList_DeleteItemHandler(object sender, Dictionary<int, List<int>> e)
        {
            if (pdfViewer != null)
            {
                pdfViewer.PDFToolManager.ClearSelect();
                ParamConverter.RemovePageAnnot(e, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
                LoadAnnotationList();
            }
        }

        public void InitWithPDFViewer(PDFViewControl newPDFView)
        {
            pdfViewer = newPDFView;
            pdfViewer.PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
        }

        private void PDFToolManager_MouseLeftButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
        {
            if (OmitList.Contains(e.annotType))
            {
                AnnotationList.SelectAnnotationChanged(-1);
            }
            else
            {
                BaseAnnot baseAnnot= pdfViewer.PDFToolManager.GetCacheHitTestAnnot();
                AnnotData annotData = baseAnnot?.GetAnnotData();
                if(annotData != null)
                {
                    AnnotationList.SelectAnnotationChanged(annotData.PageIndex, annotData.AnnotIndex);
                }
            }
        }

        public void LoadAnnotationList()
        {
            if (pdfViewer == null || pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() == null)
            {
                return;
            }

            if (pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().IsLocked)
            {
                return;
            }
            int pageCount = pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().PageCount;

            List<BindAnnotationResult> bindAnnotationResults = new List<BindAnnotationResult>();

            for (int i = 0; i < pageCount; i++)
            {

                List<AnnotParam> annotList = GetAnnotCommentList(i, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
                if (annotList != null)
                {
                    Dispatcher.Invoke(() =>
                    {
                        foreach (AnnotParam annot in annotList)
                        {
                            bindAnnotationResults.Add(new BindAnnotationResult
                            {
                                PageIndex = i,
                                annotationData = annot
                            });
                        }
                    });
                }
            }
            AnnotationList.SetAnnotationList(bindAnnotationResults);
            pdfViewer.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
        }

        public List<AnnotParam> GetAnnotCommentList(int pageIndex, CPDFDocument currentDoc)
        {
            List<AnnotParam> annotList = new List<AnnotParam>();

            if (pageIndex < 0 || pdfViewer == null || currentDoc == null)
            {
                return annotList;
            }
            CPDFPage docPage = currentDoc.PageAtIndex(pageIndex, false);
            if (docPage == null)
            {
                return annotList;
            }
            List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
            if (docAnnots != null && docAnnots.Count > 0)
            {
                foreach (CPDFAnnotation annotation in docAnnots)
                {
                    if (annotation==null|| OmitList.Contains(annotation.Type))
                    {
                        continue;
                    }
                    AnnotParam annotParam= ParamConverter.CPDFDataConverterToAnnotParam(currentDoc,pageIndex,annotation);
                    annotList.Add(annotParam);
                }
            }
            return annotList;
        }

        private void AnnotationList_Loaded(object sender, RoutedEventArgs e)
        {
            AnnotationList.AnnotationSelectionChanged += AnnotationList_AnnotationSelectionChanged;
        }

        private void AnnotationList_Unloaded(object sender, RoutedEventArgs e)
        {
            AnnotationList.AnnotationSelectionChanged -= AnnotationList_AnnotationSelectionChanged;
        }

        private void AnnotationList_AnnotationSelectionChanged(object sender, object e)
        {
            var bindAnnotationResult = e as BindAnnotationResult;
            //pdfViewer.SelectAnnotation(bindAnnotationResult.PageIndex, bindAnnotationResult.AnnotIndex);
        }
    }
}
