using Aitex.Core.Common;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using MECF.Framework.Common.DBCore;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.SubstrateTrackings;
using MECF.Framework.Common.Utilities;
using MECF.Framework.Common.WaferHolder;
using CyberX8_Core;
using CyberX8_RT.Devices.AXIS;
using CyberX8_RT.Devices.Loader;
using CyberX8_RT.Devices.PUF;
using CyberX8_RT.Modules.Loader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Modules.PUF
{
    public class PufPlaceToLoaderRoutine : RoutineBase, IRoutine
    {
        private enum PlaceStep
        {
            OnlyPlaceToLoader,
            OnlyPlaceToLoaderWait,
            SwitchWaferHolder,
            LastRotationHomeStation,
            LastRotationHomeStationWait,
            End
        }
        #region 常量
        private const string CURRENT_STATION_LIST = "CurrentStationList";
        private const string SIDE_A = "SideA";
        private const string SIDE_B = "SideB";
        #endregion
        #region 内部变量
        private string _loaderSide;
        private string _pufSide = "";
        private LoaderEntity _loaderEntity;
        private JetAxisBase _flipAxis;
        private JetAxisBase _rotationAxis;
        private PufVacuum _vacuum;
        private LoaderSideDevice _loaderSideDevice;
        private JetAxisBase _loaderCrsAxis;
        private JetAxisBase _gantryAxis;
        private PufOnlyPlaceToLoaderRoutine _onlyPlaceToLoaderRoutine;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public PufPlaceToLoaderRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            if (_onlyPlaceToLoaderRoutine != null)
            {
                _onlyPlaceToLoaderRoutine.Abort();
            }
            _flipAxis.StopPositionOperation();
            _rotationAxis.StopPositionOperation();
            Runner.Stop("Manual Abort");            
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(PlaceStep.OnlyPlaceToLoader,StartOnlyPlaceToLoader,_delay_1ms)
                .WaitWithStopCondition(PlaceStep.OnlyPlaceToLoaderWait,CheckPlaceEndStatus,CheckPlaceStopStatus)
                .Run(PlaceStep.SwitchWaferHolder,SwitchWaferHolderSideWafer,_delay_1ms)
                .Run(PlaceStep.LastRotationHomeStation, () => _rotationAxis.PositionStation("Home"), _delay_1ms)
                .WaitWithStopCondition(PlaceStep.LastRotationHomeStationWait, CheckRotationPositionStatus, CheckRotationPositionRunStop)
                .End(PlaceStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 执行放片至Loader
        /// 
        /// 
        private bool StartOnlyPlaceToLoader()
        {
            return _onlyPlaceToLoaderRoutine.Start(_pufSide,_loaderSide) == RState.Running;
        }
        /// 
        /// 检验放片状态 
        /// 
        /// 
        private bool CheckPlaceEndStatus()
        {
            return _onlyPlaceToLoaderRoutine.Monitor() ==RState.End;
        }
        /// 
        /// 检验放片停止状态
        /// 
        /// 
        private bool CheckPlaceStopStatus()
        {
            RState state = _onlyPlaceToLoaderRoutine.Monitor();
            if(state==RState.Failed||state==RState.Timeout)
            {
                NotifyError(eEvent.ERR_PUF, "puf place wafer failed", 0);
                return true;
            }
            return false;
        }
        /// 
        /// WaferHolder交换片
        /// 
        /// 
        private bool SwitchWaferHolderSideWafer()
        {
            WaferHolderInfo waferHolderInfo = _loaderEntity.WaferHolderInfo;
            if (waferHolderInfo != null)
            {
                if (_loaderSide==SIDE_A)
                {
                    WaferManager.Instance.WaferMoved(ModuleHelper.Converter(Module), 1, ModuleName.Loader1, 0);
                    WaferInfo loaderWaferInfo = WaferManager.Instance.GetWafer(ModuleName.Loader1, 0);
                    waferHolderInfo.WaferAId = loaderWaferInfo.WaferID;
                    waferHolderInfo.WaferAType = (int)loaderWaferInfo.WaferType;
                    WaferHolderDataRecorder.UpdateWaferHolderData(waferHolderInfo.Id, waferHolderInfo);
                    MaterialTrackerManager.Instance.UpdateModuleMaterial(ModuleName.Loader1.ToString());
                }
                else
                {
                    WaferManager.Instance.WaferMoved(ModuleHelper.Converter(Module), 1, ModuleName.Loader1, 1);
                    WaferInfo loaderWaferInfo = WaferManager.Instance.GetWafer(ModuleName.Loader1, 1);
                    waferHolderInfo.WaferBId = loaderWaferInfo.WaferID;
                    waferHolderInfo.WaferBType = (int)loaderWaferInfo.WaferType;
                    WaferHolderDataRecorder.UpdateWaferHolderData(waferHolderInfo.Id, waferHolderInfo);
                    MaterialTrackerManager.Instance.UpdateModuleMaterial(ModuleName.Loader1.ToString());
                }
            }
            return true;
        }
        /// 
        /// 检验Rotation移动状态
        /// 
        /// 
        private bool CheckRotationPositionStatus()
        {
            return _rotationAxis.Status == RState.End;
        }
        /// 
        /// 检验Rotation是否还在运动
        /// 
        /// 
        private bool CheckRotationPositionRunStop()
        {
            bool result= _rotationAxis.Status == RState.Failed;
            if (result)
            {
                NotifyError(eEvent.ERR_PUF, "rotation axis goto home failed", 0);
            }
            return result;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _pufSide = objs[0].ToString();
            _loaderEntity = Singleton.Instance.GetModule(ModuleName.Loader1.ToString());
            _flipAxis = DEVICE.GetDevice($"{Module}.Flip");
            _rotationAxis = DEVICE.GetDevice($"{Module}.Rotation");
            _vacuum = DEVICE.GetDevice($"{Module}.Vacuum");
            _gantryAxis = DEVICE.GetDevice($"{ModuleName.Transporter2}.Gantry");
            if (!CheckPreCondition())
            {
                return RState.Failed;
            }
            GetCrsAxis();
            _onlyPlaceToLoaderRoutine = new PufOnlyPlaceToLoaderRoutine(Module.ToString());
            return Runner.Start(Module, "Place To Loader");
        }
        /// 
        /// 检验是否存在Wafer
        /// 
        /// 
        private bool CheckWaferPresent()
        {
            if (_pufSide == "SideA")
            {
                if (!_vacuum.ChuckAWaferPresent)
                {
                    NotifyError(eEvent.ERR_PUF, $"{_pufSide} has no Wafer", -1);
                    return false;
                }
            }
            else
            {
                if (!_vacuum.ChuckBWaferPresent)
                {
                    NotifyError(eEvent.ERR_PUF, $"{_pufSide} has no Wafer",-1);
                    return false;
                }
            }
            return true;
        }
        /// 
        /// 检验前置条件
        /// 
        /// 
        private bool CheckPreCondition()
        {
            if(!CheckWaferPresent())
            {
                return false;
            }
            bool isLoaderInstall = ModuleHelper.IsInstalled(ModuleName.Loader1);
            if (isLoaderInstall)
            {
                JetAxisBase loaderRotationaxis = DEVICE.GetDevice($"{ModuleName.Loader1}.Rotation");
               
                if (loaderRotationaxis != null)
                {
                    double loaderRotationPosition = loaderRotationaxis.MotionData.MotorPosition;
                    if (!loaderRotationaxis.CheckPositionInStationIgnoreWaferSize(loaderRotationPosition, "LOAD"))
                    {
                        NotifyError(eEvent.ERR_PUF, $"Loader Rotation {loaderRotationPosition} is not in LOAD", -1);
                        return false;
                    }
                    bool isLoadA = loaderRotationaxis.CheckPositionInStationIgnoreWaferSize(loaderRotationPosition, "LOADA");
                    bool isLoadB = loaderRotationaxis.CheckPositionInStationIgnoreWaferSize(loaderRotationPosition, "LOADB");
                    _loaderSide = isLoadA ? SIDE_A : SIDE_B;
                } 
                string side = _loaderSide == SIDE_A ? "A" : "B";
                GetLoaderSide();
                if (_loaderSideDevice.SideData.WaferPresent)
                {
                    NotifyError(eEvent.ERR_PUF, $"{_loaderSideDevice.Module}.{_loaderSideDevice.Name} wafer present sensor is true", -1);
                    return false;
                }
                //Loader1.SwingA 在Open
                JetAxisBase loaderShuttleAAxis = DEVICE.GetDevice($"{ModuleName.Loader1}.Shuttle{side}");
                if (loaderShuttleAAxis != null)
                {
                    double loaderShuttleAPosition = loaderShuttleAAxis.MotionData.MotorPosition;
                    if (!loaderShuttleAAxis.CheckPositionInStationIgnoreWaferSize(loaderShuttleAPosition, "OUT"))
                    {
                        NotifyError(eEvent.ERR_PUF, $"Loader Shuttle{side} {loaderShuttleAPosition} is not in OUT", -1);
                        return false;
                    }
                }
                //Loader1.TiltA 在HORI
                JetAxisBase loaderTiltAAxis = DEVICE.GetDevice($"{ModuleName.Loader1}.Tilt{side}");
                if (loaderTiltAAxis != null)
                {
                    double loaderTiltAPosition = loaderTiltAAxis.MotionData.MotorPosition;
                    if (!loaderTiltAAxis.CheckPositionIsInStation(loaderTiltAPosition, "HORI"))
                    {
                        NotifyError(eEvent.ERR_PUF, $"Loader Tilt{side} {loaderTiltAPosition} is not in HORI", -1);
                        return false;
                    }
                }
                //Loader Handle Wafer状态确认
                // Lip Seal Vacuum "ON"
                if (!_loaderSideDevice.SideData.CRSVacuum)
                {
                    NotifyError(eEvent.ERR_PUF, "Loader1 LS Vacuum is off", -1);
                    return false;
                }
                //Bernoulli Bladder "ON",Retracted Green Light
                if (!_loaderSideDevice.SideData.BernoulliBladder)
                {
                    NotifyError(eEvent.ERR_PUF, "Loader1 Bernoulli Bladder is off",-1);
                    return false;
                }
                //其他SideA/B均为OFF
                if (_loaderSideDevice.SideData.BernoulliN2)
                {
                    NotifyError(eEvent.ERR_PUF, "Loader1 Bernoulli N2 is on",-1);
                    return false;
                }
            }
            double rotationPosition = _rotationAxis.MotionData.MotorPosition;
            if (_rotationAxis.CheckPositionIsEmpty(rotationPosition))
            {
                LOG.WriteLog(eEvent.ERR_PUF, Module, $"rotation axis {rotationPosition} is not at Station");
                return false;
            }
            double flipPosition = _flipAxis.MotionData.MotorPosition;
            if (_flipAxis.CheckPositionIsEmpty(flipPosition))
            {
                LOG.WriteLog(eEvent.ERR_PUF, Module, $"flip axis {flipPosition} is not at Station");
                return false;
            }
            return true;
        }
        /// 
        /// 获取LoaderSide
        /// 
        private void GetLoaderSide()
        {
            if (_loaderSide==SIDE_A)
            {
                _loaderSideDevice = DEVICE.GetDevice($"{ModuleName.Loader1}.SideA");
            }
            else
            {
                _loaderSideDevice = DEVICE.GetDevice($"{ModuleName.Loader1}.SideB");
            }
        }
        /// 
        /// 获取lipseal Axis
        /// 
        private void GetCrsAxis()
        {
            if(_loaderSide==SIDE_A)
            {
                _loaderCrsAxis = DEVICE.GetDevice($"{ModuleName.Loader1}.LSA");
            }
            else
            {
                _loaderCrsAxis = DEVICE.GetDevice($"{ModuleName.Loader1}.LSB");
            }
        }
    }
}