# Path: intelligine/synergy/event/move/MoveAction.py
# class MoveAction(Action):
# 
#     _listen = MoveEvent
# 
#     def __init__(self, object_id, parameters):
#         super().__init__(object_id, parameters)
#         self._move_to_point = None
#         self._move_to_direction = None
# 
#     def run(self, obj, context, synergy_manager):
#         if self._parameters.get(MoveEvent.PARAM_NEW_MODE, None):
#             obj.get_brain().switch_to_mode(self._parameters[MoveEvent.PARAM_NEW_MODE])
#             return
# 
#         try:
#             self._apply_move(obj, context)
#         except ActionAborted:
#             # TODO: Dans l'obj ces lignes
#             blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
#             context.metas.value.set(BLOCKED_SINCE, obj.get_id(), blocked_since+1)
# 
#     def _apply_move(self, obj, context):
#         # TODO: il ne faut pas choisir une direction 14.
#         if MoveEvent.PARAM_DIRECTION not in self._parameters or self._parameters[MoveEvent.PARAM_DIRECTION] == 14:
#             raise ActionAborted()
# 
#         obj.set_position(self._parameters[MoveEvent.PARAM_POSITION])
#         obj.set_previous_direction(self._parameters[MoveEvent.PARAM_DIRECTION])
#         obj.get_brain().get_part(BRAIN_PART_MOVE).done()
# 
# Path: intelligine/synergy/event/transport/TakeableEvent.py
# class TakeableEvent(NearEvent):
# 
#     _mechanism = AroundMechanism
#     PARAM_TAKE = 'take'
#     _concern = COL_TRANSPORTER_NOT_CARRYING
#     _near_name = 'objects_ids_transportable'
#     _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
# 
#     def _prepare(self, object_id, context, parameters={}):
#         if not self._can_carry(object_id, context):
#             raise NotConcernedEvent()
# 
#         try:
#             self.map(context, parameters, stop_at_first=True)
#         except NearNothingFound:
#             raise NotConcernedEvent()
# 
#         if not self._object_can_take(object_id, context, parameters[self._near_name][0]):
#             raise NotConcernedEvent()
# 
#         parameters[self.PARAM_TAKE] = parameters[self._near_name][0]
#         return parameters
# 
#     @staticmethod
#     def _can_carry(object_id, context):
#         return not context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True)
# 
#     @classmethod
#     def _object_can_take(cls, object_id, context, object_to_take_id):
#         object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_TAKE)
#         return object_take_brain_part.can_take(context, object_id, object_to_take_id)
# 
# Path: intelligine/cst.py
# CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
# 
# BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')


from intelligine.synergy.event.move.MoveAction import MoveAction
from synergine.core.Core import Core
from synergine.synergy.event.Action import Action
from intelligine.synergy.event.transport.TakeableEvent import TakeableEvent
from intelligine.cst import CANT_PUT_STILL, BRAIN_PART_TAKE
from synergine.synergy.event.exception.ActionAborted import ActionAborted


class TakeableAction(Action):

    _listen = TakeableEvent
    _depend = [MoveAction]

    def __init__(self, object_id, parameters):
        super().__init__(object_id, parameters)

    def run(self, obj, context, synergy_manager):

# Path: intelligine/synergy/event/move/MoveAction.py
# class MoveAction(Action):
# 
#     _listen = MoveEvent
# 
#     def __init__(self, object_id, parameters):
#         super().__init__(object_id, parameters)
#         self._move_to_point = None
#         self._move_to_direction = None
# 
#     def run(self, obj, context, synergy_manager):
#         if self._parameters.get(MoveEvent.PARAM_NEW_MODE, None):
#             obj.get_brain().switch_to_mode(self._parameters[MoveEvent.PARAM_NEW_MODE])
#             return
# 
#         try:
#             self._apply_move(obj, context)
#         except ActionAborted:
#             # TODO: Dans l'obj ces lignes
#             blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
#             context.metas.value.set(BLOCKED_SINCE, obj.get_id(), blocked_since+1)
# 
#     def _apply_move(self, obj, context):
#         # TODO: il ne faut pas choisir une direction 14.
#         if MoveEvent.PARAM_DIRECTION not in self._parameters or self._parameters[MoveEvent.PARAM_DIRECTION] == 14:
#             raise ActionAborted()
# 
#         obj.set_position(self._parameters[MoveEvent.PARAM_POSITION])
#         obj.set_previous_direction(self._parameters[MoveEvent.PARAM_DIRECTION])
#         obj.get_brain().get_part(BRAIN_PART_MOVE).done()
# 
# Path: intelligine/synergy/event/transport/TakeableEvent.py
# class TakeableEvent(NearEvent):
# 
#     _mechanism = AroundMechanism
#     PARAM_TAKE = 'take'
#     _concern = COL_TRANSPORTER_NOT_CARRYING
#     _near_name = 'objects_ids_transportable'
#     _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
# 
#     def _prepare(self, object_id, context, parameters={}):
#         if not self._can_carry(object_id, context):
#             raise NotConcernedEvent()
# 
#         try:
#             self.map(context, parameters, stop_at_first=True)
#         except NearNothingFound:
#             raise NotConcernedEvent()
# 
#         if not self._object_can_take(object_id, context, parameters[self._near_name][0]):
#             raise NotConcernedEvent()
# 
#         parameters[self.PARAM_TAKE] = parameters[self._near_name][0]
#         return parameters
# 
#     @staticmethod
#     def _can_carry(object_id, context):
#         return not context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True)
# 
#     @classmethod
#     def _object_can_take(cls, object_id, context, object_to_take_id):
#         object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_TAKE)
#         return object_take_brain_part.can_take(context, object_id, object_to_take_id)
# 
# Path: intelligine/cst.py
# CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
# 
# BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')


# Path: intelligine/synergy/event/transport/TakeableAction.py
from intelligine.synergy.event.move.MoveAction import MoveAction
from synergine.core.Core import Core
from synergine.synergy.event.Action import Action
from intelligine.synergy.event.transport.TakeableEvent import TakeableEvent
from intelligine.cst import CANT_PUT_STILL, BRAIN_PART_TAKE
from synergine.synergy.event.exception.ActionAborted import ActionAborted



class TakeableAction(Action):

    _listen = TakeableEvent
    _depend = [MoveAction]

    def __init__(self, object_id, parameters):
        super().__init__(object_id, parameters)

    def run(self, obj, context, synergy_manager):

        obj_id_transportable = self._parameters[TakeableEvent.PARAM_TAKE]