using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
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.TwinCat;
using CyberX8_Core;
using CyberX8_RT.Devices.Loader;
using CyberX8_RT.Devices.TransPorter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Devices.AXIS.CANOpen
{
   
    public class TransporterRetractRoutine : RoutineBase, IRoutine
    {
        private enum RetractStep
        {
            CheckInUpPosition,
            WriteRetract,
            End
        }
        #region 常量 
        private const string IMMOBILIZE_ACTIVE = "ImmobilizeActive";
        private const string IMMOBILIZE_ACTIVE2 = "ImmobilizeActive2";
        #endregion
        #region 内部变量
        private int _timeout = 5000;
        private TransporterCommon _transporterCommon;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public TransporterRetractRoutine(string module) : base(module)
        {
            if (SC.ContainsItem($"Transporter.{module}.RetractCheckTime"))
            {
                _timeout = SC.GetValue($"Transporter.{module}.RetractCheckTime") * 1000;
            }
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(RetractStep.CheckInUpPosition, CheckPosition, _delay_1ms)
                .Run(RetractStep.WriteRetract, WriteRetract, CheckRetract,_timeout)
                .End(RetractStep.End,NullFun,100);
            return Runner.Status;
        }
        /// 
        /// 检查Elevate当前是否在UP Place
        /// 
        /// 
        private bool CheckPosition()
        {
            JetAxisBase elevatorAxis = DEVICE.GetDevice($"{Module}.Elevator");
            if (elevatorAxis != null && !elevatorAxis.CurrentStation.Contains("UP"))
            {
                return false;
            }
            return true;
        }
        /// 
        /// 写入Retract数值
        /// 
        /// 
        private bool WriteRetract()
        {
            bool active = WriteIo(IMMOBILIZE_ACTIVE, false);
            if (active)
            {
                return WriteIo(IMMOBILIZE_ACTIVE2, false);
            }
            return false;
        }
        /// 
        /// 写入IO
        /// 
        /// 
        /// 
        /// 
        private bool WriteIo(string name,bool value)
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{name}");
            if (!string.IsNullOrEmpty(ioName))
            {
                return IOModuleManager.Instance.WriteIoValue(ioName, value);
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 检验Retract状态
        /// 
        /// 
        private bool CheckRetract()
        {
            return _transporterCommon.TransporterData.ImmobilizeRetracted1 && _transporterCommon.TransporterData.ImmobilizeRetracted2;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _transporterCommon = DEVICE.GetDevice($"{Module}.Common");
            return Runner.Start(Module, "Retract");
        }
    }
}