using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.EventAggregators;
using PDF_Office.Model;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.Model.EditTools.Watermark;
using PDF_Office.ViewModels.Tools;
using PDFSettings;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PDF_Office.ViewModels.EditTools.Watermark
{
    public class WatermarkContentViewModel : BindableBase, INavigationAware
    {
        public IEventAggregator eventAggregator;
        public IRegionManager watermarkRegion;

        private CPDFViewer PDFViewer;

        private CPDFViewer CurrentPDFViewer;

        private ViewContentViewModel viewContentViewModel;

        public string TemplateListName = "WatermarkTemplateListBaseContent";
        public string CreateBaseName = "WatermarkCreateBaseContent";
        public string CreateModTextName = "WatermarkCreateTextContent";
        public string CreateModFileName = "WatermarkCreateFileContent";

        public string TemplateListModTextName = "WatermarkTemplateListTextContent";
        public string TemplateListModFileName = "WatermarkTemplateListFileContent";

        public string WatermarkDocumentName = "WatermarkDocumentContent";

        private string _watermarkSettingsRegionName;
        public string WatermarkSettingsRegionName
        {
            get { return _watermarkSettingsRegionName; }
            set { _watermarkSettingsRegionName = value; }
        }

        private Visibility _watermarkSettingsVisible = Visibility.Collapsed;
        public Visibility WatermarkSettingsVisible
        {
            get { return _watermarkSettingsVisible; }
            set { _watermarkSettingsVisible = value; }
        }

        private string _watermarkDocumentRegionName;
        /// <summary>
        /// 持有Document的Region，负责渲染和保存
        /// </summary>
        public string WatermarkDocumentRegionName
        {
            get { return _watermarkDocumentRegionName; }
            set { _watermarkDocumentRegionName = value; }
        }

        private Visibility _watermarkDocumentVisible = Visibility.Visible;
        /// <summary>
        /// 持有Document的Region可见
        /// </summary>
        public Visibility WatermarkDocumentVisible
        {
            get { return _watermarkDocumentVisible; }
            set { _watermarkDocumentVisible = value; }
        }

        private EnumTextOrFile _currentCreateMod;
        public EnumTextOrFile CurrentCreateMod
        {
            get { return _currentCreateMod; }
            set { _currentCreateMod = value; }
        }

        private EnumTextOrFile _currentTemplateListMod;
        public EnumTextOrFile CurrentTemplateListMod
        {
            get { return _currentTemplateListMod; }
            set { _currentTemplateListMod = value; }
        }

        public DelegateCommand CloseEditToolCommand { get; set; }

        public DelegateCommand DeleteWatermarkCommand { get; set; }

        public DelegateCommand ConfirmEditToolCommand { get; set; }

        public DelegateCommand<string> EnterSelectedContentCommand { get; set; }

        public string Unicode = null;

        public WatermarkContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.watermarkRegion = regionManager;
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            WatermarkSettingsVisible = Visibility.Visible;
            WatermarkSettingsRegionName = Guid.NewGuid().ToString();
            WatermarkDocumentRegionName = Guid.NewGuid().ToString();
            CloseEditToolCommand = new DelegateCommand(CloseEditTool);
            ConfirmEditToolCommand = new DelegateCommand(ConfirmEditTool);
            EnterSelectedContentCommand = new DelegateCommand<string>(EnterSelectedContent);
            DeleteWatermarkCommand = new DelegateCommand(DeleteWatermark);

            eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
            eventAggregator.GetEvent<SetCurrentCreateModEvent>().Subscribe(SetCurrentCreateMod, e => e.Unicode == Unicode);
            eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Subscribe(SetCurrentTemplateListMod, e => e.Unicode == Unicode);
            eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
            eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Subscribe(EditWatermarkTemplateItem, e => e.Unicode == Unicode);

        }
        public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicod)
        {
            CurrentPDFViewer = cPDFViewerunicod.Status;

        }
        public void CloseEditTool()
        {
            this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = Unicode, Status = EnumCloseMode.StatusCancel });

        }

        public void ConfirmEditTool()
        {
            this.eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Publish();
            this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = Unicode, Status = EnumCloseMode.StatusConfirm });
        }

        public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
        {
            EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
            if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
            {
                EnterSelectedContent(TemplateListName);
            }
            else
            {
                EnterSelectedContent(CreateBaseName);
            }
        }

        public void EditWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
        {
            WatermarkItem watermarkItem = watermarkItemunicode.Status;
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
            param.Add("WatermarkItem", watermarkItem);
            watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, CreateBaseName, param);
            WatermarkSettingsVisible = Visibility.Visible;
        }

        public void SetCurrentCreateMod(stringUnicode currentCreateModNameunicode)
        {
            string currentCreateModName = currentCreateModNameunicode.Status;
            if (currentCreateModName == CreateModTextName)
            {
                CurrentCreateMod = EnumTextOrFile.StatusText;
            }
            else if (currentCreateModName == CreateModFileName)
            {
                CurrentCreateMod = EnumTextOrFile.StatusFile;
            }
        }

        public void SetCurrentTemplateListMod(stringUnicode currentTemplateListModNameunicode)
        {
            string currentTemplateListModName = currentTemplateListModNameunicode.Status;
            if (currentTemplateListModName == TemplateListModTextName)
            {
                CurrentTemplateListMod = EnumTextOrFile.StatusText;
            }
            else if (currentTemplateListModName == TemplateListModFileName)
            {
                CurrentTemplateListMod = EnumTextOrFile.StatusFile;
            }
        }

        public void EnterSelectedContent(string SelectedContentName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            if (SelectedContentName == TemplateListName)
            {
                param.Add("CurrentCreateModName", CurrentCreateMod);
            }
            else if (SelectedContentName == CreateBaseName)
            {
                param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
            }
            watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, SelectedContentName, param);
            WatermarkSettingsVisible = Visibility.Visible;
        }

        public void EnterDocumentContent()
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
            watermarkRegion.RequestNavigate(WatermarkDocumentRegionName, WatermarkDocumentName, param);
            WatermarkDocumentVisible = Visibility.Visible;
        }

        private void DeleteWatermark() {
            AlertsMessage alertsMessage = new AlertsMessage();
            if (CurrentPDFViewer.Document.GetWatermarkCount()> 0)
            {
                alertsMessage.ShowDialog("确定要删除文件水印吗？", "", "取消", "删除");
                if (alertsMessage.result == ContentResult.Ok)
                {
                    this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode {
                        Unicode = Unicode, Status = EnumDelete.StatusDeleteAll});
                }
                else
                {
                    this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode {
                            Unicode = Unicode, Status = EnumDelete.StatusCreate});
                }
            }
            else {

                alertsMessage.ShowDialog("无法在本文件中找到可删除的水印。如果您看到水印，其不是使用PDF Office添加的，因此无法被检测到。", "", "取消", "确定");
            }
            
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
            EnterSelectedContent(TemplateListName);
            EnterDocumentContent();

        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
    }
}
