using Dropbox.Api.Sharing;
using PDF_Master.EventAggregators;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using Visibility = System.Windows.Visibility;
using Dropbox.Api.FileProperties;
using ComPDFKitViewer.PdfViewer;
using PDF_Master.Model;
using PDF_Master.Model.EditTools.Background;
using PDFSettings;
using PDF_Master.CustomControl;
using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
using Prism.Services.Dialogs;
using System.Collections.Generic;

namespace PDF_Master.ViewModels.EditTools.Background
{
    public class BackgroundContentViewModel : BindableBase, INavigationAware
    {
        public IEventAggregator eventAggregator;
        public IRegionManager backgroundRegion;
        public IDialogService dialogs;

        private CPDFViewer PDFViewer;

        private ViewContentViewModel viewContentViewModel;

        public string TemplateListBaseName = "BackgroundTemplateListBaseContent";
        public string CreateBaseName = "BackgroundCreateBaseContent";

        public string CreateModColorName = "BackgroundCreateColorContent";
        public string CreateModFileName = "BackgroundCreateFileContent";

        public string TemplateListModColorName = "BackgroundTemplateListColorContent";
        public string TemplateListModFileName = "BackgroundTemplateListFileContent";

        public string BackgroundDocumentName = "BackgroundDocumentContent";

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

        private Visibility _backgroundSettingsVisible = Visibility.Collapsed;
        /// <summary>
        /// 属性设置Region可见
        /// </summary>
        public Visibility BackgroundSettingsVisible
        {
            get { return _backgroundSettingsVisible; }
            set { _backgroundSettingsVisible = value; }
        }

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

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

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

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

        /// <summary>
        /// 退出EditTool
        /// </summary>
        public DelegateCommand CloseEditToolCommand { get; set; }
        public DelegateCommand ConfirmEditToolCommand { get; set; }
        public DelegateCommand<string> EnterSelectedContentCommand { get; set; }
        public DelegateCommand BatchBackgroundCommand { get; set; }
        public DelegateCommand DeleteBackgroundCommand { get; set; }

        public string Unicode = null;

        public BackgroundContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogs)
        {
            this.eventAggregator = eventAggregator;
            this.backgroundRegion = regionManager;
            this.dialogs = dialogs;
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            BackgroundSettingsVisible = Visibility.Visible;
            BackgroundSettingsRegionName = Guid.NewGuid().ToString();
            BackgroundDocumentRegionName = Guid.NewGuid().ToString();
            BatchBackgroundCommand = new DelegateCommand(BatchBackground);
            CloseEditToolCommand = new DelegateCommand(CloseEditTool);
            ConfirmEditToolCommand = new DelegateCommand(ConfirmEditTool);
            EnterSelectedContentCommand = new DelegateCommand<string>(EnterSelectedContent);
            DeleteBackgroundCommand = new DelegateCommand(DeleteBackground);

            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<EditBackgroundTemplateItemEvent>().Subscribe(EditBackgroundTemplateItem, 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<ConfirmEditToolsBackgroundEvent>().Publish(Unicode);
            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(TemplateListBaseName);
            }
            else
            {
                EnterSelectedContent(CreateBaseName);
            }
        }

        public void SetCurrentCreateMod(stringUnicode currentCreateModNameunicode)
        {
            string currentCreateModName = currentCreateModNameunicode.Status;
            if (currentCreateModName == CreateModColorName)
            {
                CurrentCreateMod = EnumColorOrFile.StatusColor;
            }
            else if (currentCreateModName == CreateModFileName)
            {
                CurrentCreateMod = EnumColorOrFile.StatusFile;
            }
        }

        public void EditBackgroundTemplateItem(BackgroundItemUnicode backgroundItemunicode)
        {
            BackgroundItem backgroundItem = backgroundItemunicode.Status;
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
            param.Add("BackgroundItem", backgroundItem);
            backgroundRegion.RequestNavigate(BackgroundSettingsRegionName, CreateBaseName, param);
            BackgroundSettingsVisible = Visibility.Visible;
        }

        public void SetCurrentTemplateListMod(stringUnicode currentTemplateListModNameunicode)
        {
            string currentTemplateListModName = currentTemplateListModNameunicode.Status;
            if (currentTemplateListModName == TemplateListModColorName)
            {
                CurrentTemplateListMod = EnumColorOrFile.StatusColor;
            }
            else if (currentTemplateListModName == TemplateListModFileName)
            {
                CurrentTemplateListMod = EnumColorOrFile.StatusFile;
            }
        }

        public void EnterSelectedContent(string SelectedContentName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            if (SelectedContentName == TemplateListBaseName)
            {
                param.Add("CurrentCreateModName", CurrentCreateMod);
            }
            else if (SelectedContentName == CreateBaseName)
            {
                param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
            }
            backgroundRegion.RequestNavigate(BackgroundSettingsRegionName, SelectedContentName, param);
            BackgroundSettingsVisible = Visibility.Visible;
        }

        private void DeleteBackground()
        {
            AlertsMessage alertsMessage = new AlertsMessage();
            alertsMessage.ShowDialog("确定要删除背景吗?", "", "取消", "删除");
            if (alertsMessage.result == ContentResult.Ok)
            {
                this.eventAggregator.GetEvent<DeleteBackgroundEvent>().Publish(new EnumDeleteUnicode
                {
                    Unicode = Unicode,
                    Status = EnumDelete.StatusDeleteAll
                });
            }
            else
            {
                this.eventAggregator.GetEvent<DeleteBackgroundEvent>().Publish(new EnumDeleteUnicode
                {
                    Unicode = Unicode,
                    Status = EnumDelete.StatusCreate
                });
            }
        }

        private void BatchBackground()
        {
            DialogParameters backgroundpdf = new DialogParameters();
            backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
            HomePageBatchProcessingDialogModel.FilePaths = new List<string> { PDFViewer.Document.FilePath.ToString() };
            HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
            backgroundpdf.Add(ParameterNames.FilePath, new string[] { PDFViewer.Document.FilePath.ToString() });
            dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { EnterSelectedContent(TemplateListBaseName); });
        }

        public void EnterDocumentContent()
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
            backgroundRegion.RequestNavigate(BackgroundDocumentRegionName, BackgroundDocumentName, param);
            BackgroundDocumentVisible = Visibility.Visible;
        }

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

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
    }
}