using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using CyberX8_RT.Devices.AXIS;
using CyberX8_RT.Devices.TransPorter;
using CyberX8_RT.Modules.Loader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Modules.Transporter
{
   
    public class TransporterElevatorUpRoutine : RoutineBase, IRoutine
    {
        private enum ParkStep
        {
            CheckStatus,
            ElevatorUp,
            ElevatorUpWait,
            End
        }
        #region 内部变量
        private JetAxisBase _elevatorAxis;
        private JetAxisBase _gantryAxis;
        private TransporterCommon _transporterCommon;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public TransporterElevatorUpRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(ParkStep.CheckStatus, CheckPreCondition, NullFun,_delay_1ms)
                //1.1 Elevator
                .Run(ParkStep.ElevatorUp, ElevatorPosition, _delay_1ms)
                .WaitWithStopCondition(ParkStep.ElevatorUpWait, CheckVerticalPositionStatus, CheckVerticalPositionRunStop)
                .End(ParkStep.End,NullFun,100);
            return Runner.Status;
        }
        /// 
        /// 检验前置条件
        /// 
        /// 
        private bool CheckPreCondition()
        {
            if (!_elevatorAxis.IsSwitchOn)
            {
                LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{Module} elevator axis is not switch on ");
                return false;
            }
            if (!_elevatorAxis.IsHomed)
            {
                LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{Module} elevator axis is not homed ");
                return false;
            }
            return true;
        }
        /// 
        /// Elevator Position
        /// 
        /// 
        private bool ElevatorPosition()
        {
            return _elevatorAxis.PositionStation("UP");
        }
        /// 
        /// 检验Vertical移动状态
        /// 
        /// 
        private bool CheckVerticalPositionStatus()
        {
            return _elevatorAxis.Status == RState.End;
        }
        /// 
        /// 检验Vertical是否还在运动
        /// 
        /// 
        private bool CheckVerticalPositionRunStop()
        {
            return _elevatorAxis.Status == RState.Failed;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _elevatorAxis = DEVICE.GetDevice($"{Module}.Elevator");
            _gantryAxis = DEVICE.GetDevice($"{Module}.Gantry");
            _transporterCommon = DEVICE.GetDevice($"{Module}.Common");
            if (!CheckStartPreConfition())
            {
                return RState.Failed;
            }
            return Runner.Start(Module, "ElevatorUp");
        }
        /// 
        /// 启动校验条件
        /// 
        /// 
        private bool CheckStartPreConfition()
        {
            //Ready to lock或WH Present Sensor触发
            if(_transporterCommon.TransporterData.ReadyToLock1||_transporterCommon.TransporterData.ReadyToLock2||_transporterCommon.TransporterData.WhPresent1||
                _transporterCommon.TransporterData.WhPresent2)
            {
                //lock clamp sensor触发
                if(_transporterCommon.TransporterData.Locked1||_transporterCommon.TransporterData.Locked2)
                {
                    //gantry 位置不明确
                    if(_gantryAxis.CheckPositionIsEmpty(_gantryAxis.MotionData.MotorPosition))
                    {
                        LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, "gantry station is empty,can not elevator up");
                        return false;
                    }
                }
            }
            return true;
        }
    }
}