using ComPDFKitViewer.PdfViewer;
using PDF_Office.CustomControl;
using PDF_Office.EventAggregators;
using PDF_Office.Model;
using PDF_Office.ViewModels.Tools;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Button = System.Windows.Controls.Button;

namespace PDF_Office.ViewModels.EditTools.Watermark
{
    public class WatermarkTemplateListBaseContentViewModel : BindableBase, INavigationAware
    {
        public IRegionManager watermarkTemplateListRegion;
        private readonly IEventAggregator eventAggregator;

        private CPDFViewer CurrentPDFViewer;

        private string WatermarkTemplateListTextContentName = "WatermarkTemplateListTextContent";
        private string WatermarkTemplateListFileContentName = "WatermarkTemplateListFileContent";

        public bool IsFirstEnter = true;

        public EnumTextOrFile CurrentCreateMod;

        private string _currentTemplateListModName;
        public string CurrentTemplateListModName
        {
            get { return _currentTemplateListModName; }
            set { _currentTemplateListModName = value; }
        }

        private string watermarkTemplateListRegionName;

        public string WatermarkTemplateListRegionName
        {
            get => watermarkTemplateListRegionName;
            set => SetProperty(ref watermarkTemplateListRegionName, value);
        }


        private System.Windows.Visibility watermarkTemplateListVisible;
        public System.Windows.Visibility WatermarkTemplateListVisible
        {
            get => watermarkTemplateListVisible;
            set => SetProperty(ref watermarkTemplateListVisible, value);
        }

        public DelegateCommand<object> ChangeTemplateListModCommand { get; set; }
        public DelegateCommand EnterCreateCommand { get; set; }

        public string Unicode = null;

        public WatermarkTemplateListBaseContentViewModel(IRegionManager watermarkTemplateListRegion, IEventAggregator eventAggregator)
        {
            this.watermarkTemplateListRegion = watermarkTemplateListRegion;
            this.eventAggregator = eventAggregator;
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            WatermarkTemplateListRegionName = Guid.NewGuid().ToString();
            ChangeTemplateListModCommand = new DelegateCommand<object>(ChangeTemplateListMod);
            EnterCreateCommand = new DelegateCommand(EnterCreate);
            EnterSelectedTemplateListMod("WatermarkTemplateListColorContent");
            eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);

        }
        public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicode)
        {
            CurrentPDFViewer = cPDFViewerunicode.Status;
        }
        public void EnterCreate()
        {
            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.StatusDeleteInitial
                    });
                }
                else
                {
                    this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
                    {
                        Unicode = Unicode,
                        Status = EnumDelete.StatusCreate
                    });
                }
            }
            this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
            {
                Unicode = Unicode,
                Status = EnumTemplateListOrCreate.StatusCreate
            });

        }

        public void EnterSelectedTemplateListMod(string currentTemplateListName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, CurrentPDFViewer);
            watermarkTemplateListRegion.RequestNavigate(WatermarkTemplateListRegionName, currentTemplateListName, param);
            eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Publish(new CPDFViewerUnicode
            {
                Unicode = Unicode,
                Status = CurrentPDFViewer
            });
            watermarkTemplateListVisible = System.Windows.Visibility.Visible;
        }

        public void ChangeTemplateListMod(object e)
        {
            var args = e as Button;
            if (args != null)
            {
                CurrentTemplateListModName = args.Name;
                EnterSelectedTemplateListMod(CurrentTemplateListModName);
                eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Publish(new stringUnicode
                {
                    Unicode = Unicode,
                    Status = CurrentTemplateListModName
                });
            }
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (IsFirstEnter)
            {
                CurrentTemplateListModName = WatermarkTemplateListTextContentName;
                EnterSelectedTemplateListMod(CurrentTemplateListModName);
                IsFirstEnter = false;
            }
            else
            {
                navigationContext.Parameters.TryGetValue<EnumTextOrFile>("CurrentCreateModName", out CurrentCreateMod);
                if (CurrentCreateMod == EnumTextOrFile.StatusText)
                {
                    CurrentTemplateListModName = WatermarkTemplateListTextContentName;
                }
                else
                {
                    CurrentTemplateListModName = WatermarkTemplateListFileContentName;
                }
                EnterSelectedTemplateListMod(CurrentTemplateListModName);
                eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Publish(new stringUnicode
                {
                    Unicode = Unicode,
                    Status = CurrentTemplateListModName
                });


            }
        }
    }
}
