using Aitex.Core.RT.DataCenter;
using Aitex.Core.Util;
using MECF.Framework.Common.Device.LinMot;
using MECF.Framework.Common.Device.PowerSupplier;
using MECF.Framework.Common.Equipment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MECF.Framework.Common.ToolLayout
{
    public class MetalItemManager : Singleton<MetalItemManager>
    {
        #region 常量
        /// <summary>
        /// 前缀
        /// </summary>
        private const string PREFIX = "Metal";
        #endregion
        #region 内部变量
        /// <summary>
        /// 字典
        /// </summary>
        private Dictionary<string,MetalItem> _metalItemDictionary=new Dictionary<string, MetalItem>();
        #endregion

        #region 属性
        /// <summary>
        /// 已经安装模块
        /// </summary>
        public List<string> InstalledModules { get; private set; } = new List<string>();
        #endregion

        /// <summary>
        /// 初始化metal
        /// </summary>
        /// <param name="metalItemElement"></param>
        public MetalItem InitializeMetalItem(XmlElement metalItemElement)
        {
            MetalItem metalItem = new MetalItem();
            metalItem.CellType = "Cell";
            LayoutCellItemManager.Instance.InitializeLayoutCellItem(metalItem, metalItemElement);
            metalItem.MetalID = int.Parse(metalItemElement.SelectSingleNode("MetalID").InnerText);
            metalItem.PermittedWaferSizeInMM = metalItemElement.SelectSingleNode("PermittedWaferSizeInMM").InnerText;
            metalItem.PlatingPowerSupplyAID = metalItemElement.SelectSingleNode("PlatingPowerSupplyAID").InnerText;
            metalItem.PlatingPowerSupplyBID = metalItemElement.SelectSingleNode("PlatingPowerSupplyBID").InnerText;
            metalItem.LinmotID = metalItemElement.SelectSingleNode("LinmotID").InnerText;
            metalItem.DepPump = bool.Parse(metalItemElement.SelectSingleNode("DepPump").InnerText);
            string key = $"{PREFIX}{metalItem.MetalID}";
            metalItem.ModuleName = key;
            metalItem.ModuleType = ModuleType.Metal.ToString();
            CellItemManager.Instance.InitialLayoutCellItem(metalItem);
            if(metalItem.Installed && !InstalledModules.Contains(key))
            {
                InstalledModules.Add(key);
            }

            LinMotDevice linMotDevice= LinMotDeviceConfigManager.Instance.GetLinMotDevice(metalItem.LinmotID);
            if(linMotDevice!=null)
            {
                linMotDevice.ParentName = metalItem.ModuleName;
            }

            PowerSupplierDevice powerSupplierADevice= PowerSupplierDeviceConfigManager.Instance.GetPowerSupplierDeviceByName(metalItem.PlatingPowerSupplyAID);
            if(powerSupplierADevice!=null)
            {
                powerSupplierADevice.ParentName = metalItem.ModuleName;
            }

            PowerSupplierDevice powerSupplierBDevice= PowerSupplierDeviceConfigManager.Instance.GetPowerSupplierDeviceByName(metalItem.PlatingPowerSupplyBID);
            if(powerSupplierBDevice!=null)
            {
                powerSupplierBDevice.ParentName = metalItem.ModuleName;
            }
            DATA.Subscribe($"{metalItem.ModuleName}.SideA.PowerSupplier", () => { return metalItem.PlatingPowerSupplyAID; }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
            DATA.Subscribe($"{metalItem.ModuleName}.SideB.PowerSupplier", () => { return metalItem.PlatingPowerSupplyBID; }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
            return metalItem;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="metalItem"></param>
        public void AddMetalItem(MetalItem metalItem)
        {
            _metalItemDictionary[$"{PREFIX}{metalItem.MetalID}"] = metalItem;
        }
        /// <summary>
        /// 获取MetalItem对象
        /// </summary>
        /// <param name="moduleName"></param>
        /// <returns></returns>
        public MetalItem GetMetalItem(string moduleName)
        {
            return _metalItemDictionary.ContainsKey(moduleName) ? _metalItemDictionary[moduleName] : null;
        }
    }
}