using ComPDFKit.PDFDocument;
using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.EditTools.Background;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace PDF_Office.ViewModels.EditTools.Redaction
{
    public class RedactionDocumentContentViewModel : BindableBase, INavigationAware
    {
        private CPDFViewer pdfViewer;
        public IRegionManager regionManager;
        public CPDFViewer PDFViewer;
        public CPDFDocument Document;

        public string ViewerRegionName { get; set; }

        private int _currentPageIndex;
        public int CurrentPageIndex
        {
            get { return _currentPageIndex; }
            set
            {
                SetProperty(ref _currentPageIndex, PDFViewer.CurrentIndex);
            }
        }

        private int _pageSize;
        public int PageRangeNumber
        {
            get { return _pageSize; }
            set { SetProperty(ref _pageSize, value); }
        }

        public RedactionDocumentContentViewModel(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
            PageRangeNumber = PDFViewer.Document.PageCount;
            ViewerRegionName = RegionNames.BackgroundViewerRegionName;
        }

        private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
        {
        }

        private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
            if (pdfViewer != null)
            {
                if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
                {
                    PDFViewer = new CPDFViewer();
                    PDFViewer.InitDocument(pdfViewer.Document);
                    Document = PDFViewer.Document;
                    PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
                    PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
                    regionManager.AddToRegion(ViewerRegionName, PDFViewer);
                    PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
                    PDFViewer.Load();
                    PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
                    PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
                }
            }
        }
    }
}
