using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using CyberX8_Core;
using CyberX8_RT.Modules;
using CyberX8_RT.Modules.Loader;
using CyberX8_RT.Modules.PUF;
using CyberX8_RT.Modules.SRD;
using CyberX8_RT.Modules.Transporter;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using System.Runtime.InteropServices;

namespace CyberX8_RT.Devices.Safety
{
    internal class SafetyNewAllOnRoutine : RoutineBase, IRoutine
    {
        private enum SafetyAllOnStep
        {
            Ready,
            SwitchOnProcessTransporter,
            WaitSwitchOnProcessTransporter,
            SwitchOnLoaderTransporter,
            WaitSwitchOnLoaderTransporter,
            SwitchOnLoader,
            WaitSwitchOnLoader,
            SwitchOnPuf1,
            WaitSwitchOnPuf1,
            SwitchOnPuf2,
            WaitSwitchOnPuf2,
            SwitchOnSRD1,
            WaitSwitchOnSRD1,
            SwitchOnSRD2,
            WaitSwitchOnSRD2,
            End
        }
        #region 内部变量
        private SafetyDevice _device;

        private TransporterSwitchOnRoutine _loaderTransporterSwitchOnRoutine;
        private TransporterSwitchOnRoutine _processTransporterSwitchOnRoutine;
        private LoaderSwitchAllOnRoutine _loaderSwitchAllOnRoutine;
        private PufSwitchOnRoutine _puf1SwitchOnRoutine;
        private PufSwitchOnRoutine _puf2SwitchOnRoutine;
        private SRDSwitchOnRoutine _srd1SwitchOnRoutine;
        private SRDSwitchOnRoutine _srd2SwitchOnRoutine;
        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="module"></param>
        public SafetyNewAllOnRoutine(string module) : base(module)
        {
            
        }
        /// <summary>
        /// 中止
        /// </summary>
        public void Abort()
        {
            Runner.Stop($"Safety All On Abort");
        }
        /// <summary>
        /// 监控
        /// </summary>
        /// <returns></returns>
        public RState Monitor()
        {
            Runner
                .RunIf(SafetyAllOnStep.SwitchOnProcessTransporter,ModuleHelper.IsInstalled(ModuleName.Transporter1),SwitchOnProcessTransporter, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnProcessTransporter, CheckSwitchOnProcessTransporterComplete, CheckSwitchOnProcessTransporterError)
                .RunIf(SafetyAllOnStep.SwitchOnLoaderTransporter,ModuleHelper.IsInstalled(ModuleName.Transporter2), SwitchOnLoaderTransporter, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnLoaderTransporter, CheckSwitchOnLoaderTransporterComplete, CheckSwitchOnLoaderTransporterError)
                .RunIf(SafetyAllOnStep.SwitchOnPuf1,ModuleHelper.IsInstalled(ModuleName.PUF1), SwitchOnPuf1, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnPuf1, CheckSwitchOnPuf1Complete, CheckSwitchOnPuf1Error)
                .RunIf(SafetyAllOnStep.SwitchOnPuf2,ModuleHelper.IsInstalled(ModuleName.PUF2), SwitchOnPuf2, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnPuf2, CheckSwitchOnPuf2Complete, CheckSwitchOnPuf2Error)
                .RunIf(SafetyAllOnStep.SwitchOnLoader,ModuleHelper.IsInstalled(ModuleName.Loader1), SwitchOnLoader, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnLoader, CheckSwitchOnLoaderComplete, CheckSwitchOnLoaderError)
                .RunIf(SafetyAllOnStep.SwitchOnSRD1, ModuleHelper.IsInstalled(ModuleName.SRD1), SwitchOnSRD1, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD1, CheckSwitchOnSRD1Complete, CheckSwitchOnSRD1Error)
                .RunIf(SafetyAllOnStep.SwitchOnSRD2, ModuleHelper.IsInstalled(ModuleName.SRD2), SwitchOnSRD2, _delay_1ms)
                .WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD2, CheckSwitchOnSRD2Complete, CheckSwitchOnSRD2Error)
                .End(SafetyAllOnStep.End, NullFun, _delay_1ms);

