using Aitex.Core.RT.Fsm;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using SicRT.Equipments.Systems;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Device.Devices;
using SicModules.Buffers;

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

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

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

        }

        private BufferModuleBase _buffer = null;

        private int _entityTaskToken = (int)FSM_MSG.NONE;

        public SchedulerBuffer(ModuleName module) : base(module.ToString())
        {
            _module = module.ToString();
            _buffer = Singleton<EquipmentManager>.Instance.Modules[module] as BufferModuleBase;
        }

        internal float GetTemperature()
        {
            var deviceBufferTemp = DEVICE.GetDevice<IoTempMeter>($"Buffer.BufferTemp");
            return deviceBufferTemp.FeedBack;
        }

        public override bool Cooling(bool coolingTypeIsTime,int coolingValue)
        {
            _task = TaskType.Cooling;

            _entityTaskToken = _buffer.InvokeCooling(coolingTypeIsTime, coolingValue);

            if (coolingTypeIsTime)
            {
                LogTaskStart(_task, $"{Module} cooling {coolingValue} seconds");
            }
            else 
            {
                LogTaskStart(_task, $"{Module} cooling wait temprature below {coolingValue}℃");
            }

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

        public bool PreHeat(int preTemp)
        {
            _task = TaskType.WarmUp;
            LogTaskStart(_task, $"{Module} warm up to {preTemp} ℃");
            _entityTaskToken = _buffer.InvokeWarmUp(preTemp);
            return _entityTaskToken != (int)FSM_MSG.NONE;
        }

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

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