from src.gameplay.core.tasks.orchestrator import TasksOrchestrator
from src.gameplay.core.tasks.useHotkey import UseHotkeyTask
from src.gameplay.reaction import ReactionGate
from src.repositories.actionBar.core import slotIsAvailable, slotIsEquipped
from src.repositories.skills.core import getFood
from ...typings import Context


tasksOrchestrator = TasksOrchestrator()
foodGate = ReactionGate(mean=0.6, sigma=0.25, lo=0.3, hi=1.5)


# TODO: add unit tests
def eatFood(context: Context):
    currentTask = tasksOrchestrator.getCurrentTask(context)
    if currentTask is not None:
        if currentTask.status == 'completed':
            tasksOrchestrator.reset()
        else:
            tasksOrchestrator.do(context)
            return
    if not context['healing']['eatFood']['enabled']:
        return
    food = getFood(context['ng_screenshot'])
    if foodGate.ready(food <= context['healing']['eatFood']['eatWhenFoodIslessOrEqual']):
        tasksOrchestrator.setRootTask(
            context, UseHotkeyTask(context['healing']['eatFood']['hotkey'], delayAfterComplete=2))
