using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.ToolLayout;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.AXIS;
using PunkHPX8_RT.Devices.Facilities;
using PunkHPX8_RT.Devices.VpwCell;
using PunkHPX8_RT.Devices.VpwMain;
using PunkHPX8_RT.Modules.VpwMain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Modules.VpwCelMain
{
    public class VpwSimpleHomeRoutine : RoutineBase, IRoutine
    {
        private enum VpwSimpleHomeStep
        {
            CheckPreCondition,
            ChamberUp,
            HomeAllRotation,
            CheckAllRotationHome,
            ChamberDown,
            End
        }
        #region 常量 
        #endregion
        #region 内部变量
        /// 
        /// Cell device集合
        /// 
        private List _vpwCellDevices = new List();
        /// 
        /// Main Device
        /// 
        private VpwMainDevice _mainDevice;
        /// 
        /// 用于处理没有被dissable的vpwcell
        /// 
        private List _cellLst = new List();
        /// 
        /// vpw cell rotation列表
        /// 
        private List _rotationAxis = new List();
        /// 
        /// 检验chamber是否上升/下降到位
        /// 
        private int _checkChamberUpDownTimes = 3;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public VpwSimpleHomeRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Vpw Simple Home abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(VpwSimpleHomeStep.CheckPreCondition, CheckPreCondition, _delay_1ms)
                .Run(VpwSimpleHomeStep.ChamberUp, _mainDevice.ChamberUp, CheckChamberClosed, _checkChamberUpDownTimes*1000)
                .Run(VpwSimpleHomeStep.HomeAllRotation, HomeAllRotation,_delay_1s)
                .WaitWithStopCondition(VpwSimpleHomeStep.CheckAllRotationHome, CheckAllRotationHomed, CheckRotationHomeFailed)
                .Run(VpwSimpleHomeStep.ChamberDown, _mainDevice.ChamberDown, CheckChamberOpened, _checkChamberUpDownTimes*1000)
                .End(VpwSimpleHomeStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        
        private bool CheckPreCondition()
        {
            //SystemFacilities systemFacility = DEVICE.GetDevice("System.Facilities");
            //if (systemFacility == null)
            //{
            //    LOG.WriteLog(eEvent.ERR_VPW, Module, "Facility is null");
            //    return false;
            //}
            return true;
        }
        private bool HomeAllRotation()
        {
            bool result = true;
            foreach (var axis in _rotationAxis)
            {
                result &= axis.Home();
                if (result == false)
                {
                    return result;
                }
            }
            return result;
        }
        
        private bool CheckAllRotationHomed()
        {
            bool result = true;
            foreach (var axis in _rotationAxis)
            {
                bool condition = axis.IsHomed && axis.Status == RState.End;
                result &= condition;
            }
            return result;
        }
        private bool CheckRotationHomeFailed()
        {
            bool result = false;
            foreach (var axis in _rotationAxis)
            {
                bool condition = axis.Status == RState.Failed;
                result |= condition;
            }
            return result;
        }
        /// 
        /// 检验Chamber关闭
        /// 
        /// 
        private bool CheckChamberClosed()
        {
            return !_mainDevice.CommonData.ChamberClosed && _mainDevice.CommonData.ChamberOpened;
        }
        /// 
        /// 检验Chamber打开
        /// 
        /// 
        private bool CheckChamberOpened()
        {
            return _mainDevice.CommonData.ChamberClosed && !_mainDevice.CommonData.ChamberOpened;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _rotationAxis.Clear();
            _vpwCellDevices.Clear();
            _cellLst.Clear();
            VpwMainItem vpwMainItem = VpwMainItemManager.Instance.GetItem(Module.ToString());
            if (vpwMainItem == null || vpwMainItem.VpwCells == null)
            {
                return RState.Failed;
            }
            foreach (var item in vpwMainItem.VpwCells)
            {
                VpwCellDevice cellDevice = DEVICE.GetDevice(item.ModuleName);
                _vpwCellDevices.Add(cellDevice);
            }
            
            
            foreach (var device in _vpwCellDevices)
            {
                VpwCellEntity vpwCellEntity = Singleton.Instance.GetModule(device.Module);
                if (vpwCellEntity.IsAuto || vpwCellEntity.IsManual)
                {
                    _cellLst.Add(device);
                    
                    JetAxisBase axis = DEVICE.GetDevice($"{device.Module}.Rotation");
                    _rotationAxis.Add(axis);
                }
            }
            
            _mainDevice = DEVICE.GetDevice(Module.ToString());
            _checkChamberUpDownTimes = SC.GetValue("VPWMain.ChamberUpDownCheckTime");
            return Runner.Start(Module, "VPW Simple Home");
        }
    }
}