using ComPDFKit.PDFDocument;
using PDF_Office.Model;
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;
using System.Windows;

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

        public string WarningText => "Error PassWord";

        public event Action<IDialogResult> RequestClose;

        public DelegateCommand OKCommand { get; set; }

        public DelegateCommand CancelCommand { get; set; }

        private string input;

        public string Input
        {
            get { return input; }
            set
            {
                SetProperty(ref input, value);
                ShowWarning = Visibility.Collapsed;
            }
        }

        private Visibility showWarning = Visibility.Collapsed;

        public Visibility ShowWarning
        {
            get { return showWarning; }
            set
            {
                SetProperty(ref showWarning, value);
            }
        }



        private CPDFDocument document;

        public VerifyPassWordDialogViewModel()
        {
            OKCommand = new DelegateCommand(OK);
            CancelCommand = new DelegateCommand(Cancel);
        }

        private void OK()
        {
            if (string.IsNullOrEmpty(input))
                return;
            bool result = document.UnlockWithPassword(input.Trim());
            if (!result)
            {
                ShowWarning = Visibility.Visible;
                return;
            }
            var dialogresult = new DialogResult(ButtonResult.OK);
            dialogresult.Parameters.Add(ParameterNames.PassWord, input.Trim());
            RequestClose.Invoke(dialogresult);
        }

        private void Cancel()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
        }


        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            //引用类型的Doc对象 不能释放，会导致报错
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            CPDFDocument doc = null;
            parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
            if (doc != null)
            {
                document = doc;
            }

        }
    }
}
