using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.HomePageToolsDialogs;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
{
    public class SplitDialogViewModel : BindableBase, IDialogAware
    {
        public string Title => "";

        private CPDFViewer pdfViewer; 

        private double pageCount;
        //页面总数
        public double PageCount
        {
            get { return pageCount; }
            set
            {
                SetProperty(ref pageCount, value);
            }
        }

        private string customPageText;
        /// <summary>
        /// 自定义的输入内容
        /// </summary>
        public string CustomPageText 
        {
            get { return customPageText; }
            set
            {
                SetProperty(ref customPageText, value);
            }
        }


        private HomePageSplitDialogModel model;

        public HomePageSplitDialogModel Model
        {
            get { return model; }
            set
            {
                SetProperty(ref model, value);
            }
        }

        public event Action<IDialogResult> RequestClose;

        public DelegateCommand CancelCommand { get; set; }

        public DelegateCommand SplitCommnad { get; set; }

        public SplitDialogViewModel()
        {
            CancelCommand = new DelegateCommand(cancel);
            SplitCommnad = new DelegateCommand(split);

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

        private void split()
        {
            List<int> pages = new List<int>();
            var result = CommonHelper.GetPagesInRange(ref pages,CustomPageText,(int)pageCount,new char[]{ '-'},new char[] { ','});
            if(!result)
            {
                AlertsMessage alertsMessage = new AlertsMessage();
                alertsMessage.ShowDialog("","请输入有效范围","OK");
                return;
            }
            Model.PageRange = customPageText;
            DialogParameters valuePairs = new DialogParameters();
            valuePairs.Add(ParameterNames.DataModel,Model);
            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
        }


        #region
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
           
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer,out pdfViewer);
            if(pdfViewer!=null)
            {
                PageCount = pdfViewer.Document.PageCount;
                Model.SourceFileName = System.IO.Path.GetFileName(pdfViewer.Document.FileName);
            }
        }
        #endregion
    }
}
