from src.gameplay.core.tasks.orchestrator import TasksOrchestrator
from src.gameplay.core.tasks.useHotkey import UseHotkeyTask
from src.repositories.actionBar.core import slotIsAvailable, slotIsEquipped
from ...typings import Context
from .swapGuard import darfDruecken, merkeVersuch, zuruecksetzen

BEREICH = 'ring'

tasksOrchestrator = TasksOrchestrator()

def _druecke(context: Context, hotkey) -> None:
    """Taste druecken - aber nur, solange der Tausch noch etwas bewirkt.

    Ohne diese Bremse wird die Taste endlos gedrueckt, wenn der Ring gar
    nicht vorhanden ist (siehe swapGuard).
    """
    if not darfDruecken(BEREICH, hotkey):
        return
    merkeVersuch(BEREICH, hotkey)
    tasksOrchestrator.setRootTask(
        context, UseHotkeyTask(hotkey, delayAfterComplete=2))


# TODO: add unit tests
def swapRing(context: Context):
    currentTask = tasksOrchestrator.getCurrentTask(context)
    if currentTask is not None:
        if currentTask.status == 'completed':
            tasksOrchestrator.reset()
        else:
            tasksOrchestrator.do(context)
            return
    if context['healing']['highPriority']['swapRing']['enabled'] == False:
        return
    currentTaskName = context['ng_tasksOrchestrator'].getCurrentTaskName(context)
    if currentTaskName in ['depositGold', 'refill', 'buyBackpack', 'selectChatTab', 'travel']:
        return
    tankRingSlotIsEquipped = slotIsEquipped(context['ng_screenshot'], context['healing']['highPriority']['swapRing']['tankRing']['slot'])
    tankRingSlotIsAvailable = slotIsAvailable(context['ng_screenshot'], context['healing']['highPriority']['swapRing']['tankRing']['slot'])
    # Hat ein Tausch gegriffen, darf wieder von vorn gezaehlt werden.
    if tankRingSlotIsEquipped:
        zuruecksetzen(BEREICH)
    if context['ng_statusBar']['hpPercentage'] <= context['healing']['highPriority']['swapRing']['tankRing']['hpPercentageLessThanOrEqual']:
        if not tankRingSlotIsEquipped and tankRingSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapRing']['tankRing']['hotkey'])
        return
    mainRingSlotIsEquipped = slotIsEquipped(context['ng_screenshot'], context['healing']['highPriority']['swapRing']['mainRing']['slot'])
    mainRingSlotIsAvailable = slotIsAvailable(context['ng_screenshot'], context['healing']['highPriority']['swapRing']['mainRing']['slot'])
    if context['ng_statusBar']['hpPercentage'] > context['healing']['highPriority']['swapRing']['mainRing']['hpPercentageGreaterThan']:
        if not mainRingSlotIsEquipped and mainRingSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapRing']['mainRing']['hotkey'])
        return
    if context['healing']['highPriority']['swapRing']['tankRingAlwaysEquipped']:
        if not tankRingSlotIsEquipped and tankRingSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapRing']['tankRing']['hotkey'])
        return
    if tankRingSlotIsEquipped:
        _druecke(context, context['healing']['highPriority']['swapRing']['tankRing']['hotkey'])
        return
    if mainRingSlotIsEquipped:
        _druecke(context, context['healing']['highPriority']['swapRing']['mainRing']['hotkey'])
