using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Device.LinMot;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aitex.Core.RT.Log;

namespace CyberX8_RT.Devices.LinMot
{
   
    public class LinMotResetRoutine : RoutineBase, IRoutine
    {
        private enum LinMotResetStep
        {
            ClearError,
            ClearErrorDelay,
            SwitchOff,
            SwitchOn,
            Home,
            HomeDelay,
            HomeWait,
            GoToInitialPosition,
            ReDelay,
            ReWait,
            LastSwitchOn,
            LastSwitchOff,
            End
        }
        #region 憅郹揘量
        private LinMotAxis _axis;
        private bool _lastNeedSwitchOff = false;
        #endregion
        public LinMotResetRoutine(string module,LinMotAxis axis) : base(module)
        {
            _axis = axis;
        }

        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }

        public RState Monitor()
        {
            Runner .Run(LinMotResetStep.ClearError, () => { return _axis.SendOperation(LinMotOperation.ClearError); }, NullFun, 100)
                .Delay(LinMotResetStep.ClearErrorDelay, 100)
                .Run(LinMotResetStep.SwitchOff, () => { return _axis.SendOperation(LinMotOperation.SwitchOff); }, CheckSwitchOff, _delay_5s)
                .Run(LinMotResetStep.SwitchOn, () => { return _axis.SendOperation(LinMotOperation.SwitchOn); }, CheckSwitchOn, _delay_5s)
                .RunIf(LinMotResetStep.Home,CheckNotHome(), () => { return _axis.SendOperation(LinMotOperation.Home); }, NullFun, 100)
                .Delay(LinMotResetStep.HomeDelay, 500)
                .WaitWithStopCondition(LinMotResetStep.HomeWait,CheckHomed,CheckAxisHomeIsStop)
                .Run(LinMotResetStep.GoToInitialPosition, () => { return _axis.SendOperation(LinMotOperation.GoToInitialPosition); },NullFun,_delay_1ms)
                .Delay(LinMotResetStep.ReDelay,_delay_1s)
                .Wait(LinMotResetStep.ReWait,CheckInTarget,_delay_5s)
                .Run(LinMotResetStep.LastSwitchOn, () => { return  _axis.SendOperation(LinMotOperation.SwitchOn); }, CheckSwitchOn, _delay_5s)
                .RunIf(LinMotResetStep.LastSwitchOff, _lastNeedSwitchOff, () => { return _axis.SendOperation(LinMotOperation.SwitchOff); },_delay_1ms)
                .End(LinMotResetStep.End, NullFun);

            return Runner.Status;
        }
        public RState Start(params object[] objs)
        {
            _lastNeedSwitchOff = objs.Length != 0 ? (bool)objs[0] : false;
            return Runner.Start(Module, "Reset");
        }
        private bool CheckSwitchOff()
        {
            return !_axis.IsSwitchOn;
        }
        private bool CheckSwitchOn()
        {
            return _axis.IsSwitchOn;
        }

        private bool CheckNotHome()
        {
            return !_axis.IsHomed;
        }
        private bool CheckHomed()
        {
            return _axis.IsHomed;
        }

        private bool CheckInTarget()
        {
            return _axis.InTargetPosition;
        }
        private bool CheckAxisTargetIsStop()
        {
            if(_axis.InTargetPosition)
            {
                return false;
            }
            bool result= !_axis.IsMotorOn;
            if(result)
            {
                LOG.WriteLog(eEvent.ERR_PREWET, Module, "Axis is Stop");
                return true;
            }
            return false;
        }

        private bool CheckAxisHomeIsStop()
        {
            if (_axis.IsHomed)
            {
                return false;
            }
            bool result = !_axis.IsMotorOn;
            if (result)
            {
                LOG.WriteLog(eEvent.ERR_PREWET, Module, "Axis is Stop");
                return true;
            }
            return false;
        }
    }
}