using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.ToolLayout;
using MECF.Framework.Common.TwinCat;
using CyberX8_Core;
using CyberX8_RT.Devices.Metal;
using CyberX8_RT.Devices.Reservoir;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CyberX8_RT.Devices.Safety;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Modules.Reservoir
{
    public class ANPumpOnRoutine : RoutineBase, IRoutine
    {
        private enum ANPumpStep
        {
            PumpSpeed,
            Delay,
            ANByPass,
            CellsFillValve,
            FlowDelay,
            CheckFlow,
            CheckByPass,
            End
        }
        #region 常量
        private const string AN_A_PINENABLE = "ANAPinEnable";
        private const string AN_B_PINENABLE = "ANBPinEnable";
        private const string AN_PUMP = "ANPump";
        private const string AN_BY_PASS = "ANByPass";
        #endregion
        #region 内部变量
        /// 
        /// 默认泵速
        /// 
        private double _anPumpSpeed = 5000;
        /// 
        /// flow fault hold off时长
        /// 
        private int _flowFaultHoldOffTime = 10000;
        /// 
        /// 阳极最小流量 
        /// 
        private double _anMinFlow = 0.2;
        /// 
        /// 最小Bypass数值
        /// 
        private int _anBypassMinCellCount;
        /// 
        /// AN流量总和
        /// 
        private double _anTotalFlow = 0;
        /// 
        /// Reservoir设备
        /// 
        private CompactMembranReservoirDevice _device;
        /// 
        /// 阳极打开阀Cell总数
        /// 
        private int _anOpenValveCount;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public ANPumpOnRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(ANPumpStep.PumpSpeed, () => { return ANPumpSpeed(_anPumpSpeed); }, _delay_1ms)
                .Run(ANPumpStep.ANByPass,ANByPassOn,_delay_1ms)
                .Run(ANPumpStep.CellsFillValve, OpenAllCellsFillValve,_delay_1ms)
                .Delay(ANPumpStep.Delay, _flowFaultHoldOffTime)
                .Run(ANPumpStep.CheckFlow,CheckAllFlow,_delay_1ms)
                .RunIf(ANPumpStep.CheckByPass, CheckTotalFlowBypass(),ANByPassOff,NullFun,_delay_1ms)
                .End(ANPumpStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// Pump Speed
        /// 
        /// 
        /// 
        private bool ANPumpSpeed(double speed)
        {
            SafetyDevice safetyDevice = DEVICE.GetDevice("Safety");
            if (safetyDevice != null && !safetyDevice.SafetyData.PumpEdm)
            {
                LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, $"Safety PumpEdm is Activate");
                return false;
            }
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{AN_PUMP}");
            return IOModuleManager.Instance.WriteIoValue(ioName, speed);
        }
        /// 
        /// Pump Bypass
        /// 
        /// 
        /// 
        private bool ANByPassOn()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{AN_BY_PASS}");
            return IOModuleManager.Instance.WriteIoValue(ioName, true);
        }
        /// 
        /// Pump Bypass
        /// 
        /// 
        /// 
        private bool ANByPassOff()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{AN_BY_PASS}");
            return IOModuleManager.Instance.WriteIoValue(ioName, false);
        }
        /// 
        /// 检验所有流量
        /// 
        /// 
        private bool OpenAllCellsFillValve()
        {
            bool result = false;
            ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
            if (reservoirItem != null)
            {
                List metalItems = reservoirItem.MetalCells;
                if (metalItems != null && metalItems.Count > 0)
                {
                    foreach (MetalItem metalItem in metalItems)
                    {
                        if (metalItem.Installed)
                        {
                            CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
                            if (metalDevice != null && metalDevice.IsAuto)
                            {
                                _anOpenValveCount++;
                                result = metalDevice.AnSideAFillOn("", null);
                                if(!result)
                                {
                                    return false;
                                }
                                result = metalDevice.AnSideBFillOn("", null);
                                if (!result)
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
        /// 
        /// 关闭所有Cell的Flow Valve
        /// 
        /// 
        private bool CloseAllCellsFlowValve()
        {
            ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
            if (reservoirItem != null)
            {
                List metalItems = reservoirItem.MetalCells;
                if (metalItems != null && metalItems.Count > 0)
                {
                    foreach (MetalItem metalItem in metalItems)
                    {
                        if (metalItem.Installed)
                        {
                            CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
                            if (metalDevice != null && metalDevice.IsAuto)
                            {
                                metalDevice.AnSideAFillOff("", null);
                                metalDevice.AnSideBFillOff("", null);
                            }
                        }
                    }
                }
            }
            return true;
        }
        /// 
        /// 检验所有流量
        /// 
        /// 
        private bool CheckAllFlow()
        {
            if (_anOpenValveCount == 0)
            {
                return true;
            }
            _anTotalFlow = 0.0;
            ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
            if (reservoirItem != null)
            {
                List metalItems = reservoirItem.MetalCells;
                if (metalItems != null && metalItems.Count > 0)
                {
                    foreach (MetalItem metalItem in metalItems)
                    {
                        if (metalItem.Installed)
                        {
                            CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
                            if (metalDevice != null&&metalDevice.IsAuto)
                            {
                                _anTotalFlow += metalDevice.ANACellFlow.CounterValue;
                                _anTotalFlow += metalDevice.ANBCellFlow.CounterValue;
                            }
                        }
                    }
                }
            }
            if(_anTotalFlow<=_anMinFlow)
            {
                CloseAllCellsFlowValve();
                ANPumpSpeed(0);
                LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, $"total flow {_anTotalFlow} is not over {_anMinFlow}");
                return false;
            }
            else
            {
                LOG.WriteLog(eEvent.INFO_RESERVOIR, Module, $"total flow {_anTotalFlow} is over {_anMinFlow}");
                return true;
            }
        }
        /// 
        /// 检验总流量Bypass情况
        /// 
        /// 
        private bool CheckTotalFlowBypass()
        {
            return _anTotalFlow > _anBypassMinCellCount;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _device = DEVICE.GetDevice(Module);
            _anPumpSpeed = SC.GetValue("Reservoir.ANDefaultPumpSpeed");
            _flowFaultHoldOffTime = SC.GetValue($"Reservoir.{Module}.FlowFaultHoldOffTime");
            _anBypassMinCellCount = SC.GetValue($"Reservoir.{Module}.ANBypassMinCellCount");
            _anTotalFlow = 0;
            return Runner.Start(Module, "AN Pump On");
        }
    }
}