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.EFEM;

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

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

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

		}

		private EFEMModule _efem = null;

		private Hand _taskHand;

		private Hand _taskSwapPickHand;
		private Hand _taskSwapPlaceHand;

		public SchedulerWaferRobot() : base(ModuleName.WaferRobot.ToString())
		{
			_efem = Singleton<EquipmentManager>.Instance.Modules[ModuleName.EFEM] as EFEMModule;
		}


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

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


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

            return true;
        }

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

            return true;
        }

		public bool Map(ModuleName target)
		{
			if (_efem.MapWafer(target))
			{
				_task = TaskType.Map;
				LogTaskStart(_task, $"{Module}.WaferRobot Map");
            }

            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.CheckHasWafer(ModuleName.WaferRobot, (int)_taskHand);
					break;
				case TaskType.Place:
                    taskSucceed = WaferManager.Instance.CheckNoWafer(ModuleName.WaferRobot, (int)_taskHand);
                    break;
            }

            return SuperCheckTaskDone(taskSucceed, _efem.WaferRobotModule.IsIdle | _efem.WaferRobotModule.IsError);
        }
	}


}
