using Aitex.Core.RT.Fsm;
using Aitex.Core.Util;
using SicRT.Scheduler;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.SubstrateTrackings;
using SicRT.Equipments;
using SicRT.Equipments.Systems;
using Aitex.Core.RT.Device;
using SicModules.Aligners;

namespace SicRT.Modules.Schedulers
{
    public class SchedulerAligner : SchedulerModule
    {
        public override bool IsAvailable
        {
            get { return _aliger.IsIdle && _aliger.IsOnline && CheckTaskDone(); }

        }
        public override bool IsOnline
        {
            get { return _aliger.IsOnline; }

        }
        public override bool IsError
        {
            get { return _aliger.IsError; }

        }

        private AlignerModuleBase _aliger = null;

        private int _entityTaskToken = (int)FSM_MSG.NONE;

        public SchedulerAligner(ModuleName module) : base(module.ToString())
        {
            _module = module.ToString();
            _aliger = Singleton<EquipmentManager>.Instance.Modules[module] as AlignerModule;
        }

        public bool Monitor()
        {
            return true;
        }

        public override bool Aligning()
        {
            _task = TaskType.Align;
            _entityTaskToken = _aliger.InvokeAligner();
            LogTaskStart(_task, $"{Module} Aligning");

            return _entityTaskToken != (int)FSM_MSG.NONE;
        }

        public bool CheckTaskDone()
        {
            var taskSucceed = false;
            switch (_task)
            {
                case TaskType.None:
                    taskSucceed = true;
                    break;
                case TaskType.Align:
                    taskSucceed = _aliger.CheckAcked(_entityTaskToken);
                    break;
            }

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

        public override bool IsReadyForPick(ModuleName robot, int slot)
        {
            return true;
        }

        public override bool IsReadyForPlace(ModuleName robot, int slot)
        {
            return true;
        }
    }
}