using System;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.GasFlow
{
    /// <summary>
    /// 单个气体的真值表数据，所有配置行数据集合，气体需要的属性数据
    /// </summary>
    [XmlType(typeName: "GasFlowUnit")]
    public class GasFlowUnit
    {
        /// <summary>
        /// 单个真值表内所有的行数据
        /// </summary>
        [XmlElement("GasTrueTableRows")]
        public List<GasTrueTableRows> GasTrueTableRowList { get; set; }

        /// <summary>
        /// 气体名称
        /// </summary>
        [XmlAttribute()]
        public string GasName { get; set; }

        /// <summary>
        /// 气体名称显示SC配置
        /// </summary>
        [XmlAttribute()]
        public string scBasePath { get; set; }

        /// <summary>
        /// 气体流量系数名称
        /// </summary>
        [XmlAttribute()]
        public string GasFlowRatioName { get; set; }

        public string ModuleName { get; set; }

        /// <summary>
        /// 系统中配置的气体流量系数
        /// </summary>
        public double GasFlowRatioValue { get; set; }

        /// <summary>
        /// 气体流量计算值
        /// </summary>
        public double GasFlowFeedBack { get; set; } = 0;

        /// <summary>
        /// PN2的特殊计算,仅作为界面显示用，不存储到数据库
        /// </summary>
        public double PN2_VentSpecial { get; set; } = 0;


        private R_TRIG _trigError = new();

        public void Initialize(string moduleName)
        {
            ModuleName = moduleName;
            SC.RegisterValueChangedCallback($"PM.{ModuleName}.Efficiency.{GasFlowRatioName}", OnGasFlowRatioSCChanged);
            GasFlowRatioValue = SC.SafeGetValue<double>($"PM.{ModuleName}.Efficiency.{GasFlowRatioName}", 1d);

            if (GasName == "PN2_Vent")
                DATA.Subscribe($"{ModuleName}.GasRealTimeFlow.{GasName}.FeedBackSpecial", () => PN2_VentSpecial, SubscriptionAttribute.FLAG.IgnoreSaveDB);//仅作为显示用，不存储数据库

            DATA.Subscribe($"{ModuleName}.GasRealTimeFlow.{GasName}.FeedBack", () => GasFlowFeedBack);

            foreach (var row in GasTrueTableRowList)
                row.Initialize(moduleName, GasName);
        }

        private void OnGasFlowRatioSCChanged(object value)
        {
            if (value is double ratio)
                GasFlowRatioValue = ratio;
            else
                GasFlowRatioValue = 1d;
        }

        public double GetGasFlowFeedBack()
        {
            double _feedBack = 0;
            try
            {
                //循环检测气阀状态
                if (GasTrueTableRowList.Count > 0)
                {
                    for (int i = 0; i < GasTrueTableRowList.Count; i++)//遍历每一行配置的内容，统计气体输出
                    {

                        //PN2是特殊计算
                        if (GasName == "PN2_Run")
                            _feedBack += GasTrueTableRowList[i].AccumulationPN2Run();
                        else if (GasName == "PN2_Vent")
                        {
                            double cache = GasTrueTableRowList[i].AccumulationPN2Run();
                            if (i == 2)
                                PN2_VentSpecial = cache;//洪膺只要输出项的第三条数据作为显示
                            else
                                _feedBack += cache;//储存前面两行
                        }
                        else
                            _feedBack += GasTrueTableRowList[i].Accumulation();
                    }
                    if (_feedBack < 0)
                        _feedBack = 0;
                    GasFlowFeedBack = _feedBack * GasFlowRatioValue;//计算得到的结果*系数
                }

            }
            catch (Exception ex)
            {
                _trigError.CLK = true;
                if (_trigError.Q)
                    EV.PostWarningLog(ModuleName, $"{GasName} , The truth table calculation is error : {ex}");
            }
            _trigError.RST = true;
            return GasFlowFeedBack;
        }
    }
}
