from src.gameplay.targeting import (
    getAttackableCreatures, ignoreCreatureTemporarily,
)


def _context(monsterNames):
    return {
        'gameWindow': {'monsters': [{'name': name} for name in monsterNames]},
        'ng_targeting': {
            'canIgnoreCreatures': False,
            'creatures': {},
        },
        'ignorable_creatures': [],
    }


def test_temp_ignored_creature_is_excluded():
    context = _context(['wasp', 'rotworm'])
    ignoreCreatureTemporarily(context, 'wasp')

    attackable = getAttackableCreatures(context)

    assert [monster['name'] for monster in attackable] == ['rotworm']


def test_temp_ignore_expires(mocker):
    context = _context(['wasp'])
    timeMock = mocker.patch('src.gameplay.targeting.time')
    timeMock.return_value = 1000
    ignoreCreatureTemporarily(context, 'wasp', seconds=30)

    timeMock.return_value = 1031
    attackable = getAttackableCreatures(context)

    assert [monster['name'] for monster in attackable] == ['wasp']
    assert context['ng_targeting']['tempIgnored'] == {}


def test_temp_ignore_applies_with_target_settings():
    context = _context(['wasp', 'rotworm'])
    context['ng_targeting']['canIgnoreCreatures'] = True
    context['ng_targeting']['creatures'] = {
        'wasp': {'ignore': False, 'priority': 1},
        'rotworm': {'ignore': False, 'priority': 5},
    }
    ignoreCreatureTemporarily(context, 'rotworm')

    attackable = getAttackableCreatures(context)

    assert [monster['name'] for monster in attackable] == ['wasp']
