using Aitex.Core.Util;
using Aitex.Sorter.Common;
using SicRT.Modules.Schedulers;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.SubstrateTrackings;
using SicModules.TMs;
using SicRT.Equipments.Systems;

namespace SicRT.Scheduler
{
    public class SchedulerTMRobot : SchedulerModule
    {
        public override bool IsAvailable => _tmRobot.IsIdle && _tmRobot.IsOnline && CheckTaskDone();

        public override bool IsOnline => _tmRobot.IsOnline;

        public override bool IsError => _tmRobot.IsError;

        private TMModuleBase _tmRobot = null;

        private Hand _taskHand;

        private ModuleName _target;

        public bool IsInPumping => ((_task == TaskType.Pick || _task == TaskType.Place) &&
                                    (_target == ModuleName.UnLoad || _target == ModuleName.LoadLock ||
                                     _target == ModuleName.Load));

        public SchedulerTMRobot() : base(ModuleName.TMRobot.ToString())
        {
            _tmRobot = Singleton<EquipmentManager>.Instance.Modules[ModuleName.TM] as TMModuleBase;
        }


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

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


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

            return true;
        }

        public bool Place(ModuleName target, int slot, Hand hand)
        {
            if (_tmRobot.Place(target, hand, slot, out string reason))
            {
                _task = TaskType.Place;
                _taskHand = hand;
                _target = target;
                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.TMRobot, (int)_taskHand);
                    break;
                case TaskType.Place:
                    taskSucceed = WaferManager.Instance.CheckNoTray(ModuleName.TMRobot, (int)_taskHand);
                    break;
            }

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