using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.ConverterDialogs;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DialogResult = Prism.Services.Dialogs.DialogResult;

namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
{
    public class ConverterRTFDialogViewModel : BindableBase, IDialogAware
    {

        public string Title => "";

        public event Action<IDialogResult> RequestClose;

        #region 参数和属性
        public ConverterRTFDialogModel ConverterRTFModel = new ConverterRTFDialogModel();

        public CPDFViewer currentViewer;

        public IDialogService dialogs;

        public string CurrentPageIndex = "1";

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

        public string PageRangeSelectIndex { set; get; } = "0";
        #endregion
        #region 委托声明
        public DelegateCommand CancelCommand { get; set; }

        public DelegateCommand ConverterCommnad { get; set; }

        public DelegateCommand<object> CmbPageSelectionChanged { get; set; }

        public DelegateCommand<object> CmbPageTextChanged { get; set; }
        #endregion

        public ConverterRTFDialogViewModel(IDialogService dialogService)
        {
            CancelCommand = new DelegateCommand(cancel);
            ConverterCommnad = new DelegateCommand(converter);
            CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
            CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
            dialogs = dialogService;
        }

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

        private void converter()
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ConverterRTFModel.OutputPath = dlg.SelectedPath.Trim();
            }
            HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterRTFModel.PageRange, PageRangeText, true, CurrentPageIndex);
            char[] enumerationSeparator = new char[] { ',' };
            char[] rangeSeparator = new char[] { '-' };
            if (!CommonHelper.GetPagesInRange(ref ConverterRTFModel.PageIndexLists, ConverterRTFModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
            { //TODO
                Trace.WriteLine("输入不对");
                MessageBoxEx.Show("输入不对");
                return;
            }
            //result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, ConverterTextModel.GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword);
            DialogParameters value = new DialogParameters();
            value.Add(ParameterNames.ConverterType, "RTF");
            value.Add(ParameterNames.ConverterTypeModel, ConverterRTFModel);
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
            {

            });
        }

        private void CmbPageSelectionChangedEvent(object e)
        {
            ///这里采用的是将预览UI控件传递过来的方式，为下下策
            ///正确的方式应该是 通过声明一些属性，再通过绑定来更新预览控件对应值的形式
            ///但是目前发现自定义控件的依赖属性绑定有些问题，因此先用此方法，将业务逻辑代码先调整到VM里
            var ConverterPreview = e as PageTurningPreview;
            if (ConverterPreview != null)
            {
                string PageRangeSelectIndex = this.PageRangeSelectIndex;
                var currentViewer = this.currentViewer;
                string PageRange = "";
                var PageRangeText = this.PageRangeText;
                if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
                {
                    HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
                    char[] enumerationSeparator = new char[] { ',' };
                    char[] rangeSeparator = new char[] { '-' };
                    if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
                    { //TODO
                        ConverterPreview.PageIndexLists.Add(0);
                        Trace.WriteLine("输入不对");
                        MessageBoxEx.Show("输入不对");
                        return;
                    }

                }
                if (PageRangeSelectIndex == "1")
                {
                    char[] enumerationSeparator = new char[] { ',' };
                    char[] rangeSeparator = new char[] { '-' };
                    if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (ConverterPreview.PageIndexLists[ConverterPreview.CurrentIndex] + 1).ToString(), currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
                    { //TODO
                        Trace.WriteLine("输入不对");
                        MessageBoxEx.Show("输入不对");
                        return;
                    }
                    this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
                }
                ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
                ConverterPreview.CurrentIndex = 0;
            }
        }

        private void CmbPageTextChangedEvent(object e)
        {
            var ConverterPreview = e as PageTurningPreview;
            if (ConverterPreview != null)
            {
                if (ConverterPreview != null)
                {
                    string PageRangeSelectIndex = this.PageRangeSelectIndex;
                    var currentViewer = this.currentViewer;
                    string PageRange = "";
                    var PageRangeText = this.PageRangeText;
                    HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
                    char[] enumerationSeparator = new char[] { ',' };
                    char[] rangeSeparator = new char[] { '-' };

                    if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
                    { //TODO
                        ConverterPreview.PageIndexLists.Add(0);

                        return;
                    }
                    else
                    {
                        ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
                        ConverterPreview.CurrentIndex = 0;
                    }
                }
            }
        }
        #endregion

        #region 构架行为

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            CPDFViewer pdfViewer = null;
            parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
            if (pdfViewer != null)
            {
                currentViewer = pdfViewer;
                ConverterRTFModel.InputPath = pdfViewer.Document.FilePath;
                FileInfo fileinfo = new FileInfo(ConverterRTFModel.InputPath);
                ConverterRTFModel.OutputPath = fileinfo.DirectoryName;
            }
        }
        #endregion
    }
}
