import numpy as np

from src.gameplay.core.tasks import takeSupplies as modul
from src.gameplay.core.tasks.takeSupplies import TakeSuppliesTask


BAR = np.zeros((12, 57), dtype=np.uint8)
TRUHE = np.zeros((21, 32), dtype=np.uint8)


def _task():
    return TakeSuppliesTask(BAR, TRUHE)


def _context():
    return {'ng_screenshot': np.zeros((1080, 1920), dtype=np.uint8)}


def test_ends_without_backpack_or_chest(mocker):
    mocker.patch.object(modul, 'locate', return_value=None)
    task = _task()

    task.do(_context())

    assert task.terminable is True


def test_ends_when_nothing_is_missing(mocker):
    """Sind Seil und Schaufel da, gibt es nichts zu holen."""
    task = _task()
    mocker.patch.object(modul, 'locate', return_value=(10, 20, 32, 21))
    mocker.patch.object(task, '_fehlt', return_value=[])
    zieh = mocker.patch.object(modul, 'drag')

    task.do(_context())

    assert task.terminable is True
    zieh.assert_not_called()


def test_missing_item_is_dragged_back(mocker):
    """Fehlt die Schaufel und liegt in der Truhe, wird sie zurueckgeholt."""
    task = _task()
    mocker.patch.object(modul, 'locate', side_effect=[
        (100, 200, 57, 12),   # Rucksack-Leiste
        (700, 800, 32, 21),   # Truhe
    ])
    mocker.patch.object(task, '_fehlt', return_value=['shovel'])
    mocker.patch.object(task, '_inTruhe', return_value=(716, 810))
    zieh = mocker.patch.object(modul, 'drag')
    mocker.patch.object(modul, 'pause')

    task.do(_context())

    assert task.terminable is False
    zieh.assert_called_once_with((716, 810), (100 + 10 + 16, 200 + 18 + 10))


def test_missing_item_not_in_chest_ends_task(mocker):
    """Liegt es auch nicht in der Truhe, ist hier nichts zu holen."""
    task = _task()
    mocker.patch.object(modul, 'locate', side_effect=[
        (100, 200, 57, 12), (700, 800, 32, 21),
    ])
    mocker.patch.object(task, '_fehlt', return_value=['rope'])
    mocker.patch.object(task, '_inTruhe', return_value=None)
    zieh = mocker.patch.object(modul, 'drag')

    task.do(_context())

    assert task.terminable is True
    zieh.assert_not_called()


def test_required_items_have_templates():
    """Ohne Vorlage koennte nie erkannt werden, dass etwas fehlt."""
    from src.repositories.inventory.core import images

    for name in modul.BENOETIGT:
        assert name in images['slots'], name