            return Runner.Status;
        }
        #region ProcessTransporter
        /// <summary>
        /// Switch on ProcessTransporter Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnProcessTransporter()
        {
            return _processTransporterSwitchOnRoutine.Start()==RState.Running;
        }
        /// <summary>
        /// 检查ProcessTransporter Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnProcessTransporterComplete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_processTransporterSwitchOnRoutine);
        }
        /// <summary>
        /// 检查ProcessTransporter Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnProcessTransporterError()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                return false;
            }
            return CommonFunction.CheckRoutineStopState(_processTransporterSwitchOnRoutine);
        }
        #endregion

        #region LoaderTransporter
        /// <summary>
        /// Switch on LoaderTransporter Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnLoaderTransporter()
        {
            return _loaderTransporterSwitchOnRoutine.Start() == RState.Running;
        }
        /// <summary>
        /// 检查LoaderTransporter Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnLoaderTransporterComplete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter2))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_loaderTransporterSwitchOnRoutine);
        }
        /// <summary>
        /// 检查LoaderTransporter Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnLoaderTransporterError()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter2))
            {
                return false;
            }
            return CommonFunction.CheckRoutineStopState(_loaderTransporterSwitchOnRoutine);
        }
        #endregion

        #region Puf1
        /// <summary>
        /// Switch on Puf1 Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnPuf1()
        {
            return _puf1SwitchOnRoutine.Start()==RState.Running;
        }
        /// <summary>
        /// 检查Puf1 Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnPuf1Complete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.PUF1))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_puf1SwitchOnRoutine);
        }
        /// <summary>
        /// 检查Puf1 Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnPuf1Error()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.PUF1))
            {
                return false;
            }
            return CommonFunction.CheckRoutineStopState(_puf1SwitchOnRoutine);
        }
        #endregion

        #region Puf2
        /// <summary>
        /// Switch on Puf2 Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnPuf2()
        {
            return _puf2SwitchOnRoutine.Start()==RState.Running;    
        }
        /// <summary>
        /// 检查Puf1 Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnPuf2Complete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.PUF2))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_puf2SwitchOnRoutine);
        }
        /// <summary>
        /// 检查Puf2 Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnPuf2Error()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                return false;
            }
            return CommonFunction.CheckRoutineStopState(_puf2SwitchOnRoutine);
        }
        #endregion

        #region Loader
        /// <summary>
        /// Switch on Loader Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnLoader()
        {
            return _loaderSwitchAllOnRoutine.Start() == RState.Running;
        }
        /// <summary>
        /// 检查Loader Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnLoaderComplete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Loader1))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_loaderSwitchAllOnRoutine);
        }
        /// <summary>
        /// 检查Loader Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnLoaderError()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                return false;
            }
            return CommonFunction.CheckRoutineEndState(_loaderSwitchAllOnRoutine);
        }
        #endregion

        #region SRD1
        /// <summary>
        /// Switch on SRD1 Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnSRD1()
        {
            return _srd1SwitchOnRoutine.Start() == RState.Running;
        }
        /// <summary>
        /// 检查SRD1 Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnSRD1Complete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.SRD1))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_srd1SwitchOnRoutine);
        }
        /// <summary>
        /// 检查SRD1 Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnSRD1Error()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.SRD1))
            {
                return false;
            }
            return CommonFunction.CheckRoutineStopState(_srd1SwitchOnRoutine);
        }
        #endregion

        #region SRD2
        /// <summary>
        /// Switch on SRD2 Motors
        /// </summary>
        /// <returns></returns>
        private bool SwitchOnSRD2()
        {
            return _srd2SwitchOnRoutine.Start()==RState.Running;
        }
        /// <summary>
        /// 检查SRD2 Motors switchon是否完成
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnSRD2Complete()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.SRD2))
            {
                return true;
            }
            return CommonFunction.CheckRoutineEndState(_srd2SwitchOnRoutine);
        }
        /// <summary>
        /// 检查SRD2 Motors switchon是否出错
        /// </summary>
        /// <returns></returns>
        private bool CheckSwitchOnSRD2Error()
        {
            if (!ModuleHelper.IsInstalled(ModuleName.SRD2))
            {
                return true;
            }
            return CommonFunction.CheckRoutineStopState(_srd2SwitchOnRoutine);
        }
        #endregion

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public RState Start(params object[] objs)
        {
            _loaderTransporterSwitchOnRoutine = new TransporterSwitchOnRoutine(ModuleName.Transporter2.ToString());
            _processTransporterSwitchOnRoutine = new TransporterSwitchOnRoutine(ModuleName.Transporter1.ToString());
            _loaderSwitchAllOnRoutine = new LoaderSwitchAllOnRoutine(ModuleName.Loader1.ToString());
            _puf1SwitchOnRoutine = new PufSwitchOnRoutine(ModuleName.PUF1);
            _puf2SwitchOnRoutine = new PufSwitchOnRoutine(ModuleName.PUF2);
            _srd1SwitchOnRoutine = new SRDSwitchOnRoutine(ModuleName.SRD1.ToString());
            _srd2SwitchOnRoutine = new SRDSwitchOnRoutine(ModuleName.SRD2.ToString());
            _device = DEVICE.GetDevice<SafetyDevice>(Module);
            return Runner.Start(Module, $"Safety All On");
        }
    }
}