using Aitex.Core.RT.SCCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MECF.Framework.Common.Account.Extends
{
	public class MenuLoader : XmlLoader
	{

		#region Variables

		private List<AppMenu> _menuList = new();

		#endregion
		
		#region Constructors

		public MenuLoader(string fileName)
			: base(fileName)
		{
		}
		
		#endregion

		#region Methods

		/// <summary>
		/// 系统中配置的菜单项目。
		/// </summary>
		public List<AppMenu> MenuList => _menuList;

        private static Dictionary<string, string> _viewModelItems;

        private static string defaultViewModel;

        /// <inheritdoc />
        protected override void AnalyzeXml()
		{
			if (XmlDoc != null)
			{
                _viewModelItems = TranslateScViewModelItems(XmlDoc.Root);
                _menuList = TranslateMenus(XmlDoc.Root);
            }
		}

		private static List<AppMenu> TranslateMenus(XContainer pElement)
		{
			var list = new List<AppMenu>();
			var enumerable = from r in pElement.Elements("menuItem")
				select (r);

            

            foreach (var item in enumerable)
			{
				var menuId = "";
				var menuViewModel = "";
                var menuScViewmodel = "";
                var resKey = "";
				var desc = "";
				if (!item.HasElements)
				{
					menuId = item.Attribute("id").Value;
                    menuScViewmodel = item.Attribute("scViewmodel")?.Value ?? null;
					if (String.IsNullOrEmpty(menuScViewmodel))
						menuViewModel = item.Attribute("viewmodel").Value;
					else
					{
						var view = SC.GetStringValue(menuScViewmodel);
                        if (_viewModelItems.TryGetValue(view, out string value))
						    menuViewModel = value;
						else//出现异常时，使用默认气路图打开,在RT界面配置中配置默认显示界面
                            menuViewModel = defaultViewModel;
					}


                    resKey = item.Attribute("resKey").Value;
					if (item.Attribute("Description") != null)
						desc = item.Attribute("Description").Value;
					
					var appMenu = new AppMenu(menuId, menuViewModel, resKey, null, desc);
					if (item.Attribute("System") != null)
						appMenu.System = item.Attribute("System").Value;
					if (item.Attribute("AlarmModule") != null)
						appMenu.AlarmModule = item.Attribute("AlarmModule").Value;
					list.Add(appMenu);
					continue;
				}
				menuId = item.Attribute("id").Value;
				resKey = item.Attribute("resKey").Value;
				var subMenus = new List<AppMenu>();
				subMenus = TranslateMenus(item);
				var appMenu2 = new AppMenu(menuId, menuViewModel, resKey, subMenus, desc);
				if (item.Attribute("System") != null)
					appMenu2.System = item.Attribute("System").Value;
				if (item.Attribute("AlarmModule") != null)
					appMenu2.AlarmModule = item.Attribute("AlarmModule").Value;
				list.Add(appMenu2);
			}
			return list;
		}

        private static Dictionary<string, string> TranslateScViewModelItems(XContainer items)
        {
            defaultViewModel = items.Element("ViewModelItems").Attribute("defaultViewModel").Value;
            var enumerable = items.Element("ViewModelItems").Elements("Item");

			Dictionary<string, string> viewModelItems = new Dictionary<string, string>();

			foreach (var item in enumerable)
			{
				var key = item.Attribute("key").Value;
				var value = item.Attribute("viewModel").Value;

				if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value))
					viewModelItems.Add(key, value);
			}

			return viewModelItems;
        }

        #endregion
    }
}
