from src.gameplay.core.tasks.orchestrator import TasksOrchestrator
from src.gameplay.reaction import ReactionGate
from ...typings import Context
from ..utils.potions import healthPotionTakesPriority, matchManaHealing
from .healingByPotions import usePotionTask


tasksOrchestrator = TasksOrchestrator()
manaGate = ReactionGate()


# TODO: add unit tests
def healingByMana(context: Context):
    if healthPotionTakesPriority(context):
        return
    currentTask = tasksOrchestrator.getCurrentTask(context)
    if currentTask is not None:
        if currentTask.status == 'completed':
            tasksOrchestrator.reset()
        else:
            tasksOrchestrator.do(context)
            return
    potion = context['healing']['potions']['firstManaPotion']
    if potion['enabled']:
        urgent = (context['ng_statusBar'].get('hpPercentage') or 100) < 50
        if manaGate.ready(matchManaHealing(potion, context['ng_statusBar']), urgent=urgent):
            tasksOrchestrator.setRootTask(context, usePotionTask(
                potion, context.get('ng_actionBar', {}).get('smartHotkeys', False)))
            return
