using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using CyberX8_RT.Devices.AXIS;
using CyberX8_RT.Modules.Transporter;
using CyberX8_RT.Modules;
using MECF.Framework.Common.Equipment;
using System;
namespace CyberX8_RT.Devices.TransPorter
{
    public class LoaderTransPorterElevatorAxisInterLock : IAxisInterLock
    {
        #region 内部变量
        private JetAxisBase _axis;
        #endregion
        #region 属性
        /// 
        /// 模块名称
        /// 
        public string Module { get { return _axis.Module; } }
        /// 
        /// 子模块名称
        /// 
        public string Name { get { return _axis.Name; } }
        #endregion
        /// 
        /// 栣函数
        /// 
        /// 
        /// 
        public LoaderTransPorterElevatorAxisInterLock(JetAxisBase axis)
        {
            _axis = axis;
        }
        /// 
        /// GotoPosition条件检验
        /// 
        /// 
        /// 
        /// 
        public bool CheckGotoPosition(string station)
        {
            if (!AxisManager.Instance.CheckModuleAxisSwitchOn(Module, Name))
            {
                return false;
            }            
            //ProcessTransporter Home
            if (ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                TransporterEntity processTransporterEntity = Singleton.Instance.GetModule(ModuleName.Transporter1.ToString());
                if (processTransporterEntity == null)
                {
                    LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{ModuleName.Transporter1.ToString()} entity is null");
                    return false;
                }
                if (!processTransporterEntity.IsHomed)
                {
                    LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{ModuleName.Transporter1.ToString()} is not home, Cannot execute GotoSavedPosition");
                    return false;
                }
            }
            //LoaderTransporter 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;
                }
            }
            //LoaderTransporter Gantry is not run
            JetAxisBase gantryAxis = DEVICE.GetDevice($"{Module}.Gantry");
            if (gantryAxis == null)
            {
                LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{Module} Gantry Axis is null");
                return false;
            }
            if (gantryAxis.Status == CyberX8_Core.RState.Running)
            {
                LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{Module} Gantry Axis is Run");
                return false;
            }
            //Check Gantry at station
            double gantryPosition = gantryAxis.MotionData.MotorPosition;
            //if(gantryAxis.CheckPositionIsEmpty(gantryPosition))
            //{
            //    LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"{Module} Gantry is not in any station");
            //    return false;
            //}
            //是否在Load位
            if (gantryAxis.CheckPositionIsInStation(gantryPosition, "Loader"))
            {
                JetAxisBase rotationAxis = DEVICE.GetDevice($"Loader1.Rotation");
                double rotationPosition = rotationAxis.MotionData.MotorPosition;
                if (!rotationAxis.CheckPositionIsInStation(rotationPosition, "TRNPA") && !rotationAxis.CheckPositionIsInStation(rotationPosition, "TRNPB"))
                {
                    if(station != "UP" && station != "CELLTOP")
                    {
                        LOG.WriteLog(eEvent.ERR_TRANSPORTER, Module, $"Loader Rotation is not in TRNP, {Module} Gantry only can go to UP or CELLTOP");
                        return false;
                    }
                }
            }          
            return true;
        }
    }
}