from src.gameplay.core.tasks.castSpell import CastSpellTask
from src.gameplay.core.tasks.useHotkey import UseHotkeyTask
from src.gameplay.core.tasks.useSlotItem import UseSlotItemTask
from src.gameplay.healing.observers.healingBySpells import _castTask


def _context(smart):
    return {'ng_actionBar': {'smartHotkeys': smart}}


def test_smart_mode_prefers_slot_click():
    rule = {'slot': 4, 'hotkey': '5'}

    task = _castTask(_context(True), rule, 'exura vita', UseHotkeyTask)

    assert isinstance(task, UseSlotItemTask)
    assert task.slot == 4


def test_smart_mode_casts_via_chat_without_slot():
    rule = {'slot': None, 'hotkey': '5'}

    task = _castTask(_context(True), rule, 'exura vita', UseHotkeyTask)

    assert isinstance(task, CastSpellTask)
    assert task.spell == 'exura vita'


def test_classic_mode_uses_hotkey():
    rule = {'slot': 4, 'hotkey': '5'}

    task = _castTask(_context(False), rule, 'exura vita', UseHotkeyTask)

    assert isinstance(task, UseHotkeyTask)
    assert task.hotkey == '5'


from src.gameplay.healing.observers.healingBySpells import _hpMatches


def test_hp_matches_percentage():
    rule = {'hpPercentageLessThanOrEqual': 60, 'hpLessThanOrEqual': None}
    assert _hpMatches(rule, {'hpPercentage': 55, 'hp': 800}) is True
    assert _hpMatches(rule, {'hpPercentage': 75, 'hp': 800}) is False


def test_hp_matches_absolute():
    rule = {'hpPercentageLessThanOrEqual': None, 'hpLessThanOrEqual': 300}
    assert _hpMatches(rule, {'hpPercentage': 90, 'hp': 250}) is True
    assert _hpMatches(rule, {'hpPercentage': 10, 'hp': 450}) is False
    assert _hpMatches(rule, {'hpPercentage': 10, 'hp': None}) is False


from src.gameplay.healing.observers.healingBySpells import _upkeepHpMatches


def test_upkeep_without_threshold_always_matches():
    rule = {'hpPercentageLessThanOrEqual': None, 'hpLessThanOrEqual': None}
    assert _upkeepHpMatches(rule, {'hpPercentage': 95, 'hp': 2000}) is True
    rule_zero = {'hpPercentageLessThanOrEqual': 0}
    assert _upkeepHpMatches(rule_zero, {'hpPercentage': 95}) is True


def test_upkeep_with_threshold():
    rule = {'hpPercentageLessThanOrEqual': 75}
    assert _upkeepHpMatches(rule, {'hpPercentage': 60}) is True
    assert _upkeepHpMatches(rule, {'hpPercentage': 90}) is False
    rule_abs = {'hpLessThanOrEqual': 1300}
    assert _upkeepHpMatches(rule_abs, {'hp': 1200}) is True
    assert _upkeepHpMatches(rule_abs, {'hp': 1500}) is False
