from components import Getable, Coordinates, Brain
from components.enums import Intention
from engine.constants import PRIORITY_LOW
from systems.utilities import retract_turn, retract_intention


def run(scene):
    for brain in [b for b in scene.cm.get(Brain) if b.intention is Intention.DROP_ITEM]:
        entity = brain.entity
        target = brain.intention_target
        drop(scene, entity, target)


def drop(scene, dropper: int, droppee: int):
    dropper_coords = scene.cm.get_one(Coordinates, entity=dropper)
    getable = scene.cm.get_one(Getable, entity=droppee)
    
    if getable.held_by == dropper:
        getable.held_by = None

        droppee_coords = Coordinates(
            entity=droppee,
            x=dropper_coords.x,
            y=dropper_coords.y,
            priority=PRIORITY_LOW
        )
        scene.cm.add(droppee_coords)
        retract_intention(scene, dropper)
        retract_turn(scene, dropper)
    else:
        print(f"{dropper} can't drop that")
