using Aitex.Core.Util;
using Aitex.Sorter.Common;
using SicRT.Modules.Schedulers;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.SubstrateTrackings;
using SicModules.EFEMs;
using SicRT.Equipments.Systems;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.HineAutomation;

namespace SicRT.Scheduler
{
	public class SchedulerTrayRobot : SchedulerModule
	{
		public override bool IsAvailable
		{
			get { return _efem.IsIdle && _efem.IsOnline && _efem.TrayRobotModule.IsIdle && _efem.TrayRobotModule.IsOnline && CheckTaskDone(); }
		}

		public override bool IsOnline
		{
			get { return _efem.IsOnline && _efem.TrayRobotModule.IsOnline; }

		}
		public override bool IsError
		{
			get { return _efem.IsError || _efem.TrayRobotModule.IsError; }

		}

		private EFEMModule _efem = null;

		private Hand _taskHand;
		public SchedulerTrayRobot() : base(ModuleName.TrayRobot.ToString())
		{
			_efem = Singleton<EquipmentManager>.Instance.Modules[ModuleName.EFEM] as EFEMModule;
		}


		public bool IsReadyForPick(Hand blade)
		{
			return WaferManager.Instance.CheckNoWafer(ModuleName.TrayRobot, (int)blade);
		}

		public bool IsReadyForPlace(Hand blade)
		{
			return WaferManager.Instance.CheckHasWafer(ModuleName.TrayRobot, (int)blade);
		}


		public bool Pick(ModuleName target, int slot, Hand hand)
		{
			if (_efem.PickTray(target, hand, slot))
			{
				_task = TaskType.Pick;
				_taskHand = hand;
				LogTaskStart(_task, $"{target}.{slot + 1}=>{Module}.{hand}");
            }

            return true;
        }

		public bool Place(ModuleName target, int slot, Hand hand)
		{
			if (_efem.PlaceTray(target, hand, slot))
			{
				_task = TaskType.Place;
				_taskHand = hand;
				LogTaskStart(_task, $"{Module}.{hand}=>{target}.{slot + 1}");
            }

            return true;
        }

		public bool Goto(ModuleName chamber, int slot, Hand hand)
		{
			return true;
		}

		public bool Monitor()
		{
			return true;
		}

		public bool CheckTaskDone()
		{
			var taskSucceed = false;
			switch (_task)
			{
				case TaskType.None:
                    taskSucceed = true;
					break;
				case TaskType.Pick:
					taskSucceed = WaferManager.Instance.CheckHasTray(ModuleName.TrayRobot, (int)_taskHand);
					break;
				case TaskType.Place:
                    taskSucceed = WaferManager.Instance.CheckNoTray(ModuleName.TrayRobot, (int)_taskHand);
                    break;
            }
            return SuperCheckTaskDone(taskSucceed, _efem.TrayRobotModule.IsIdle | _efem.TrayRobotModule.IsError);
        }
	}


}
