using ComPDFKit.PDFDocument;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System; 
using System.Windows.Forms;

namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
{
    public class DeleteSafetySettingsDialogViewModel : BindableBase, IDialogAware
    {
        #region 参数和属性
        DeleteSafetySettintgsModel deleteSafetySettintgsModel;
        public IDialogService dialogs;
        private CPDFDocument document;
        #endregion

        #region 委托声明
        public DelegateCommand DelegateCheckPasswordCommand { get; set; }
        public DelegateCommand DelegateCancelCommand { get; set; }
        #endregion

        public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
        {
            dialogs = dialogService;
            DelegateCheckPasswordCommand = new DelegateCommand(OpenCheckPasswordDialog);
            DelegateCancelCommand = new DelegateCommand(Cancel);
        }

        #region 检查函数
        private bool CheckHaveAllPermissions()
        {
            CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
            if (permissionsInfo.AllowsDocumentChanges &&
                permissionsInfo.AllowsPrinting &&
                permissionsInfo.AllowsHighQualityPrinting &&
                permissionsInfo.AllowsCopying &&
                //permissionsInfo.AllowsDocumentAssembly&&
                //TODO: 这个权限有问题
                permissionsInfo.AllowsCommenting)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检测是否需要输入密码
        /// </summary>
        /// <returns>
        /// EnumNeedPassword.StatusVerifyPassword：需要确认密码；
        /// EnumNeedPassword.StatusDirectDescrypt：直接执行操作
        /// EnumNeedPassword.StatusCannotDecrypt：无可执行操作。
        /// </returns>
        private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
        {
            if (document.IsLocked)
            {
                return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
            }
            else { }
            if (document.IsEncrypted)
            {
                if (CheckHaveAllPermissions())
                {
                    return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
                }
                else
                {
                    return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
                }
            }
            else
            {
                return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
            }
        }
        #endregion

        #region 逻辑函数
        private void OpenCheckPasswordDialog()
        {
            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 (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword)
            {
                bool IfDiscryptied = false;
                DialogParameters value = new DialogParameters();
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
                dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { IfDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
                if (IfDiscryptied)
                {
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {

                        document.Descrypt(sfd.FileName);
                        MessageBox.Show("保存成功");
                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
                    }
                    else
                    {
                        MessageBox.Show("取消保存");
                    }
                }
                else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
                {
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        document.Descrypt(sfd.FileName);
                        MessageBox.Show("Ok");
                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
                    }
                    else
                    {
                        MessageBox.Show("取消保存");
                    }
                }
                else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
                {
                    MessageBox.Show("无可用安全性设置");
                }
                else
                { }
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
            }
        }

        private void Cancel()
        {
            RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.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)
            {
                document = doc;
            }
        }
        #endregion
    }
}
