using ComPDFKit.PDFDocument;
using PDF_Office.Model.Dialog.ToolsDialogs;
using PDF_Office.Model;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows;
using DialogResult = Prism.Services.Dialogs.DialogResult;
using MessageBox = System.Windows.Forms.MessageBox;

namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs
{
    public class CompressDialogViewModel : BindableBase, IDialogAware
    {
        #region 参数和属性
        private CompressDialogModel compressDialogModel;
        private IntPtr compressingIntpr = IntPtr.Zero;
        private CPDFDocument document;
        private Visibility _compressLargeStyle = Visibility.Hidden;
        public IDialogService dialogs;

        public Visibility CompressLargeStyle
        {
            get { return _compressLargeStyle; }
            set
            {
                SetProperty(ref _compressLargeStyle, value);
            }
        }

        private Visibility _compressStandardStyle = Visibility.Hidden;
        public Visibility CompressStandardStyle
        {
            get { return _compressStandardStyle; }
            set
            {
                SetProperty(ref _compressStandardStyle, value);
            }
        }
        private Visibility _compressLittleStyle = Visibility.Hidden;
        public Visibility CompressLittleStyle
        {
            get { return _compressLittleStyle; }
            set
            {
                SetProperty(ref _compressLittleStyle, value);
            }
        }
        private Visibility _compressMicroStyle = Visibility.Hidden;
        public Visibility CompressMicroStyle
        {
            get { return _compressMicroStyle; }
            set
            {
                SetProperty(ref _compressMicroStyle, value);
            }
        }
        #endregion

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

        public DelegateCommand StandardQualityCommand { get; set; }

        public DelegateCommand LittleQualityCommand { get; set; }

        public DelegateCommand MicroQualityCommand { get; set; }

        public DelegateCommand CompressCommand { get; set; }

        public DelegateCommand ConfirmCompressCommand { get; set; }
        #endregion

        public CompressDialogViewModel(IDialogService dialogService)
        {
            LargeQualityCommand = new DelegateCommand(LargeQuality);
            StandardQualityCommand = new DelegateCommand(StandardQuality);
            LittleQualityCommand = new DelegateCommand(LittleQuality);
            MicroQualityCommand = new DelegateCommand(MicroQuality);
            CompressCommand = new DelegateCommand(Compress);
            ConfirmCompressCommand = new DelegateCommand(ConfirmCompress);
            dialogs = dialogService;
        }

        #region 逻辑函数
        private void LargeQuality()
        {
            compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLarge;
            CompressLargeStyle = Visibility.Visible;
            CompressStandardStyle = Visibility.Hidden;
            CompressLittleStyle = Visibility.Hidden;
            CompressMicroStyle = Visibility.Hidden;
        }
        private void StandardQuality()
        {
            compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusStandard;
            CompressLargeStyle = Visibility.Hidden;
            CompressStandardStyle = Visibility.Visible;
            CompressLittleStyle = Visibility.Hidden;
            CompressMicroStyle = Visibility.Hidden;
        }
        private void LittleQuality()
        {
            compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLittle;
            CompressLargeStyle = Visibility.Hidden;
            CompressStandardStyle = Visibility.Hidden;
            CompressLittleStyle = Visibility.Visible;
            CompressMicroStyle = Visibility.Hidden;
        }
        private void MicroQuality()
        {
            compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusMicro;
            CompressLargeStyle = Visibility.Hidden;
            CompressStandardStyle = Visibility.Hidden;
            CompressLittleStyle = Visibility.Hidden;
            CompressMicroStyle = Visibility.Visible;
        }

        private void ConfirmCompress()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        private int GetIndex(int pageindex)
        {
            Trace.WriteLine(pageindex);
            return 0;
        }
        private void Compress()
        {

            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            /*
             *设置这个对话框的起始保存路径
             */
            sfd.InitialDirectory = document.FilePath;
            /*
             *设置保存的文件的类型，注意过滤器的语法 例子：“文件类型|*.后缀名；*.后缀名；”
             */
            sfd.Filter = "PDF|*.pdf;";
            /*
             *调用ShowDialog()方法显示该对话框，该方法的返回值代表用户是否点击了确定按钮
             **/
            sfd.FileName = document.FileName + "_CompressFile.pdf";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                /*
                 * 做一些工作
                 */

                Trace.WriteLine("compressDialogModel.CompressQuality:  " + compressDialogModel.CompressQuality);
                DialogParameters value = new DialogParameters();
                value.Add(ParameterNames.PDFDocument, document);
                value.Add(ParameterNames.FilePath, sfd.FileName);
                value.Add("compressDialogModel.CompressQuality", (int)compressDialogModel.CompressQuality);
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                dialogs.ShowDialog(DialogNames.CompressProgressBarDialog, value, e =>
                {

                });
            }
            else
            {
                MessageBox.Show("Cancel.");
            }
        }
        #endregion

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

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            CPDFDocument doc = null;
            parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
            if (doc != null)
            {
                CompressDialogModel compressdialogmodel = new CompressDialogModel();
                document = doc;
                compressDialogModel = compressdialogmodel;
                compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusDefault;
            }
        }
        #endregion
    }
}
