using ComPDFKit.PDFDocument;
using ComPDFKit_Conversion.Converter;
using ComPDFKit_Conversion.Options;
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.Linq;
using System.Net.Mime;
using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;

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

        public event Action<IDialogResult> RequestClose;

        #region 参数和属性
        private string _pageIndex = "0";

        /// <value>
        /// 当前页
        /// </value>
        public string PageIndex
        {
            get { return _pageIndex; }
            set
            {
                SetProperty(ref _pageIndex, value);
            }
        }

        private string _pageNumber = "";
        /// <value>
        /// 总页数
        /// </value>
        public string PageNumber
        {
            get { return _pageNumber; }
            set
            {
                SetProperty(ref _pageNumber, value);
            }
        }
        #endregion

        public ConverterProgressBarDialogViewModel()
        {

        }
        #region 逻辑函数
        private void SetProgress(int pageIndex)
        {
            PageIndex=pageIndex.ToString();
        }
        #endregion
        
        #region 构架行为

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            ConverterHelper.Clear();

        }

        public async void OnDialogOpened(IDialogParameters parameters)
        {
            bool result=false;
            string convertType="";
            OnProgress GetProgress = null;
            GetProgress += SetProgress;
            parameters.TryGetValue<string>(ParameterNames.ConverterType, out convertType);
            switch (convertType)
            {
                case "CSV":
                    ConverterCSVDialogModel ConverterCSVModel;
                    parameters.TryGetValue<ConverterCSVDialogModel>(ParameterNames.ConverterTypeModel, out ConverterCSVModel);
                    if (ConverterCSVModel != null)
                    {
                        PageNumber = ConverterCSVModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.CSVConvert(ConverterCSVModel.InputPath, ConverterCSVModel.OutputPath, GetProgress, ConverterCSVModel.PageIndexLists, ConverterCSVModel.Pawssword);
                       
                    }
                    break;
                case "Excel":
                    ConverterExcelDialogModel ConverterExcelModel;
                    parameters.TryGetValue<ConverterExcelDialogModel>(ParameterNames.ConverterTypeModel, out ConverterExcelModel);
                    if (ConverterExcelModel != null)
                    {
                        PageNumber = ConverterExcelModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.ExcelConvert(ConverterExcelModel.InputPath, ConverterExcelModel.OutputPath, GetProgress, ConverterExcelModel.PageIndexLists, ConverterExcelModel.Pawssword, ConverterExcelModel.excelOption);
                    }
                    break;
                case "HTML":
                    ConverterHTMLDialogModel ConverterHTMLModel;
                    parameters.TryGetValue<ConverterHTMLDialogModel>(ParameterNames.ConverterTypeModel, out ConverterHTMLModel);
                    if (ConverterHTMLModel != null)
                    {
                        PageNumber = ConverterHTMLModel.PageIndexLists.Count().ToString();
                    }
                    break;
                case "Img":
                    ConverterImgDialogModel ConverterImgModel;
                    parameters.TryGetValue<ConverterImgDialogModel>(ParameterNames.ConverterTypeModel, out ConverterImgModel);
                    if (ConverterImgModel != null)
                    {
                        PageNumber = ConverterImgModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.ImgConvert(ConverterImgModel.InputPath, ConverterImgModel.OutputPath, GetProgress, ConverterImgModel.PageIndexLists, ConverterImgModel.Pawssword, ConverterImgModel.Type);
                    }
                    break;
                case "PPT":
                    ConverterPPTDialogModel ConverterPPTModel;
                    parameters.TryGetValue<ConverterPPTDialogModel>(ParameterNames.ConverterTypeModel, out ConverterPPTModel);
                    if (ConverterPPTModel != null)
                    {
                        PageNumber = ConverterPPTModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.PPTConvert(ConverterPPTModel.InputPath, ConverterPPTModel.OutputPath, GetProgress, ConverterPPTModel.PageIndexLists, ConverterPPTModel.Pawssword);
                    }
                    break;
                case "RTF":
                    ConverterRTFDialogModel ConverterRTFModel;
                    parameters.TryGetValue<ConverterRTFDialogModel>(ParameterNames.ConverterTypeModel, out ConverterRTFModel);
                    if (ConverterRTFModel != null)
                    {
                        PageNumber = ConverterRTFModel.PageIndexLists.Count().ToString();
                    }
                        break;
                case "Text":
                    ConverterTextDialogModel ConverterTextModel;
                    parameters.TryGetValue<ConverterTextDialogModel>(ParameterNames.ConverterTypeModel, out ConverterTextModel);
                    if (ConverterTextModel != null)
                    {
                        PageNumber = ConverterTextModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword);
                    }
                    break;
                case "Word":
                    ConverterWordDialogModel ConverterWordModel;
                    parameters.TryGetValue<ConverterWordDialogModel>(ParameterNames.ConverterTypeModel, out ConverterWordModel);
                    if (ConverterWordModel != null) {
                        PageNumber = ConverterWordModel.PageIndexLists.Count().ToString();
                        result = await ConverterHelper.WordConvert(ConverterWordModel.InputPath, ConverterWordModel.OutputPath, GetProgress, ConverterWordModel.PageIndexLists, ConverterWordModel.Pawssword);
                    }
                    break;
                default:
                    break;
            }
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }
        #endregion
    }
}
