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;

namespace CyberX8_RT.Devices.TransPorter
{
    public class ProcessTransporterElevatorAxisInterLock : IAxisInterLock
    {
        #region 内部变量
        private JetAxisBase _axis;
        #endregion
        #region 属性
        /// <summary>
        /// 模块名称
        /// </summary>
        public string Module { get { return _axis.Module; } }
        /// <summary>
        /// 子模块名称
        /// </summary>
        public string Name { get { return _axis.Name; } }
        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Module"></param>
        /// <param name="name"></param>
        public ProcessTransporterElevatorAxisInterLock(JetAxisBase axis)
        {
            _axis = axis;
        }
        /// <summary>
        /// GotoPosition条件检验
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public bool CheckGotoPosition(string station)
        {
            if (!AxisManager.Instance.CheckModuleAxisSwitchOn(Module, Name))
            {
                return false;
            }

            //ProcessTransporter Home
            if (ModuleHelper.IsInstalled(ModuleName.Transporter1))
            {
                TransporterEntity processTransporterEntity = Singleton<RouteManager>.Instance.GetModule<TransporterEntity>(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<RouteManager>.Instance.GetModule<TransporterEntity>(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;
                }
            }
            //ProcessTransporter Gantry is not run
            JetAxisBase gantryAxis = DEVICE.GetDevice<JetAxisBase>($"{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;
            //}

            return true;
        }
    }
}