using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.Equipment;
using CyberX8_RT.Devices.AXIS;
using System;
using Aitex.Core.Util;
using CyberX8_RT.Modules.PUF;
using CyberX8_RT.Modules.Transporter;
using CyberX8_RT.Modules;
namespace CyberX8_RT.Devices.Loader
{
    public class LoaderShuttleAxisInterLock : IAxisInterLock
    {
        #region 内部变量
        private JetAxisBase _axis;
        #endregion
        #region 属性
        /// 
        /// 模块名称
        /// 
        public string Module { get { return _axis.Module ; } }
        /// 
        /// 子模块名称
        /// 
        public string Name { get { return _axis.Name; } }
        #endregion
        #region 内部变量
        private LoaderSideDevice _loaderSide = null;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        /// 
        public LoaderShuttleAxisInterLock(JetAxisBase axis)
        {
            _axis = axis;
        }
        /// 
        /// 加载LoaderSide对象
        /// 
        private void GetLoaderSide()
        {
            if (_loaderSide == null)
            {
                switch (Name)
                {
                    case "ShuttleA":
                        _loaderSide = DEVICE.GetDevice($"{Module}.SideA");
                        break;
                    default:
                        _loaderSide = DEVICE.GetDevice($"{Module}.SideB");
                        break;
                }
            }
        }
        /// 
        /// GotoPosition条件检验
        /// 
        /// 
        /// 
        /// 
        public bool CheckGotoPosition(string station)
        {
            //Check Home
            if (!CheckHomeCondition())
            {
                return false;
            }
            //Shuttle Home
            if (!_axis.IsHomed)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{Name} is not home, Cannot execute GotoSavedPosition");
                return false;
            }
            JetAxisBase rotationAxis = DEVICE.GetDevice($"{Module}.Rotation");
            if(rotationAxis == null)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{Module} rotation Axis is null");
                return false;
            }
            if (!rotationAxis.IsSwitchOn)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{rotationAxis.Module} is switch off");
                return false;
            }
            if (rotationAxis.Status == CyberX8_Core.RState.Running)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{Name} rotation status is running, Cannot execute GotoSavedPosition");
                return false;
            }
            GetLoaderSide();
            JetAxisBase tiltAxis = null;
            if (Name == "ShuttleA")
            {
                tiltAxis = DEVICE.GetDevice($"{Module}.TiltA");
            }
            else
            {
                tiltAxis = DEVICE.GetDevice($"{Module}.TiltB");
            }
            if (tiltAxis == null)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{Module} tilt Axis is null");
                return false;
            }
            if (!tiltAxis.IsSwitchOn)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{tiltAxis.Module} is switch off");
                return false;
            }
            if (tiltAxis.Status==CyberX8_Core.RState.Running)
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, "tilt status is running,Cannot execute GotoSavedPosition");
                return false;
            }
            //Loader 的 TILT 轴在‘VERT’位
            double tiltPosition = tiltAxis.MotionData.MotorPosition;
            if (!tiltAxis.CheckPositionIsInStation(tiltPosition, "VERT"))
            {
                LOG.WriteLog(eEvent.ERR_LOADER, Module, $"{Module} Tilt {tiltPosition} is not in VERT station,Cannot execute GotoSavedPosition");
                return false;
            }
            //PUF1 的 ROTATION 轴在‘HOME’、‘FLIP’或‘ROBOT’位
            if (ModuleHelper.IsInstalled(ModuleName.PUF1))
            {
                JetAxisBase puf1RotationAxis = DEVICE.GetDevice($"{ModuleName.PUF1}.Rotation");
                if (puf1RotationAxis == null)
                {
                    LOG.WriteLog(eEvent.ERR_PUF, Module, "Puf1 Rotation Axis is null");
                    return false;
                }
                double puf1RotationPosition = puf1RotationAxis.MotionData.MotorPosition;
                if (!puf1RotationAxis.CheckPositionIsInStation(puf1RotationPosition, "Home")
                    && !puf1RotationAxis.CheckPositionIsInStation(puf1RotationPosition, "Flip")
                    && !puf1RotationAxis.CheckPositionIsInStation(puf1RotationPosition, "Robot"))
                {
                    LOG.WriteLog(eEvent.ERR_LOADER, Module, $"PUF1 Rotation {puf1RotationPosition} is not in Home, Flip or Robot station,Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            return true;
        }
        /// 
        /// Check Home
        /// 
        /// 
        private bool CheckHomeCondition()
        {
            //Efem Home
            if (ModuleHelper.IsInstalled(ModuleName.EFEM))
            {
                EfemEntity efemEntity = Singleton.Instance.GetModule(ModuleName.EFEM.ToString());
                if (efemEntity == null)
                {
                    LOG.WriteLog(eEvent.ERR_EFEM_ROBOT, Module, $"{ModuleName.EFEM.ToString()} entity is null");
                    return false;
                }
                if (!efemEntity.IsHomed)
                {
                    LOG.WriteLog(eEvent.ERR_EFEM_ROBOT, Module, $"{ModuleName.EFEM.ToString()} is not home, Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            //Transporter2 Home
            if (ModuleHelper.IsInstalled(ModuleName.Transporter2))
            {
                TransporterEntity loaderTransporterEntity = Singleton.Instance.GetModule(ModuleName.Transporter2.ToString());
                if (loaderTransporterEntity == null)
                {
                    LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{ModuleName.Transporter2.ToString()} entity is null");
                    return false;
                }
                if (!loaderTransporterEntity.IsHomed)
                {
                    LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{ModuleName.Transporter2.ToString()} is not home, Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            //Puf1 Home
            if (ModuleHelper.IsInstalled(ModuleName.PUF1))
            {
                PUFEntity puf1Entity = Singleton.Instance.GetModule(ModuleName.PUF1.ToString());
                if (puf1Entity == null)
                {
                    LOG.WriteLog(eEvent.ERR_PUF, Module, $"{ModuleName.PUF1.ToString()} entity is null");
                    return false;
                }
                if (!puf1Entity.IsHomed)
                {
                    LOG.WriteLog(eEvent.ERR_PUF, Module, $"{ModuleName.PUF1.ToString()} is not home, Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            return true;
        }
    }
}