from components import Usable, Brain
from components.enums import Intention
from effects import get_effect
from systems.utilities import retract_intention, retract_turn


def run(scene):
    for brain in [b for b in scene.cm.get(Brain) if b.intention is Intention.USE_ITEM]:
        user = brain.entity
        usee = brain.intention_target
        usable = scene.cm.get_one(Usable, entity=usee)
        if usable:
            effect = get_effect(usable.on_use)
            if effect:
                effect(scene, user, usee)
                scene.cm.delete(entity=usee)
            retract_turn(scene, user)
        else:
            scene.message("You can't use that.")
        retract_intention(scene, user)
