using ComPDFKit.PDFPage;
using ComPDFKit.Tool;
using Compdfkit_Tools.PDFControlUI;
using ComPDFKitViewer;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Compdfkit_Tools.PDFControl
{
    public partial class CPDFSearchControl : UserControl
    {
        /// <summary>
        /// PDFViewer
        /// </summary>
        private CPDFViewer pdfView;
        private int currentHighLightIndex { get; set; } = -1;

        private PDFTextSearch textSearch;

        private string keyWord;

        private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));

        private int ResultCount = 0;

        private PDFViewControl ViewControl;
        public CPDFSearchControl()
        {
            InitializeComponent();
            Loaded += PDFSearch_Loaded;
        }

        public void InitWithPDFViewer(PDFViewControl newViewControl)
        {
            ViewControl = newViewControl;
            if (ViewControl != null)
            {
                CPDFViewerTool viewTool = ViewControl.PDFViewTool;
                if (viewTool != null)
                {
                    CPDFViewer newPDFView = viewTool.GetCPDFViewer();
                    if (pdfView != newPDFView)
                    {
                        ClearSearchResult();
                        pdfView = newPDFView;

                    }
                }
            }
            else
            {
                ClearSearchResult();
            }

        }

        private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
        {
            textSearch = new PDFTextSearch();
            SearchInput.SearchEvent += SearchInput_SearchEvent;
            SearchInput.ClearEvent += SearchInput_ClearEvent;
            textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
            SearchResult.SelectionChanged += SearchResult_SelectionChanged;
        }

        private void SearchInput_ClearEvent(object sender, EventArgs e)
        {
            ClearSearchResult();
        }

        private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
        {
            if (currentHighLightIndex > 0)
            {
                currentHighLightIndex--;
                BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
                HighLightSelectResult(result);
            }
        }

        private void SearchInput_FindNextEvent(object sender, EventArgs e)
        {
            currentHighLightIndex++;
            if (currentHighLightIndex >= 0)
            {
                BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
                HighLightSelectResult(result);
            }
        }

        private void SearchResult_SelectionChanged(object sender, int e)
        {
            currentHighLightIndex = e;
            BindSearchResult result = SearchResult.GetSelectItem();
            HighLightSelectResult(result);
            ResultText.Text = string.Format("Results:{0}/{1}", e+1,ResultCount);
        }

        private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
        {
            Dispatcher.Invoke(() =>
            {
                List<BindSearchResult> resultList = new List<BindSearchResult>();
                List<TextSearchItem> totalItems = new List<TextSearchItem>();
                foreach (int pageIndex in e.Items.Keys)
                {
                    List<TextSearchItem> textSearchItems = e.Items[pageIndex];
                    foreach (TextSearchItem item in textSearchItems)
                    {
                        resultList.Add(new BindSearchResult()
                        {
                            PageIndex = item.PageIndex,
                            TextContent = item.TextContent,
                            TextRect = item.TextRect,
                            SearchWord = keyWord,
                            HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
                            PageRotate = item.PageRotate,
                            RefData=item
                        });
                        totalItems.Add(item);
                    }
                }
                SearchResult.SetSearchResult(resultList);
                ResultCount=resultList.Count;
                ViewControl.PDFViewTool?.SetPageSelectText(totalItems);

                if (resultList==null ||  resultList.Count==0)
                {
                    ResultText.Text= string.Empty;
                }
                else
                {
                    ResultText.Text = string.Format("{0} results found", ResultCount);
                }
            });
        }

        private void SearchInput_SearchEvent(object sender, string e)
        {
            if (string.IsNullOrEmpty(e))
            {
                return;
            }

            if (pdfView == null || pdfView.GetDocument() == null)
            {
                return;
            }

            if (textSearch != null && textSearch.CanDoSearch)
            {
                keyWord = e;
                textSearch.TextSearchDocument = pdfView.GetDocument();
                textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
            }
        }

        private void HighLightSelectResult(BindSearchResult result)
        {
            if (result == null || result.RefData==null)
            {
                return;
            }

            List<TextSearchItem> selectList = new List<TextSearchItem>();
            TextSearchItem highlightItem=result.RefData as TextSearchItem;
            if(highlightItem != null)
            {
                highlightItem.CreatePaintBrush(highLightBrush.Color);
                selectList.Add(highlightItem);
                ViewControl.PDFViewTool?.HighLightSearchText(selectList);
            }
        }

        private void ClearSearchResult()
        {
            SearchResult?.SetSearchResult(null);
            ResultText.Text = string.Empty;
            SearchInput.SearchKeyWord=string.Empty;
        }
    }
}
