using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Model;
using Prism.Commands;
using System.Windows.Controls;
using DryIoc;
using Prism.Services.Dialogs;

namespace PDF_Office.ViewModels.BOTA
{
    public class BOTAContentViewModel : BindableBase, INavigationAware
    {
        private CPDFViewer pdfViewer { get; set; }

        private IRegionManager regions { get; set; }
        private IDialogService dialogs { get; set; }

        private ViewContentViewModel viewContentViewModel { get; set; }

        private Dictionary<string, string> viewNameByTabItem;

        private string botaContentRegionName;

        public string BOTAContentRegionName
        {
            get { return botaContentRegionName; }
            set
            {
                SetProperty(ref botaContentRegionName, value);
            }
        }

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

        public BOTAContentViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            regions = regionManager;
            dialogs = dialogService;
            BOTAContentRegionName = Guid.NewGuid().ToString();
            TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectionChangedEvent);
            viewNameByTabItem = new Dictionary<string, string>();
            InitDictionartViewNameByTabItem(ref viewNameByTabItem);
        }

        private void TabControlSelectionChangedEvent(object e)
        {
            var args = e as SelectionChangedEventArgs;
            if (args != null)
            {
                EnterSelectedBar((args.AddedItems[0] as TabItem).Name);
            }
        }

        /// <summary>
        /// 初始化名称-视图字典
        /// </summary>
        private void InitDictionartViewNameByTabItem(ref Dictionary<string, string> viewNameByTabItem)
        {
            //绑定tabitem名字和对应的View控件名称
            viewNameByTabItem.Add("TabItemThumbnail", "");
            viewNameByTabItem.Add("TabItemOutLine", "");
            viewNameByTabItem.Add("TabItemBookMark", "BookmarkContent");
            viewNameByTabItem.Add("TabItemAnnotation", "");
            viewNameByTabItem.Add("TabItemSearch", "");
            viewNameByTabItem.Add("TabItemForm", "");
            viewNameByTabItem.Add("TabItemSign", "");
        }

        /// <summary>
        /// 导航至目标TabItem
        /// </summary>
        /// <param name="currentBar"></param>
        private void EnterSelectedBar(string currentBar)
        {
            NavigationParameters param = new NavigationParameters();
            param.Add(ParameterNames.PDFViewer, pdfViewer);
            regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
        }

        #region Navigate

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
            if (contentViewModel != null)
            {
                viewContentViewModel = contentViewModel;
            }
            var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
            if (pdfview != null)
            {
                pdfViewer = pdfview;
            }
        }

        #endregion Navigate
    }
}