using PDF_Office.EventAggregators;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using PDFSettings;

namespace PDF_Office.ViewModels.EditTools.HeaderFooter
{
    public class HeaderFooterContentViewModel : BindableBase, INavigationAware
    {
        
        public IEventAggregator eventAggregator;
        public IRegionManager headerFooterRegion;

        private CPDFViewer PDFViewer;

        private ViewContentViewModel viewContentViewModel;

        public string TemplateListName = "HeaderFooterTemplateListContent";
        public string CreateName = "HeaderFooterCreateContent";
        public string HeaderFooterDocumentName = "HeaderFooterDocumentContent";

        public string HeaderFooterType = "HeaderFooterType";

        private string _headerFooterSettingsRegionName;
        /// <summary>
        /// 属性设置Region
        /// </summary>
        public string HeaderFooterSettingsRegionName
        {
            get { return _headerFooterSettingsRegionName; }
            set { _headerFooterSettingsRegionName = value; }
        }

        private Visibility _headerFooterSettingsVisible = Visibility.Visible;
        /// <summary>
        /// 属性设置Region可见
        /// </summary>
        public Visibility HeaderFooterSettingsVisible
        {
            get { return _headerFooterSettingsVisible; }
            set { _headerFooterSettingsVisible = value; }
        }

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

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

        public DelegateCommand CloseEditToolCommand { get; set; }
        public DelegateCommand ConfirmEditToolCommand { get; set; }

        public string Unicode = null;

        public HeaderFooterContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.headerFooterRegion = regionManager;

            HeaderFooterSettingsRegionName = Guid.NewGuid().ToString();
            HeaderFooterDocumentRegionName = Guid.NewGuid().ToString();
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            CloseEditToolCommand = new DelegateCommand(CloseEditTool);
            ConfirmEditToolCommand = new DelegateCommand(ConfirmEditTool);
            eventAggregator.GetEvent<EditBatesHeaderFooterTemplateItemEvent>().Subscribe(EditHeaderFooterTemplateItem, e => e.Unicode == Unicode&&e.Type== HeaderFooterType);
            eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
        }

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

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

        }

        public void EnterSelectedContent(string SelectedContentName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
           
            headerFooterRegion.RequestNavigate(HeaderFooterSettingsRegionName, SelectedContentName, param);
            HeaderFooterSettingsVisible = Visibility.Visible;
        }

        public void EnterDocumentContent()
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
            headerFooterRegion.RequestNavigate(HeaderFooterDocumentRegionName, HeaderFooterDocumentName, param);
            HeaderFooterDocumentVisible = Visibility.Visible;
        }
        public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
        {
            EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
            if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
            {
                EnterSelectedContent(TemplateListName);
            }
            else
            {
                EnterSelectedContent(CreateName);
            }
        }

        public void EditHeaderFooterTemplateItem(BatesHeaderFooterItemUnicode headerfooterItemunicode)
        {
            BatesHeaderFooterItem headerfooterItem = headerfooterItemunicode.Status;
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("BatesItem", headerfooterItem);
            headerFooterRegion.RequestNavigate(HeaderFooterSettingsRegionName, CreateName, param);
            HeaderFooterSettingsVisible = Visibility.Visible;
        }

        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)
        {

        }
    }
}
