using ComPDFKitViewer.PdfViewer;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
using PDF_Office.Model.PDFTool;
using PDFSettings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace PDF_Office.ViewModels.HomePanel.PDFTools
{
    public class QuickToolsContentViewModel : BindableBase
    {
        #region 属性
        /// <summary>
        /// 扩展/收缩
        /// </summary>
        private bool _isExpendTools = false;
        public bool IsExpendTools
        {
            get { return _isExpendTools; }
            set { SetProperty(ref _isExpendTools, value); }
        }

        private string regionName;

        public string ToolRegionName
        {
            get { return regionName; }
            set { SetProperty(ref regionName, value); }
        }

        #endregion

        #region Command and Event
        public IDialogService dialogs;
        public DelegateCommand<ToolItem> QuickToolsCommand { get; set; }
        public DelegateCommand<object> OpenMenuCommand { get; set; }
        public DelegateCommand ExpendCommand { get; set; }
        public DelegateCommand ShowToolCommand { get; set; }

        public IRegionManager toolregion;
        public event EventHandler<bool> ExpendToolsHanlder;
        #endregion

        public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
        {
            toolregion = regionManager;
            ToolRegionName = RegionNames.ToolRegionName;
            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                toolregion.RequestNavigate(ToolRegionName, "Guid");
            }));

            dialogs = dialogService;

            InitVariable();
            InitCommand();
            
        }

        #region 初始化和绑定
        private void InitVariable()
        {

        }

        private void InitCommand()
        {
            QuickToolsCommand = new DelegateCommand<ToolItem>(QuickTools_Click);
            OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
            ExpendCommand = new DelegateCommand(Expend);
            ShowToolCommand = new DelegateCommand(ShowTool);
        }

        #endregion

        private void ShowTool()
        {
            toolregion.RequestNavigate(ToolRegionName, "Tools");
        }


        public void QuickTools_Click(ToolItem toolItem)
        {
            /*
             *设置这个对话框的起始保存路径
             */
            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "PDF|*.pdf;*.PDF;";
            if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security)
            {
                dlg.Multiselect = true;
            }
            if (toolItem.FnType == PDFFnType.Merge)
            {
                dlg.Multiselect = true;
                dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*bmp;*jpeg;*gif;*tiff;";
            }
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                CPDFViewer viewer = new CPDFViewer();

                switch ((PDFFnType)toolItem.FnType)
                {
                    case PDFFnType.Split:

                        viewer.InitDocument(dlg.FileName);
                        DialogParameters splitvalue = new DialogParameters();
                        splitvalue.Add(ParameterNames.PDFViewer, viewer);
                        splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
                        dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
                        break;

                    case PDFFnType.Extract:
                        viewer.InitDocument(dlg.FileName);
                        DialogParameters extractvalue = new DialogParameters();
                        extractvalue.Add(ParameterNames.PDFViewer, viewer);
                        extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
                        dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
                        break;

                    case PDFFnType.Insert:
                        viewer.InitDocument(dlg.FileName);
                        DialogParameters insertvalue = new DialogParameters();
                        insertvalue.Add(ParameterNames.PDFViewer, viewer);
                        insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
                        dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
                        break;

                    case PDFFnType.Compress:
                        DialogParameters compresspdf = new DialogParameters();
                        compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
                        HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
                        HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
                        compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
                        dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
                        break;

                    case PDFFnType.Merge:
                        DialogParameters picturetopdf = new DialogParameters();
                        picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
                        dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
                        break;

                    case PDFFnType.Print:
                        viewer.InitDocument(dlg.FileName);
                        DialogParameters printvalue = new DialogParameters();
                        printvalue.Add(ParameterNames.PDFViewer, viewer);
                        printvalue.Add(ParameterNames.FilePath, dlg.FileName);
                        dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
                        break;

                    case PDFFnType.Security:
                        DialogParameters securitypdf = new DialogParameters();
                        securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
                        HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
                        HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
                        securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
                        dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });

                        break;
                    case PDFFnType.ConvertPDF:
                        DialogParameters convertpdf = new DialogParameters();
                        convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
                        HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
                        HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
                        convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
                        dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });

                        break;
                }

            }
        }



        private void Expend()
        {
            IsExpendTools = !IsExpendTools;
            ExpendToolsHanlder?.Invoke(null, IsExpendTools);
        }

        private void OpenMenu(object obj)
        {
            var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
            var btn = obj as Button;
            if (menu != null && btn != null)
            {
                if(menu.Items.Count == 1)
                {
                  var item = menu.Items[0] as MenuItem;
                    if(_isExpendTools)
                    {
                        item.Header = "收缩";
                    }
                    else
                    {
                        item.Header = "展开";
                    }

                    item.Command = ExpendCommand;
                    //btn.ContextMenu = menu;
                    menu.PlacementTarget = btn;
                    menu.IsOpen = true;
                }
            }
        }



    }
}
