using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Beckhoff.AxisProvider;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.AXIS;
using PunkHPX8_RT.Devices.PlatingCell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Modules.PlatingCell
{
    public class PlatingCellInterRinseRoutine : RoutineBase, IRoutine
    {
        private enum RunRecipeStep
        {
            OpenRinseValve,
            StartRotation,
            CheckRotationStop,
            CloseRinseValve,
            End
        }
        #region 常量
        /// 
        /// ROTATION电机转速比例
        /// 
        private const int SPEED_RATIO = 1;
        #endregion
        /// 
        /// recipe
        /// 
        private DepRecipe _recipe;
        /// 
        /// Rotation axis
        /// 
        private JetAxisBase _rotationAxis;
        /// 
        /// Platingcell device
        /// 
        private PlatingCellDevice _device;
        /// 
        ///rotation Provider对象
        /// 
        private BeckhoffProviderAxis _rotationProviderAxis;
        /// 
        /// 构造函数
        /// 
        /// 
        public PlatingCellInterRinseRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(RunRecipeStep.OpenRinseValve, _device.RinseEnableAction, _delay_1ms)
                .Run(RunRecipeStep.StartRotation, StartRotation, _delay_1ms)
                .WaitWithStopCondition(RunRecipeStep.CheckRotationStop, CheckRotationEndStatus, CheckRotationStopStatus)
                .Run(RunRecipeStep.CloseRinseValve, _device.RinseDisableAction, _delay_1ms)
                .End(RunRecipeStep.End, NullFun);
            return Runner.Status;
        }
        /// 
        /// rotation开始旋转
        /// 
        /// 
        /// 
        private bool StartRotation()
        {
            //比例
            double _scale = _rotationProviderAxis.ScaleFactor;
            //rinse 目标位置 
            double rinsePosition = _recipe.IntervalRinseTime * BeckhoffVelocityUtil.ConvertVelocityToDegPerSecondByRPM(_recipe.IntervalRinseSpeed);
            int targetPosition = (int)Math.Round((rinsePosition + _recipe.IntervalRinseZoffset) * _scale, 0);
            bool result = _rotationAxis.ProfilePosition(targetPosition, _recipe.IntervalRinseSpeed * SPEED_RATIO, 0, 0);
            if (!result)
            {
                NotifyError(eEvent.ERR_PLATINGCELL, "Start Rotation is failed", 0);
                return false;
            }
            return true;
        }
        /// 
        /// 检验Rotation是否停止
        /// 
        /// 
        private bool CheckRotationEndStatus()
        {
            if (!_rotationAxis.IsRun && _rotationAxis.Status == RState.End)
            {
                return true;
            }
            return false;
        }
        /// 
        /// 检验Rotation停止状态
        /// 
        /// 
        private bool CheckRotationStopStatus()
        {
            if (_rotationAxis.Status == RState.Failed || _rotationAxis.Status == RState.Timeout)
            {
                NotifyError(eEvent.ERR_PLATINGCELL, $"{Module}.Rotation is failed", 0);
                return true;
            }
            return false;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _recipe = (DepRecipe)objs[0];
            _rotationAxis = (JetAxisBase)objs[1];
            _device = (PlatingCellDevice)objs[2];
            _rotationProviderAxis = (BeckhoffProviderAxis)objs[3];
            return Runner.Start(Module, "start intervale rinse");
        }
    }
}