using ComPDFKit.PDFDocument;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.Linq;
using PDF_Office.Model.HomePageToolsDialogs;
using PDF_Office.Helper;
using System.Diagnostics;
using PDF_Office.CustomControl;
using PDF_Office.Model.Dialog.HomePageToolsDialogs;

namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs
{
    public class HomePageExtractDialogViewModel : BindableBase, IDialogAware
    {
        #region 参数和属性
        public CPDFDocument document;

        private CPDFViewer currentViewer;

        private HomePageExtractDialogModel extractModel = new HomePageExtractDialogModel();

        private string savefilepath { get; set; }



        public string ExtractToSingleFileIsCheck {get;set;} = "False";

        public string DeleteAfterExtractIsCheck { get; set; } = "Fales";

        public string PageRangeText { get; set; } = "";

        public string PageRangeSelectIndex { get; set; } = "";
        #endregion

        #region 委托声明
        public DelegateCommand CancelCommand { get; set; }

        public DelegateCommand ExtractCommand { get; set; }

        public DelegateCommand ExtractToSingleFileCommand { get; set; }

        public DelegateCommand DeleteAfterExtractCommand { get; set; }
        #endregion

        public HomePageExtractDialogViewModel()
        {
            CancelCommand = new DelegateCommand(cancel);
            ExtractCommand = new DelegateCommand(extract);
            ExtractToSingleFileCommand = new DelegateCommand(extractToSingleFile);
            DeleteAfterExtractCommand = new DelegateCommand(deleteAfterExtract);

        }
        #region 逻辑函数
        private void cancel()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        private void extract()
        {
            HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref extractModel.PageRange, PageRangeText);
            char[] enumerationSeparator = new char[] { ',' };
            char[] rangeSeparator = new char[] { '-' };
            if (!CommonHelper.GetPagesInRange(ref extractModel.PageParm, extractModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
            { //TODO
                Trace.WriteLine("输入不对");
                MessageBoxEx.Show("输入不对");
                return;
            }
            if (extractModel.ExtractToSingleFile)
            {
                for (int i = 0; i < extractModel.PageParm.ToArray().Length; i++)
                {
                    CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                    extractdoc.ImportPages(currentViewer.Document, (extractModel.PageParm.ToArray()[i] + 1).ToString());
                    Trace.WriteLine(extractModel.PageParm.ToArray()[i].ToString());
                    extractdoc.WriteToFilePath(savefilepath + currentViewer.Document.FileName + ".extract" + i.ToString() + ".pdf");
                    extractdoc.Release();
                }
                if (extractModel.DeleteAfterExtract)
                {
                    currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
                    currentViewer.Document.WriteToLoadedPath();
                }

            }
            else
            {
                CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                extractdoc.ImportPages(currentViewer.Document, extractModel.PageRange);
                extractdoc.WriteToFilePath(savefilepath + currentViewer.Document.FileName + ".extract.pdf");
                if (extractModel.DeleteAfterExtract)
                {
                    currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
                    currentViewer.Document.WriteToLoadedPath();
                }
                extractdoc.Release();
            }
            System.Diagnostics.Process.Start("Explorer", "/select," + currentViewer.Document.FilePath);
            RequestClose.Invoke(new DialogResult(ButtonResult.OK));
        }

        private void extractToSingleFile()
        {
            if (ExtractToSingleFileIsCheck == "True")
            {
                extractModel.ExtractToSingleFile = true;
                Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
            }
            else
            {
                extractModel.ExtractToSingleFile = false;
                Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
            }
        }

        private void deleteAfterExtract()
        {

            if (DeleteAfterExtractIsCheck == "True")
            {
                extractModel.DeleteAfterExtract = true;
                Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
            }
            else
            {
                extractModel.DeleteAfterExtract = false;
                Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
            }

        }
        #endregion

        #region 框架行为
        public string Title => "";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            CPDFViewer viewer = null;
            string filepath = "";
            parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
            parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
            if (viewer != null && viewer.Document != null)
            {
                currentViewer = viewer;
                savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", "");
                document = currentViewer.Document;
            }
        }
        #endregion
    }
}
