using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device.Devices;
using Aitex.Core.RT.Device;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Threading.Tasks;
using Aitex.Core.RT.Event;

namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.GasFlow
{
    /// <summary>
    /// 单行真值表对应的逻辑对象,主要执行逻辑就在这里
    /// </summary>
    [XmlType(typeName: "GasTrueTableRows")]
    public partial class GasTrueTableRows
    {

        [XmlArray("Inputs")]
        public List<ControlNameValue> XMLInputList { get; set; }

        [XmlArray("Outputs")]
        public List<ControlNameValue> XMLOutputList { get; set; }

        public string ModuleName { get; set; }

        public string GasName { get; set; }

        private Dictionary<IoValve, double> InputIoValveList = new Dictionary<IoValve, double>();

        private Dictionary<object, double> OutputMfcList = new Dictionary<object, double>();


        public void Initialize(string moduleName, string gasName)
        {
            string strErrorMeg = "";
            ModuleName = moduleName;
            GasName = gasName;
            try
            {

                foreach (var rowInputs in XMLInputList)
                {
                    strErrorMeg = $"Name = {rowInputs.Name}  Value = {rowInputs.Value}  ControlName = {rowInputs.ControlName}  ";
                    InputIoValveList.Add(DEVICE.GetDevice<IoValve>($"{ModuleName}.{rowInputs.ControlName}"), rowInputs.Value);
                }


                if (GasName.Contains("PN2"))//PN2是特殊计算不能在此初始化
                    return;

                foreach (var rowOutputs in XMLOutputList)
                {
                    strErrorMeg = $"Name = {rowOutputs.Name}  Value = {rowOutputs.Value}  ControlName = {rowOutputs.ControlName}  ";
                    if (rowOutputs.ControlName.Contains("Mfc"))
                        OutputMfcList.Add(DEVICE.GetDevice<IoMFC>($"{ModuleName}.{rowOutputs.ControlName}"), rowOutputs.Value);
                    else
                        OutputMfcList.Add(rowOutputs.ControlName, rowOutputs.Value);
                }
            }
            catch (Exception ex)
            {
                //输出打印过早，界面可能未加载，造成异常未输出到界面
                Task.Delay(5000).ContinueWith(x => EV.PostAlarmLog(ModuleName, $"Truth table initialization error , {GasName}, {strErrorMeg} ,  Restart the software after the fault is rectified"));
            }


        }


        private bool IsCanOut()
        {
            //有一个集合为空，不用循环
            if (InputIoValveList.Count == 0 || XMLOutputList.Count == 0)
                return false;

            //一行内单元格的输入输出配置
            foreach (var rowInput in InputIoValveList)
            {
                if (rowInput.Key.Status == true && rowInput.Value == 1)
                    continue;
                if (rowInput.Key.Status == false && rowInput.Value == 0)
                    continue;
                return false;
            }
            return true;
        }
        public double Accumulation()
        {
            double volue = 0;
            if (!IsCanOut())
                return 0;

            foreach (var rowOutput in OutputMfcList)
            {
                if (rowOutput.Key is IoMFC _mfc)
                    volue += _mfc.FeedBack * rowOutput.Value;
                else//混合气体流量
                    volue += Convert.ToDouble(DATA.Poll($"{ModuleName}.GasRealTimeFlow.{rowOutput.Key}_Run.FeedBack")) * rowOutput.Value;

            }

            return volue;
        }

        public double AccumulationPN2Run()
        {
            double volue = 0;
            if (!IsCanOut())
                return 0;

            foreach (var rowOutputs in XMLOutputList)
            {
                if (rowOutputs.ControlName.Contains("Mfc5"))
                {
                    //Output配置的单元格
                    var _mfc = DEVICE.GetDevice<IoMFC>($"{ModuleName}.{rowOutputs.ControlName}");
                    volue += _mfc.FeedBack * rowOutputs.Value;
                }
                else if (rowOutputs.ControlName.Contains("Mfc6"))
                {
                    //特殊气体流量
                    var _mfc3 = DEVICE.GetDevice<IoMFC>($"{ModuleName}.Mfc3");
                    var _mfc4 = DEVICE.GetDevice<IoMFC>($"{ModuleName}.Mfc4");
                    var _mfc6 = DEVICE.GetDevice<IoMFC>($"{ModuleName}.Mfc6");
                    var m3 = _mfc3.FeedBack;
                    var m4 = _mfc4.FeedBack;
                    var m6 = _mfc6.FeedBack;

                    volue += (m6 * m4 / (m3 + m4)) * rowOutputs.Value;
                }
            }
            return volue;
        }
    }
}
