from .olives_abstractcontrollableobject import AbstractControllableObject
from olives_object import SlotType, PositionStatus, ContainedStatus, ContainedStatusChange
from olives_objectcontrollable import ObjectControllable
from olives_job import Job
from olives_gamestate import GameState


def sleep(self: AbstractControllableObject, job: Job, game_state: GameState, elapsed_ticks: int,
          obj: ObjectControllable,
          parameter: str):
    current_step = job.get_step().get_current_step()
    if current_step == 0:
        # if we can transition to an active status without moving no further is necessary in step 0
        if self.transition_to_active_status(job, obj, PositionStatus.LYING):
            return

        # navigate to the context object and set the appropriate animation when done (successfully)
        self.handle_context_transition_step(job, game_state, obj, None, 'action', SlotType.JOB)

    elif current_step == 1:
        # nothing to do but to wait for the change to finish
        if obj.get_contained_status_change() == ContainedStatusChange.NONE:
            game_state.set_animation(self.object_id, 'lie', True)
            job.increment_step(1)

    elif current_step == 2:
        # the regeneration rate per tick
        value = 0.003
        # update the elapsed ticks
        self.update_elapsed_ticks(job, elapsed_ticks)
        # get the progress value to add
        obj.set_energy(obj.get_energy() + self.get_relative_progress_unlimited(job, value))
        # and see if we have finished the step already. if so, finish the job
        if obj.get_energy() == 100:
            job.increment_step(1)
