from .olives_abstractcontrollableobject import AbstractControllableObject
from olives_objectcontrollable import ObjectControllable
from olives_object import ObjectCategory
from olives_job import Job
from olives_gamestate import GameState


def set_status(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:
        # navigate to the context object and set the appropriate animation when done (successfully)
        def success_callback() -> bool:
            context_object = game_state.get_object(job.get_context().get_object_id())
            is_far = int(context_object.get_category()) & int(ObjectCategory.WALL_MOUNTED_SLIM)
            animation = "change_settings_far" if is_far else "change_settings"
            game_state.set_animation(self.object_id, animation, False)
            return True
        self.handle_context_transition_step(job, game_state, obj, success_callback)

    elif current_step == 1:
        # reset if necessary, as well as reduce the funds for this action
        self.reset_step_progress(job, game_state, job.get_step().get_price())
        # set the object to enabled/disabled if this is the first run
        if job.get_step().get_ticks_elapsed() == 0:
            new_value = not (job.get_context().get_parameter() == '1')
            object_id = job.get_context().get_object_id()
            game_state.set_object_active_state(object_id, new_value)

        # update the elapsed ticks
        self.update_elapsed_ticks(job, elapsed_ticks)

        # and see if we have finished the step already. if so, finish the job
        if self.check_for_completion(job):
            game_state.set_animation(self.object_id, "idle", True)

