using ComPDFKitViewer.PdfViewer;
using PDF_Office.EventAggregators;
using PDF_Office.Model;
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;

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

        private CPDFViewer PDFViewer;

        public EnumTextOrFile CurrentTemplateListMod;

        private string watermarkCreateRegionName;

        public string WatermarkCreateRegionName
        {
            get => watermarkCreateRegionName;
            set => SetProperty(ref watermarkCreateRegionName, value);
        }

        private string _currentCreateName;
        public string CurrentCreateName
        {
            get => _currentCreateName;
            set => _currentCreateName = value;
        }

        private string _currentCreateModName;
        public string CurrentCreateModName
        {
            get => _currentCreateModName;
            set => _currentCreateModName = value;
        }


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

        public DelegateCommand<object> ChangeCreateModCommand { get; set; }
        public DelegateCommand EnterTemplateListCommand { get; set; }

        public WatermarkCreateBaseContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.watermarkCreateRegion = regionManager;
            WatermarkCreateRegionName = Guid.NewGuid().ToString();
            ChangeCreateModCommand = new DelegateCommand<object>(ChangeCreateMod);
            EnterTemplateListCommand = new DelegateCommand(EnterTemplateList);
        }

        public void EnterTemplateList()
        {
            this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
        }

        public void EnterSelectedCreateMod(string currentCreateName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            watermarkCreateRegion.RequestNavigate(WatermarkCreateRegionName, currentCreateName,param);
            watermarkCreateVisible = System.Windows.Visibility.Visible;
        }

        public void ChangeCreateMod(object e)
        {
            var args = e as Button;
            if (args != null)
            {
                CurrentCreateName = args.Name;
                EnterSelectedCreateMod(CurrentCreateName);
                eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(CurrentCreateName);
            }
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
            navigationContext.Parameters.TryGetValue<EnumTextOrFile>("CurrentTemplateListModName", out CurrentTemplateListMod);
            if (CurrentTemplateListMod == EnumTextOrFile.StatusText)
            {
                CurrentCreateModName = "WatermarkCreateTextContent";
            }
            else
            {
                CurrentCreateModName = "WatermarkCreateFileContent";
            }
            EnterSelectedCreateMod(CurrentCreateModName);
            eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(CurrentCreateModName);
        }
    }
}