using ComPDFKitViewer.PdfViewer;
using PDF_Office.EventAggregators;
using PDF_Office.Model;
using PDF_Office.Model.EditTools.Background;
using PDF_Office.Model.EditTools.Watermark;
using PDFSettings;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using static PDF_Office.ViewModels.EditTools.Background.BackgroundCreateBaseContentViewModel;

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

        private CPDFViewer PDFViewer;

        public WatermarkItem WatermarkItem;

        public EnumTextOrFile CurrentTemplateListMod;

        private string watermarkCreateRegionName;

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

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

   

        public enum EnumCreateOrEdit
        {
            None,
            StatusCreate,
            StatusEdit
        }

        private EnumCreateOrEdit _createOrEdit;
        public EnumCreateOrEdit CreateOrEdit
        {
            get { return _createOrEdit; }
            set
            {
                _createOrEdit = value;
                if (value == EnumCreateOrEdit.StatusEdit)
                {
                    EditBaseVisible = Visibility.Visible;
                    CreateBaseVisible = Visibility.Collapsed;
                }
                else if (value == EnumCreateOrEdit.StatusCreate)
                {
                    CreateBaseVisible = Visibility.Visible;
                    EditBaseVisible = Visibility.Collapsed;
                }
            }
        }

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

        private Visibility _createBaseVisible;
        public Visibility CreateBaseVisible
        {
            get => _createBaseVisible;
            set => SetProperty(ref _createBaseVisible, value);
        }

        private Visibility _editBaseVisible;
        public Visibility EditBaseVisible
        {
            get => _editBaseVisible;
            set => SetProperty(ref _editBaseVisible, value);
        }

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

        public DelegateCommand EnterTemplateListCommand { get; set; }

        public DelegateCommand SaveToTemplateListCommand { get; set; }

        public DelegateCommand SaveToCurrentTemplateListCommand { get; set; }

        public string Unicode = null;

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

            SaveToCurrentTemplateListCommand = new DelegateCommand(SaveToCurrentTemplateList);

        }

        public void SaveToTemplateList()
        {
            if (CurrentCreateModName == "WatermarkCreateTextContent")
            {
                this.eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Publish(new EnumTextOrFileUnicode
                {
                    Unicode = Unicode,
                    Status = EnumTextOrFile.StatusText
                });
            }
            if (CurrentCreateModName == "WatermarkCreateFileContent")
            {
                this.eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Publish(new EnumTextOrFileUnicode
                {
                    Unicode = Unicode,
                    Status = EnumTextOrFile.StatusFile
                });
            }
        }

        public void SaveToCurrentTemplateList()
        {
            if (CurrentCreateModName == "WatermarkCreateTextContent")
            {
                eventAggregator.GetEvent<ConfirmEditWatermarkTemplateItemEvent>().Publish(new EnumTextOrFileUnicode
                {
                    Unicode = Unicode,
                    Status = EnumTextOrFile.StatusText
                });
            }
            if (CurrentCreateModName == "WatermarkCreateFileContent")
            {
                eventAggregator.GetEvent<ConfirmEditWatermarkTemplateItemEvent>().Publish(new EnumTextOrFileUnicode
                {
                    Unicode = Unicode,
                    Status = EnumTextOrFile.StatusFile
                });
            }
        }

        public void EnterTemplateList()
        {
            this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = 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 EditSelectedTemplateItem(string currentCreateName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("WatermarkItem", WatermarkItem);
            watermarkCreateRegion.RequestNavigate(WatermarkCreateRegionName, currentCreateName, param);
            watermarkCreateVisible = System.Windows.Visibility.Visible;
        }

        public void ChangeCreateMod(object e)
        {
            var args = e as Button;
            if (args != null)
            {
                if (CreateOrEdit == EnumCreateOrEdit.StatusCreate)
                {
                    CurrentCreateModName = args.Name;
                    EnterSelectedCreateMod(CurrentCreateModName);
                    eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(new stringUnicode
                    {
                        Unicode = Unicode,
                        Status = CurrentCreateModName
                    });
                }
                else if (CreateOrEdit == EnumCreateOrEdit.StatusEdit)
                {
                    CurrentCreateModName = args.Name;
                    EditSelectedTemplateItem(CurrentCreateModName);
                    eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(new stringUnicode
                    {
                        Unicode = Unicode,
                        Status = CurrentCreateModName
                    });
                }
            }
        }

        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";
            }
            if (navigationContext.Parameters.TryGetValue<WatermarkItem>("WatermarkItem", out WatermarkItem))
            {
                EditSelectedTemplateItem(CurrentCreateModName);
                CreateOrEdit = EnumCreateOrEdit.StatusEdit;
            }
            else
            {
                EnterSelectedCreateMod(CurrentCreateModName);
                CreateOrEdit = EnumCreateOrEdit.StatusCreate;
            }
            eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(new stringUnicode
            {
                Unicode = Unicode,
                Status = CurrentCreateModName
            });
        }
    }
}
