using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.Entity;
using Txgy.EWS.Client.MainModule.Models;

namespace Txgy.EWS.Client.MainModule.ViewModels
{
    public class TreeMenuViewModel : BindableBase
    {
        private const string SystemSettingsTargetView = "SystemSettingsView";
        private const string AdvancedSystemSettingsTargetView = "SystemSettingsAdvanced";
        private const string SystemSettingsMenuHeader = "参数设置";

        public List<MenuItemModel> Menus { get; set; } = new List<MenuItemModel>();

        private List<MenuEntity> origMenus = null;

        private IRegionManager _regionManager = null;

        public TreeMenuViewModel(IContainerProvider containerProvider, IRegionManager regionManager)
        {
            _regionManager = regionManager;
            //var global = containerProvider.Resolve<GlobalEntity>();// 注册一个单例
            if (GlobalData.CurrentUserInfo != null)
            {
                origMenus = GlobalData.CurrentUserInfo.Menus;
                this.FillMenus(Menus, 0);
            }

            AddLocalSystemSettingsMenu();
        }

        ///递归
        ///
        private void FillMenus(List<MenuItemModel> menus, int parentId)
        {
            var sub = origMenus.Where(m => m.ParentId == parentId).OrderBy(o => o.Index);

            if (sub.Count() > 0)
            {
                foreach (var item in sub)
                {
                    if (string.Equals(item.TargetView, AdvancedSystemSettingsTargetView, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    MenuItemModel mm = new MenuItemModel(_regionManager)
                    {
                        MenuHeader = string.Equals(item.TargetView, SystemSettingsTargetView, StringComparison.OrdinalIgnoreCase)
                            ? SystemSettingsMenuHeader
                            : item.MenuHeader,
                        MenuIcon = item.MenuIcon,
                        TargetView = item.TargetView
                    };
                    menus.Add(mm);

                    FillMenus(mm.Children = new List<MenuItemModel>(), item.MenuId);
                }
            }
        }

        private void AddLocalSystemSettingsMenu()
        {
            if (ContainsTargetView(Menus, SystemSettingsTargetView))
            {
                return;
            }

            Menus.Add(new MenuItemModel(_regionManager)
            {
                MenuHeader = SystemSettingsMenuHeader,
                MenuIcon = "\ue64c",
                TargetView = SystemSettingsTargetView,
                Children = new List<MenuItemModel>()
            });
        }

        private bool ContainsTargetView(IEnumerable<MenuItemModel> menus, string targetView)
        {
            if (menus == null)
            {
                return false;
            }

            foreach (var menu in menus)
            {
                if (string.Equals(menu.TargetView, targetView, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }

                if (ContainsTargetView(menu.Children, targetView))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
