from olives_gamestate import GameState
from olives_object_type import ObjectType

from .olives_abstractobject import AbstractObject
from olives_object import Object, MessageType
from olives_types import Position2, Position3, LightInfo


class AbstractTempModifier(AbstractObject):
    """
    This is the base class for temperature modifying objects.
    Unlike the abstract light source, this does not take over the responsibility of
    creating and updating temperature modifiers. This is because these are more akin
    to the light sources created by windows and as such more deeply integrated.
    If something needs to dynamically modify them, they can still be fetched by
    running get_object_temp_modifier_id() on the game_state.

    The "olives" prefix in the file indicates it is not meant to be edited,
    as this will most definitely result in breakage.
    """
    is_active = 1
    change_status = False

    def __init__(self, object_type_name: str, object_id: int, object_type: ObjectType):
        super().__init__(object_type_name, object_id, object_type)

    def on_tick(self, obj: Object, elapsed_ticks: int, game_state: GameState, temperature: int):
        if self.change_status:
            # also fetch the existing temp modifier, set the new value and update the game state
            temp_modifier_id = game_state.get_object_temp_modifier_id(self.object_id)
            temp_modifier = game_state.get_temp_modifier(temp_modifier_id)
            # the new value is stored as the job context parameter
            temp_modifier.is_active = self.is_active
            game_state.update_temp_modifier(temp_modifier_id, temp_modifier, game_state.get_object(self.object_id).get_position())
            self.change_status = False
            return

    def on_enable(self, obj: Object):
        self.is_active = 1
        self.change_status = True

    def on_disable(self, obj: Object):
        self.is_active = 0
        self.change_status = True
