using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using CyberX8_Core;
using CyberX8_RT.Devices.AXIS;
using CyberX8_RT.Devices.AXIS.CANOpen;
using CyberX8_RT.Modules.PUF;
using CyberX8_RT.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.CommonData.Loader;
using CyberX8_RT.Modules.Transporter;
using Aitex.Core.RT.SCCore;
namespace CyberX8_RT.Devices.Loader
{
    public class LoaderUnloadRoutine : RoutineBase, IRoutine
    {
        private enum UnloadStep
        {
            CheckPreCondition,
            WSClampOn,
            LSGotoSetUp,
            LSGotoSetUpWait,
            ShuttleGotoIN,
            ShuttleGotoINWait,
            BernoulliBladderOn,
            BernoulliBladderOnCheck,
            BernoulliN2On,
            WSBladderOn,
            WSBladderOnCheck,
            LSVacuumOn,
            LSVacuumOnCheck,
            LSVacuumLevelCheck,
            LSVacuumLevelCheckWait,
            ShuttleGotoLS,
            ShuttleGotoLSWait,
            LSGotoSetup,
            LSGotoSetupWait,
            LSGotoUnlock,
            LSGotoUnlockWait,
            ReLSVacuumLevelCheck,
            ReLSVacuumLevelCheckWait,
            ShuttleGotoOUT,
            ShuttleGotoOUTWait,
            TiltGotoHori,
            TiltGotoHoriCheck,
            BernoulliN2Off,
            WSBladderOff,
            WSBladderOffCheck,            
            End
        }
        #region 内部变量
        private string _side = "";
        private JetAxisBase _lsAxis;
        private LoaderSideBernoulliBladderRoutine _bernoulliBladderRoutine;
        private LoaderSideVacuumRoutine _vacuumRoutine;
        private LoaderSideVacuumLevelCheckRoutine _vacuumLevelCheckRoutine;
        private LoaderSideWhBladderRoutine _whBladderRoutine;
        private JetAxisBase _shuttleAxis;
        private JetAxisBase _tiltAxis;
        private JetAxisBase _rotationAxis;
        private LoaderSideDevice _sideDevice;
        private LoaderCommonDevice _loaderCommonDevice;
        /// 
        /// WaferSize
        /// 
        private int _waferSize;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public LoaderUnloadRoutine(string module, string side) : base(module)
        {
            _side = side;           
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(UnloadStep.CheckPreCondition,CheckPreCondition,_delay_1ms)
                //1. WS Clamp on
                .Run(UnloadStep.WSClampOn, WSClampOnAction, () => { return _loaderCommonDevice.CommonData.WaferHolderClamp; }, _delay_5s)
                //2. LS Goto SetUp位
                .Run(UnloadStep.LSGotoSetUp, () => { return AxisPosition(_lsAxis,$"Setup{_waferSize}"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.LSGotoSetUpWait, () => { return _lsAxis.Status == RState.End; }, 
                    ()=>CheckAxisMotionStopStatus(_lsAxis))
                //3. Shuttle Goto IN
                .Run(UnloadStep.ShuttleGotoIN, () => { return AxisPosition(_shuttleAxis, "IN"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.ShuttleGotoINWait, () => { return _shuttleAxis.Status == RState.End; },
                    () => CheckAxisMotionStopStatus(_shuttleAxis))
                //4. BernoulliBladderOn
                .Run(UnloadStep.BernoulliBladderOn, () => { return _bernoulliBladderRoutine.Start(true) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.BernoulliBladderOnCheck, () => { return CommonFunction.CheckRoutineEndState(_bernoulliBladderRoutine); }, 
                    () => CheckRoutineStopStatus(_bernoulliBladderRoutine, "BernoulliBladder On failed"))
                //5. BernoulliN2 On
                .Run(UnloadStep.BernoulliN2On, BernoulliN2On, () => { return _sideDevice.SideData.BernoulliN2; }, _delay_5s)
                //6. WS Bladder On
                .Run(UnloadStep.WSBladderOn, () => { return _whBladderRoutine.Start(true) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.WSBladderOnCheck, () => { return CommonFunction.CheckRoutineEndState(_whBladderRoutine); },
                    () => CheckRoutineStopStatus(_whBladderRoutine, "WSBladder On failed"))
                //7. LS Vacuum On
                .Run(UnloadStep.LSVacuumOn, () => { return _vacuumRoutine.Start(true) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.LSVacuumOnCheck, () => { return CommonFunction.CheckRoutineEndState(_vacuumRoutine); },
                    () => CheckRoutineStopStatus(_vacuumRoutine, "LS Vaccum On failed"))
                //8. LS vacuum level check
                .Run(UnloadStep.LSVacuumLevelCheck, () => { return _vacuumLevelCheckRoutine.Start(true) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.LSVacuumLevelCheckWait, () => { return CommonFunction.CheckRoutineEndState(_vacuumLevelCheckRoutine); },
                    () => CheckRoutineStopStatus(_vacuumLevelCheckRoutine, "LS Vacuum Level Check failed"))
                //9. Shuttle Goto LS
                .Run(UnloadStep.ShuttleGotoLS, () => { return AxisPosition(_shuttleAxis, "LS"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.ShuttleGotoLSWait, () => { return _shuttleAxis.Status == RState.End; },
                    () => CheckAxisMotionStopStatus(_shuttleAxis))
                //10. LS Goto Unlock
                .Run(UnloadStep.LSGotoUnlock, () => { return AxisPosition(_lsAxis, $"Unlock{_waferSize}"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.LSGotoUnlockWait, () => { return _lsAxis.Status == RState.End; },
                    () => CheckAxisMotionStopStatus(_lsAxis))
                //11. Re LS Vacuum level Check
                .Run(UnloadStep.ReLSVacuumLevelCheck, () => { return _vacuumLevelCheckRoutine.Start(true) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.ReLSVacuumLevelCheckWait, () => { return CommonFunction.CheckRoutineEndState(_vacuumLevelCheckRoutine); }, 
                    () => CheckRoutineStopStatus(_vacuumLevelCheckRoutine,"LS Vacuum Level check failed"))
                //12. Shuttle Goto OUT
                .Run(UnloadStep.ShuttleGotoOUT, () => { return AxisPosition(_shuttleAxis, $"OUT{_waferSize}"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.ShuttleGotoOUTWait, () => { return _shuttleAxis.Status == RState.End; },
                    () => CheckAxisMotionStopStatus(_shuttleAxis))
                //13. Tilt Goto HORI
                .Run(UnloadStep.TiltGotoHori, () => { return AxisPosition(_tiltAxis,"HORI"); }, NullFun, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.TiltGotoHoriCheck, () => { return _tiltAxis.Status == RState.End; }, 
                    () => CheckAxisMotionStopStatus(_tiltAxis))
                //14. Bernoulli N2 Off
                .Run(UnloadStep.BernoulliN2Off, BernoulliN2Off, () => { return !_sideDevice.SideData.BernoulliN2; }, _delay_5s)
                //15. WS Bladder Off
                .Run(UnloadStep.WSBladderOff, () => { return _whBladderRoutine.Start(false) == RState.Running; }, _delay_1ms)
                .WaitWithStopCondition(UnloadStep.WSBladderOffCheck, () => { return CommonFunction.CheckRoutineEndState(_whBladderRoutine); },
                    () => CheckRoutineStopStatus(_whBladderRoutine, "WSBladder Off failed"))
                //17 Home CRS Axis
                //.Run(UnloadStep.HomingCRSAxis, CRSHome, _delay_1ms)
                //.WaitWithStopCondition(UnloadStep.HomingCRSAxisWait, () => { return  _lsAxis.Status == RState.End; },
                //    () => CheckCRSHomeStopStatus())
                //.Run(UnloadStep.CRSHomedGotoSetUp, () => { return AxisPosition(_lsAxis, $"Setup{_waferSize}"); }, _delay_1ms)
                //.WaitWithStopCondition(UnloadStep.CRSHomedGotoSetUpCheck, () => { return _lsAxis.Status == RState.End; },
                //    () => CheckAxisMotionStopStatus(_lsAxis))
                .End(UnloadStep.End, NullFun, 10);
            return Runner.Status;
        }
        /// 
        /// 检验Routine异常状态
        /// 
        /// 
        private bool CheckRoutineStopStatus(IRoutine routine,string error)
        {
            bool result = CommonFunction.CheckRoutineStopState(routine);
            if (result)
            {
                NotifyError(eEvent.ERR_LOADER, $"{error}", 0);
            }
            return result;
        }
        /// 
        /// Axis goto position
        /// 
        /// 
        /// 
        /// 
        private bool AxisPosition(JetAxisBase axis,string station)
        {
            bool result = axis.PositionStation(station);
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, $"{axis.Module} goto {station} failed", 0);
            }
            return result;
        }
        /// 
        /// 检验电机运动异常状态
        /// 
        /// 
        /// 
        private bool CheckAxisMotionStopStatus(JetAxisBase axis)
        {
            bool result = axis.Status == RState.Failed || axis.Status == RState.Timeout;
            if (result)
            {
                NotifyError(eEvent.ERR_LOADER, $"{axis.Module} motion failed", 0);
            }
            return result;
        }
        /// 
        /// BerolliN2 On
        /// 
        /// 
        private bool BernoulliN2On()
        {
            bool result= _sideDevice.BernoulliN2OnAction("", null);
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, $"BernoulliN2 On failed", 0);
            }
            return result;
        }
        /// 
        /// BernoulliN2Off
        /// 
        /// 
        private bool BernoulliN2Off()
        {
            bool result = _sideDevice.BernoulliN2OffAction("", null);
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, "BernoulliN2 Off failed", 0);
            }
            return result;
        }
        /// 
        /// Wafer Holder Clamp Off
        /// 
        /// 
        public bool WSClampOffAction()
        {
            bool result = _loaderCommonDevice.WaferHolderClampOffAction();
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, "Wafer Shuttle Clamp Off failed", 2);
            }
            return result;
        }
        /// 
        /// Wafer Holder Clamp On
        /// 
        /// 
        public bool WSClampOnAction()
        {
            bool result = _loaderCommonDevice.WaferHolderClampOnAction();
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, "Wafer Shuttle Clamp On failed", 2);
            }
            return result;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _waferSize = SC.GetValue($"Loader1.{_side}WaferSize");
            _shuttleAxis = GetShuttleAxis();
            _lsAxis = GetCrsAxis();
            _tiltAxis = GetTiltAxis();
            _loaderCommonDevice = DEVICE.GetDevice($"Loader1.Common");
            _rotationAxis = DEVICE.GetDevice($"{ModuleName.Loader1}.Rotation");
            _sideDevice = DEVICE.GetDevice($"{Module}.{_side}");
            _vacuumRoutine = new LoaderSideVacuumRoutine($"{Module}.{_side}");
            _vacuumLevelCheckRoutine = new LoaderSideVacuumLevelCheckRoutine($"{Module}.{_side}");
            //_unloadVacuumLevelCheckRoutine = new LoaderSideUnloadVacuumLevelCheckRoutine($"{Module}.{_side}");
            _whBladderRoutine = new LoaderSideWhBladderRoutine($"{Module}.{_side}");
            _bernoulliBladderRoutine = new LoaderSideBernoulliBladderRoutine($"{Module}.{_side}");
            Runner.Start(Module, $"Unload {_side}");
            return RState.Running;
        }
        /// 
        /// 获取Shuttle轴对象
        /// 
        /// 
        private JetAxisBase GetShuttleAxis()
        {
            switch (_side)
            {
                case "SideA":
                    return DEVICE.GetDevice($"{Module}.ShuttleA");
                default:
                    return DEVICE.GetDevice($"{Module}.ShuttleB");
            }
        }
        /// 
        /// 获取CRS轴对象
        /// 
        /// 
        private JetAxisBase GetCrsAxis()
        {
            switch (_side)
            {
                case "SideA":
                    return DEVICE.GetDevice($"{Module}.LSA");
                default:
                    return DEVICE.GetDevice($"{Module}.LSB");
            }
        }
        /// 
        /// 获取Tilt轴对象
        /// 
        /// 
        private JetAxisBase GetTiltAxis()
        {
            switch (_side)
            {
                case "SideA":
                    return DEVICE.GetDevice($"{Module}.TiltA");
                default:
                    return DEVICE.GetDevice($"{Module}.TiltB");
            }
        }
        /// 
        /// 检验前置条件
        /// 
        /// 
        private bool CheckPreCondition()
        {
            if (!CheckHomeCondition())
            {
                return false;
            }
            if (!CheckUnloadAxisCondition())
            {
                return false;
            }
            if (!UnloadStatusCheck())
            {
                return false;
            }
            return true;
        }
        /// 
        /// 检验Home条件
        /// 
        /// 
        private bool CheckHomeCondition()
        {
            //检验PUF、Loader Transporter,Robot均Homed
            //Efem Home
            if (ModuleHelper.IsInstalled(ModuleName.EFEM))
            {
                EfemEntity efemEntity = Singleton.Instance.GetModule(ModuleName.EFEM.ToString());                
                if (!efemEntity.IsHomed)
                {
                    LOG.WriteLog(eEvent.ERR_EFEM_ROBOT, Module, $"{ModuleName.EFEM.ToString()} is not home, Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            if (ModuleHelper.IsInstalled(ModuleName.PUF1))
            {
                PUFEntity puf1Entity = Singleton.Instance.GetModule(ModuleName.PUF1.ToString());
                if (!puf1Entity.IsHomed)
                {
                    NotifyError(eEvent.ERR_LOADER, "PUF1 is not homed",-1);
                    return false;
                }
            }           
            if (ModuleHelper.IsInstalled(ModuleName.Transporter2))
            {
                TransporterEntity loaderTransportEntity = Singleton.Instance.GetModule(ModuleName.Transporter2.ToString());
                if (!loaderTransportEntity.IsHomed)
                {
                    NotifyError(eEvent.ERR_LOADER, "Loader Transporter is not homed",-1);
                    return false;
                }
            }
            return true;
        }
        /// 
        /// 检验Axis条件
        /// 
        /// 
        /// 
        private bool CheckUnloadAxisCondition()
        {
            if (!_rotationAxis.IsHomed)
            {
                NotifyError(eEvent.ERR_LOADER, $"Rotation is not homed",-1);
                return false;
            }
            if (!_shuttleAxis.IsHomed)
            {
                NotifyError(eEvent.ERR_LOADER, $"{_shuttleAxis.Name} is not homed", -1);
                return false;
            }
            if (!_tiltAxis.IsHomed)
            {
                NotifyError(eEvent.ERR_LOADER, $"{_tiltAxis.Name} is not homed",-1);
                return false;
            }
            if (!_lsAxis.IsHomed)
            {
                NotifyError(eEvent.ERR_LOADER, $"{_lsAxis.Name} is not homed", -1);
                return false;
            }
            //LS已经运动到 Setup 位
            double crsPosition = _lsAxis.MotionData.MotorPosition;
            if (!_lsAxis.CheckPositionIsInStation(crsPosition, $"Setup{_waferSize}"))
            {
                NotifyError(eEvent.ERR_LOADER, $"LS {crsPosition} not in Setup{_waferSize}", -1);
                return false;
            }
            //Rotation已经运动到 loaderA 位(sideA 下片时)或 loaderB 位(sideB 下片时)
            double rotationPosition = _rotationAxis.MotionData.MotorPosition;
            if(_side == "SideA")
            {      
                
                if (!_rotationAxis.CheckPositionIsInStation(rotationPosition, $"LOADA{_waferSize}"))
                {
                    NotifyError(eEvent.ERR_LOADER, $"Rotation {rotationPosition} not in LOADA{_waferSize}", -1);
                    return false;
                }
            }
            else
            {
                if (!_rotationAxis.CheckPositionIsInStation(rotationPosition, $"LOADB{_waferSize}"))
                {
                    NotifyError(eEvent.ERR_LOADER, $"Rotation {rotationPosition} not in LOADB{_waferSize}", -1);
                    return false;
                }
            }            
            //Shuttle已经运动到 MID 位
            double shuttlePosition =_shuttleAxis.MotionData.MotorPosition;
            if (!_shuttleAxis.CheckPositionIsInStation(shuttlePosition, "MID"))
            {
                NotifyError(eEvent.ERR_LOADER, $"Shuttle {shuttlePosition} not in MID", -1);
                return false;
            }
            //Tilt已经运动到 VERT 位
            double tiltPosition = _tiltAxis.MotionData.MotorPosition;
            if (!_tiltAxis.CheckPositionIsInStation(tiltPosition, "VERT"))
            {
                NotifyError(eEvent.ERR_LOADER, $"Tilt {tiltPosition} not in VERT", -1);
                return false;
            }
                     
            return true;
        }
        
        /// 
        /// 检验状态条件
        /// 
        /// 
        /// 
        private bool UnloadStatusCheck()
        {
            LoaderSideData sideData = _sideDevice.SideData;
            LoaderCommonData commonData = _loaderCommonDevice.CommonData;
            //Bernoulli N2(BernoulliN2 off)
            if (sideData.BernoulliN2)
            {
                NotifyError(eEvent.ERR_LOADER, "Bernoulli N2 is on",-1);
                return false;
            }
            //LS Vacuum检验(LS Vaccum off)
            if (sideData.CRSVacuum)
            {
                NotifyError(eEvent.ERR_LOADER, "LS Vacuum is on", -1);
                return false;
            }
          
            //Wafer Shuttle Present(WS present)         
            if (!commonData.WaferHolderPresent)
            {
                NotifyError(eEvent.ERR_LOADER, "Wafer Shuttle is absent", -1);
                return false;
            }
            //Wafer Shuttle Clamp(Clamp on/off均可)          
            
            //Wafer Shuttle Bladder(Bladder off)          
            if (sideData.WHBladder)
            {
                NotifyError(eEvent.ERR_LOADER, "Wafer Shuttle Bladder is on", -1);
                return false;
            }
            //Drip Tray Fluid(Drip Tray Fluid正常)
            if (commonData.DripTrayFluid)
            {
                NotifyError(eEvent.ERR_LOADER, "Drip Tray Fluid is on", -1);
                return false;
            }
            return true;
        }
        /// 
        /// CRS Home
        /// 
        /// 
        private bool CRSHome()
        {
            bool result = _lsAxis.Home();
            if (!result)
            {
                NotifyError(eEvent.ERR_LOADER, "LS Axis home failed", 0);
            }
            return result;
        }
        /// 
        /// 检验CRS home异常状态
        /// 
        /// 
        private bool CheckCRSHomeStopStatus()
        {
            bool result = _lsAxis.Status == RState.Failed || _lsAxis.Status == RState.Timeout;
            if (result)
            {
                NotifyError(eEvent.ERR_LOADER, "LS Axis home failed", 0);
            }
            return result;
        }
    }
}