using ComPDFKitViewer.PdfViewer;
using DryIoc;
using PDF_Master.EventAggregators;
using PDF_Master.Model;
using PDF_Master.Model.EditTools.Background;
using PDF_Master.Views.EditTools.Background;
using PDFSettings;
using Prism.Commands;
using Prism.Common;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace PDF_Master.ViewModels.EditTools.Background
{
    public class BackgroundCreateBaseContentViewModel : BindableBase, INavigationAware
    {
        IEventAggregator eventAggregator;
        IRegionManager backgroundCreateRegion;

        private CPDFViewer PDFViewer;
        public BackgroundItem BackgroundItem;

        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;
                    SelectContentVisibility = Visibility.Collapsed;
                    CreateBaseVisible = Visibility.Collapsed;
                }
                else if (value == EnumCreateOrEdit.StatusCreate)
                {
                    CreateBaseVisible = Visibility.Visible;
                    SelectContentVisibility= Visibility.Visible;
                    EditBaseVisible = Visibility.Collapsed;
                }
            }
        }

        private string backgroundCreateRegionName;

        public string BackgroundCreateRegionName
        {
            get => backgroundCreateRegionName;
            set => SetProperty(ref backgroundCreateRegionName, value);
        }

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


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

        private bool isFirstEnter = true;
        public bool IsFirstEnter
        {
            get => isFirstEnter;
            set => isFirstEnter = 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);
        }

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

        public EnumColorOrFile CurrentTemplateListMod;
        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 string Unicode = null;

        public BackgroundCreateBaseContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.backgroundCreateRegion = regionManager;
            Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
            BackgroundCreateRegionName = 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 == "BackgroundCreateColorContent")
            {
                this.eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Publish(new EnumColorOrFileUnicode { Unicode = Unicode, Status = EnumColorOrFile.StatusColor });
            }
            if (CurrentCreateModName == "BackgroundCreateFileContent")
            {
                this.eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Publish(new EnumColorOrFileUnicode
                {
                    Unicode = Unicode,
                    Status = EnumColorOrFile.StatusFile
                });
            }
        }

        public void SaveToCurrentTemplateList()
        {
            if (CurrentCreateModName == "BackgroundCreateColorContent")
            {
                eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Publish(new EnumColorOrFileUnicode {
                    Unicode = Unicode, Status = EnumColorOrFile.StatusColor});
            }
            if (CurrentCreateModName == "BackgroundCreateFileContent")
            {
                eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Publish(new EnumColorOrFileUnicode {
                    Unicode = Unicode, Status = EnumColorOrFile.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);
            backgroundCreateRegion.RequestNavigate(BackgroundCreateRegionName, currentCreateName, param);
            backgroundCreateVisible = System.Windows.Visibility.Visible;
        }



        public void EditSelectedTemplateItem(string currentCreateName)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, PDFViewer);
            param.Add("BackgroundItem", BackgroundItem);
            backgroundCreateRegion.RequestNavigate(BackgroundCreateRegionName, currentCreateName, param);
            backgroundCreateVisible = 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<EnumColorOrFile>("CurrentTemplateListModName", out CurrentTemplateListMod);
            if (CurrentTemplateListMod == EnumColorOrFile.StatusColor)
            {
                CurrentCreateModName = "BackgroundCreateColorContent";
            }
            else
            {
                CurrentCreateModName = "BackgroundCreateFileContent";
            }
            if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out BackgroundItem))
            {
                EditSelectedTemplateItem(CurrentCreateModName);
                CreateOrEdit = EnumCreateOrEdit.StatusEdit;
            }
            else
            {
                EnterSelectedCreateMod(CurrentCreateModName);
                CreateOrEdit = EnumCreateOrEdit.StatusCreate;
            }
            eventAggregator.GetEvent<SetCurrentCreateModEvent>().Publish(new stringUnicode
            {
                Unicode = Unicode,
                Status = CurrentCreateModName
            });
        }
    }
}