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.Bates
{
    public class BatesContentViewModel : BindableBase,INavigationAware
    {
        public IEventAggregator eventAggregator;
        public IRegionManager batesRegion;

        private CPDFViewer PDFViewer;

        private ViewContentViewModel viewContentViewModel;

        public string TemplateListName = "BatesTemplateListContent";
        public string CreateName = "BatesCreateContent";

        public string BatesDocumentName = "BatesDocumentContent";

        public string BatesType = "BatesType";

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

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

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

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

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

        public string Unicode = null;

        public BatesContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.batesRegion = regionManager;
            BatesSettingsRegionName = Guid.NewGuid().ToString();
            BatesDocumentRegionName = Guid.NewGuid().ToString();
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;

            CloseEditToolCommand = new DelegateCommand(CloseEditTool);
            ConfirmEditToolCommand = new DelegateCommand(ConfirmEditTool);
            eventAggregator.GetEvent<EditBatesHeaderFooterTemplateItemEvent>().Subscribe(EditBatesTemplateItem, e => e.Unicode == Unicode&&e.Type== BatesType);
            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);
            batesRegion.RequestNavigate(BatesSettingsRegionName, SelectedContentName, param);
            BatesSettingsVisible = Visibility.Visible;
        }

        public void EditBatesTemplateItem(BatesHeaderFooterItemUnicode batesItemunicode)
        {
            BatesHeaderFooterItem batesItem = batesItemunicode.Status;
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("BatesItem", batesItem);
            batesRegion.RequestNavigate(BatesSettingsRegionName, CreateName, param);
            BatesSettingsVisible = Visibility.Visible;
        }

        public void EnterDocumentContent()
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
            batesRegion.RequestNavigate(BatesDocumentRegionName, BatesDocumentName, param);
            BatesDocumentVisible = Visibility.Visible;
        }

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

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