using ComPDFKit.PDFDocument;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using PDF_Office.Helper;
using System.Diagnostics;
using PDF_Office.CustomControl;
using PDF_Office.Model.HomePageToolsDialogs;

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

        private CPDFViewer currentViewer;

        private HomePageSplitDialogModel splitModel = new HomePageSplitDialogModel();

        private string savefilepath { get; set; }

        private string savefilename { get; set; }

        private string pageRangeText = "0";
        public string PageRangeText { set; get; } = "0";
        
        public string PageRangeSelectIndex { set; get; } = "0";

        private string averagePagesIsCheck = "True";
        public string AveragePagesIsCheck
        {
            get { return averagePagesIsCheck; }
            set
            {
                SetProperty(ref averagePagesIsCheck, value);
            }
        }

        private string averageFilesIsCheck = "False";
        public string AverageFilesIsCheck
        {
            get { return averageFilesIsCheck; }
            set
            {
                SetProperty(ref averageFilesIsCheck, value);
            }
        }

        private string customPageRangeIsCheck = "False";
        public string CustomPageRangeIsCheck
        {
            get { return customPageRangeIsCheck; }
            set
            {
                SetProperty(ref customPageRangeIsCheck, value);
            }
        }

        private string averagePagesIsEnabled = "True";
        public string AveragePagesIsEnabled
        {
            get { return averagePagesIsEnabled; }
            set
            {
                SetProperty(ref averagePagesIsEnabled, value);
            }
        }

        private string averageFilesIsEnabled = "False";
        public string AverageFilesIsEnabled
        {
            get { return averageFilesIsEnabled; }
            set
            {
                SetProperty(ref averageFilesIsEnabled, value);
            }
        }

        private string customPageRangeIsEnabled = "False";
        public string CustomPageRangeIsEnabled
        {
            get { return customPageRangeIsEnabled; }
            set
            {
                SetProperty(ref customPageRangeIsEnabled, value);
            }
        }

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

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

        private string filePathName = "";
        public string FilePathName
        {
            get { return filePathName; }
            set
            {
                SetProperty(ref filePathName, value);
            }
        }

        public string FileNameLabelText{ set; get; } = "part";
        

        public string FileNameDeimiterText{ set; get; } = "-";
        

        public string FrontFileIsCheck { set; get; } = "True";
       
        public string FileNameLabelIsCheck { set; get; } = "True";

        public string FileNameDeimiterIsCheck { set; get; } = "True";

        private string fileNameLabelIsEnabled = "False";
        public string FileNameLabelIsEnabled
        {
            get { return fileNameLabelIsEnabled; }
            set
            {
                SetProperty(ref fileNameLabelIsEnabled, value);
            }
        }

        private string fileNameDeimiterIsEnabled = "False";
        public string FileNameDeimiterIsEnabled
        {
            get { return fileNameDeimiterIsEnabled; }
            set
            {
                SetProperty(ref fileNameDeimiterIsEnabled, value);
            }
        }
        #endregion

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

        public DelegateCommand SplitCommand { get; set; }

        public DelegateCommand AveragePagesCommand { get; set; }

        public DelegateCommand AverageFilesCommand { get; set; }

        public DelegateCommand CustomPageRangeCommand { get; set; }

        public DelegateCommand FrontFileNameCommand { get; set; }

        public DelegateCommand FileNameLabelCommand { get; set; }

        public DelegateCommand FileNameDeimiterCommand { get; set; }

        public DelegateCommand FileNameDeimiterChangeCommand { get; set; }

        public DelegateCommand FileNameLabelChangeCommand { get; set; }
        #endregion

        public HomePageSplitDialogViewModel()
        {
            CancelCommand = new DelegateCommand(cancel);
            SplitCommand = new DelegateCommand(split);
            AveragePagesCommand = new DelegateCommand(averagePages);
            AverageFilesCommand = new DelegateCommand(averageFiles);
            CustomPageRangeCommand = new DelegateCommand(customPageRange);
            FrontFileNameCommand = new DelegateCommand(frontFileName);
            FileNameLabelCommand = new DelegateCommand(fileNameLabel);
            FileNameDeimiterCommand = new DelegateCommand(fileNameDeimiter);
            FileNameDeimiterChangeCommand = new DelegateCommand(fileNameDeimiter);
            FileNameLabelChangeCommand = new DelegateCommand(fileNameLabel);


        }
        #region 逻辑函数
        private void FileNameIsCheck()
        {

            if (FileNameDeimiterIsCheck == "True")
            {
                splitModel.FileNameDeimiter = FileNameDeimiterText;
            }
            else
            {
                splitModel.FileNameDeimiter = "";
            }

            if (FileNameDeimiterIsCheck == "True")
            {
                splitModel.FileNameDeimiter = FileNameDeimiterText;
            }
            else
            {
                splitModel.FileNameDeimiter = "";
            }
            if (FileNameLabelIsCheck == "True")
            {
                splitModel.FrontFileName = true;
                splitModel.FileName = (savefilepath + currentViewer.Document.FileName + splitModel.FileNameDeimiter + splitModel.FileNameLabel);
            }
            else
            {
                splitModel.FrontFileName = false;
                splitModel.FileName = (savefilepath + splitModel.FileNameLabel + splitModel.FileNameDeimiter + currentViewer.Document.FileName);
            }
        }

        private void cancel()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        private void split()
        {
            HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref splitModel.PageRange, PageRangeText);
            FileNameIsCheck();
            char[] enumerationSeparator = new char[] { ',' };
            char[] rangeSeparator = new char[] { '-' };
            if (!CommonHelper.GetPagesInRange(ref splitModel.PageParm, splitModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
            { //TODO
                Trace.WriteLine("输入不对");
                MessageBoxEx.Show("输入不对");
                return;
            }
            Trace.WriteLine("splitModel.PageRange: " + splitModel.PageRange + "-----");
            if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
            {
                if (AveragePagesText != "")
                {
                    splitModel.GetModeCount = int.Parse(AveragePagesText);
                }
                int averagepagesremainder = 0;
                var averagepagesdocs = HomePageEditHelper.AverageSplitPages(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagepagesremainder);
                int averagepagesdoc;
                for (averagepagesdoc = 1; averagepagesdoc < averagepagesdocs; averagepagesdoc++)
                {
                    CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                    extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + (splitModel.GetModeCount * averagepagesdoc).ToString());

                    extractdoc.WriteToFilePath(splitModel.FileName + averagepagesdoc.ToString() + ".pdf");

                    extractdoc.Release();
                }
                if (averagepagesdoc == averagepagesdocs)
                {
                    CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                    extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + currentViewer.Document.PageCount.ToString());
                    extractdoc.WriteToFilePath(splitModel.FileName + averagepagesdoc.ToString() + ".pdf");
                }
            }
            if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
            {
                if (AverageFilesText != "")
                {
                    splitModel.GetModeCount = int.Parse(AverageFilesText);
                }
                int averagefilesremainder = 0;
                var averagefilespages = HomePageEditHelper.AverageSplitDocs(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagefilesremainder);
                int averagefilesdoc;
                if (splitModel.GetModeCount > currentViewer.Document.PageCount)
                {
                    splitModel.GetModeCount = 10;
                }
                for (averagefilesdoc = 1; averagefilesdoc <= splitModel.GetModeCount; averagefilesdoc++)
                {
                    CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                    if (averagefilesremainder > 0)
                    {
                        extractdoc.ImportPages(currentViewer.Document, (1 + (averagefilespages + 1) * (averagefilesdoc - 1)).ToString() + "-" + ((averagefilespages + 1) * averagefilesdoc).ToString());
                        extractdoc.WriteToFilePath(splitModel.FileName + averagefilesdoc.ToString() + ".pdf");
                        averagefilesremainder--;
                    }
                    else
                    {
                        extractdoc.ImportPages(currentViewer.Document, (averagefilespages * averagefilesremainder + averagefilesremainder + 1 + averagefilespages * (averagefilesdoc - 1)).ToString() + "-" + (averagefilespages * averagefilesremainder + averagefilesremainder + (averagefilespages * averagefilesdoc)).ToString());
                        extractdoc.WriteToFilePath(splitModel.FileName + averagefilesdoc.ToString() + ".pdf");
                    }
                    extractdoc.Release();
                }
            }
            if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.CustomPageRange)
            {
                CPDFDocument extractdoc = CPDFDocument.CreateDocument();
                extractdoc.ImportPages(currentViewer.Document, splitModel.PageRange);
                extractdoc.WriteToFilePath(splitModel.FileName + "1" + ".pdf");
                if (splitModel.PageRange != "1-" + currentViewer.Document.PageCount.ToString())
                {
                    currentViewer.Document.RemovePages(splitModel.PageParm.ToArray());
                    currentViewer.Document.WriteToFilePath(splitModel.FileName + "2" + ".pdf");
                }
            }
            System.Diagnostics.Process.Start("Explorer", "/select," + currentViewer.Document.FilePath);
            RequestClose.Invoke(new DialogResult(ButtonResult.OK));
        }

        private void averagePages()
        {
            CustomPageRangeIsCheck = "False";
            AverageFilesIsCheck = "False";
            AveragePagesIsCheck = "True";

            CustomPageRangeIsEnabled = "False";
            AverageFilesIsEnabled = "False";
            AveragePagesIsEnabled = "True";
            splitModel.Mode = HomePageSplitDialogModel.SplitMode.AveragePages;
        }

        private void averageFiles()
        {
            CustomPageRangeIsCheck = "False";
            AverageFilesIsCheck = "True";
            AveragePagesIsCheck = "False";

            CustomPageRangeIsEnabled = "False";
            AverageFilesIsEnabled = "True";
            AveragePagesIsEnabled = "False";
            splitModel.Mode = HomePageSplitDialogModel.SplitMode.AverageFiles;
        }

        private void customPageRange()
        {
            CustomPageRangeIsCheck = "True";
            AverageFilesIsCheck = "False";
            AveragePagesIsCheck = "False";

            CustomPageRangeIsEnabled = "True";
            AverageFilesIsEnabled = "False";
            AveragePagesIsEnabled = "False";
            splitModel.Mode = HomePageSplitDialogModel.SplitMode.CustomPageRange;

        }

        private void frontFileName()
        {
            if (FileNameLabelIsCheck == "False")
            {
                FileNameLabelText = "";
            }
            if (FileNameDeimiterIsCheck == "False")
            { FileNameDeimiterText = ""; }
            if (FrontFileIsCheck == "True")
            {
                FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText;
                splitModel.FrontFileName = true;
            }
            else
            {
                splitModel.FrontFileName = false;
                FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename;
            }

        }

        private void fileNameLabel()
        {
            if (FileNameDeimiterIsCheck == "False")
            {
                FileNameDeimiterText = "";
            }
            if (FileNameLabelIsCheck == "True")
            {
                FileNameLabelIsEnabled = "True";
                if (FrontFileIsCheck == "True")
                {
                    FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText;
                    splitModel.FrontFileName = true;
                }
                else
                {
                    splitModel.FrontFileName = false;
                    FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename;
                }
            }
            else
            {
                FileNameLabelIsEnabled = "False";

                if (FrontFileIsCheck == "True")
                {
                    FilePathName = savefilename + FileNameDeimiterText;
                    splitModel.FrontFileName = true;
                }
                else
                {
                    splitModel.FrontFileName = false;
                    FilePathName = FileNameDeimiterText + savefilename;
                }
            }

        }

        private void fileNameDeimiter()
        {

            if (FileNameLabelIsCheck == "False")
            {
                FileNameLabelText = "";
            }
            if (FileNameDeimiterIsCheck == "True")
            {
                FileNameDeimiterIsEnabled = "True";
                if (FrontFileIsCheck == "True")
                {
                    FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText;
                }
                else
                {
                    FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename;
                }
            }
            else
            {
                FileNameDeimiterIsEnabled = "False";
                if (FrontFileIsCheck == "True")
                {
                    FilePathName = savefilename + FileNameLabelText;
                }
                else
                {
                    FilePathName = FileNameLabelText + savefilename;
                }
            }
        }
        #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;
                document = viewer.Document;
                savefilename = currentViewer.Document.FileName;
                FilePathName = savefilename + "-" + "part" + ".pdf";
                savefilepath = filepath.Replace(savefilename + ".pdf", "");
            }
        }
        #endregion
    }
}
