using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Aitex.Core.Common.DeviceData;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.GasFlow
{
    [XmlType(typeName: "PMGsaFlow")]
    public class PMAllGsa
    {
        /// <summary>
        /// PM腔体中气体
        /// </summary>
        [XmlElement("GasFlowUnit")]
        public List<GasFlowUnit> GasFlowUnitList { get; set; }

        public string ModuleName { get; set; }

        public List<GasFlowSum> GasFlowSumList { get; set; }

        private DateTime start;
        private DateTime end;

        public void Initialize(string moduleName)
        {
            IniGasUnit(moduleName);

            DATA.Subscribe($"{ModuleName}.PMGasFlowSumList", () => GasFlowSumList);
            var _thread = new PeriodicJob(1000, OnTimer, $"{moduleName}.ModuleGsaFlow", true);
        }

        private bool OnTimer()
        {
            foreach (GasFlowUnit Unit in GasFlowUnitList)
            {
                Unit.GetGasFlowFeedBack();
            }
            return true;
        }

        /// <summary>
        /// 初始化单个气体和界面气体统计对象
        /// </summary>
        /// <param name="moduleName"></param>
        private void IniGasUnit(string moduleName)
        {
            ModuleName = moduleName;

            GasFlowSumList = new();

            //单种气体初始化
            foreach (var unit in GasFlowUnitList)
            {
                unit.Initialize(moduleName);

                string gasName = unit.GasName.Split('_')[0];//气体格式定位为H2_Run  H2_Vent,所以根据下划线来拆分

                var gfs = new GasFlowSum();
                gfs.Name = gasName;
                gfs.Display = gasName;
                if (string.IsNullOrEmpty(unit.scBasePath) == false)
                {
                    gfs.Display = SC.GetStringValue(unit.scBasePath);
                }
                if (!GasFlowSumList.Exists(t => t.Name == gasName))
                {
                    GasFlowSumList.Add(gfs);
                }
            }
        }
    }
}