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_temp_modifier(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 new target temperature if this is the first run
        if job.get_step().get_ticks_elapsed() == 0:
            # fetch the existing temp modifier, set the new value and update the game state
            object_id = job.get_context().get_object_id()
            temp_modifier_id = game_state.get_object_temp_modifier_id(object_id)
            temp_modifier = game_state.get_temp_modifier(temp_modifier_id)
            # the new value is stored as the job context parameter
            temp_modifier.temperature = int(job.get_context().get_parameter())
            game_state.update_temp_modifier(temp_modifier_id, temp_modifier, game_state.get_object(object_id).get_position())

        # 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)
