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 = 'amulett'

tasksOrchestrator = TasksOrchestrator()

# TODO: add unit tests
def _druecke(context: Context, hotkey) -> None:
    """Taste druecken - aber nur, solange der Tausch etwas bewirkt.

    Gleiche Bremse wie beim Ring: Ohne Amulett bliebe die Bedingung fuer
    immer erfuellt und die Taste wuerde endlos gedrueckt (siehe
    swapGuard).
    """
    if not darfDruecken(BEREICH, hotkey):
        return
    merkeVersuch(BEREICH, hotkey)
    tasksOrchestrator.setRootTask(
        context, UseHotkeyTask(hotkey, delayAfterComplete=2))


def swapAmulet(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']['swapAmulet']['enabled'] == False:
        return
    currentTaskName = context['ng_tasksOrchestrator'].getCurrentTaskName(context)
    if currentTaskName in ['depositGold', 'refill', 'buyBackpack', 'selectChatTab', 'travel']:
        return
    tankAmuletSlotIsEquipped = slotIsEquipped(context['ng_screenshot'], context['healing']['highPriority']['swapAmulet']['tankAmulet']['slot'])
    tankAmuletSlotIsAvailable = slotIsAvailable(context['ng_screenshot'], context['healing']['highPriority']['swapAmulet']['tankAmulet']['slot'])
    # Hat ein Tausch gegriffen, darf wieder von vorn gezaehlt werden.
    if tankAmuletSlotIsEquipped:
        zuruecksetzen(BEREICH)
    if context['ng_statusBar']['hpPercentage'] <= context['healing']['highPriority']['swapAmulet']['tankAmulet']['hpPercentageLessThanOrEqual']:
        if not tankAmuletSlotIsEquipped and tankAmuletSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapAmulet']['tankAmulet']['hotkey'])
        return
    mainAmuletSlotIsEquipped = slotIsEquipped(context['ng_screenshot'], context['healing']['highPriority']['swapAmulet']['mainAmulet']['slot'])
    mainAmuletSlotIsAvailable = slotIsAvailable(context['ng_screenshot'], context['healing']['highPriority']['swapAmulet']['mainAmulet']['slot'])
    if context['ng_statusBar']['hpPercentage'] > context['healing']['highPriority']['swapAmulet']['mainAmulet']['hpPercentageGreaterThan']:
        if not mainAmuletSlotIsEquipped and mainAmuletSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapAmulet']['mainAmulet']['hotkey'])
        return
    if context['healing']['highPriority']['swapAmulet']['tankAmuletAlwaysEquipped']:
        if not tankAmuletSlotIsEquipped and tankAmuletSlotIsAvailable:
            _druecke(context, context['healing']['highPriority']['swapAmulet']['tankAmulet']['hotkey'])
        return
    if tankAmuletSlotIsEquipped:
        _druecke(context, context['healing']['highPriority']['swapAmulet']['tankAmulet']['hotkey'])
        return
    if mainAmuletSlotIsEquipped:
        _druecke(context, context['healing']['highPriority']['swapAmulet']['mainAmulet']['hotkey'])
