using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using StartServerWPF.Modules.Main.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StartServerWPF.Modules.Main.ViewModels
{
    public class TreeMenuViewModel : BindableBase
    {
        private ObservableCollection<MenuItemModel> _menus;
        public ObservableCollection<MenuItemModel> Menus { get => _menus; set => SetProperty(ref _menus, value); }

    //    private List<MenuInfoDTO> origMenus = null;

        private IRegionManager _regionManager = null;
        private readonly IDialogService _dialogService;

        public TreeMenuViewModel(IContainerProvider containerProvider, IRegionManager regionManager, IEventAggregator ea, IDialogService dialogService)
        {
            _regionManager = regionManager;
            this._dialogService = dialogService;
            //var global = containerProvider.Resolve<GlobalEntity>();// 注册一个单例
            //   ea.GetEvent<LoginEvent>().Subscribe(OnLogin, ThreadOption.UIThread);
            Menus=new ObservableCollection<MenuItemModel>();
            FillMenus(Menus);
        }

        #region 事件

        public DelegateCommand AboutCommand => new(()=>
        {
            DialogParameters param=new DialogParameters();
            _dialogService.ShowDialog(
             "AboutDialogView",
             param,
             new Action<IDialogResult>(result =>
             {
                 if (result != null && result.Result == ButtonResult.OK)
                 {
                     MessageBox.Show("数据已保存", "提示");
                 }
             }));
        });
        #endregion  
 
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="menus"></param>
        private void FillMenus(ObservableCollection<MenuItemModel> menus)
        {
            for (int i = 0; i < menuNames.Count; i++)
            {
                MenuItemModel menuItem = new MenuItemModel(_regionManager)
                {
                    MenuHeader = menuNames[i],
                    MenuIcon = "\ue63a",
                    TargetView = viewName[i],
                    Children = new ObservableCollection<MenuItemModel>(),
                    IsExpanded = true,
                };
                menus.Add(menuItem);
            }
        }
        List<string> menuNames = new List<string>() {"首页","实时波形","波形回放", "数据处理", "设置","日志", };
        List<string> viewName = new List<string>() { "MainView", "ChartPlotRealDataView", "ChartPlotView", "DataProcessView", "SetParamView", "LogManagementView"};
    }
}
