from pymasep.utils import native_xml_types


class Initializer:
    """
    Initializer used to initialize the value of BaseObject.
    An initializer can be executed by the Engine (by default) or processed by the Interface.

    An initializer is created from a dictionary. the format is the following:

    - name: name of the initializer used as id
    - value_type: for Characteristic, the type of the value (as string)
    - subclass_initializer: name of sub initializer used to initialize sub BaseObject.\
                              These initializers must already exist in the game

    :param game: the game where the initializer is created
    :param initializer_dict: the dictionary used to create the initializer
    """

    """ Initializer for the Engine """
    INIT_TYPE_ENGINE = 'Engine'
    """ Initializer for the Interface """
    INIT_TYPE_INTERFACE = 'Interface'

    def __init__(self, game, initializer_dict: dict) -> None:
        self.game = game
        self.name = initializer_dict['name']
        self.value_type = native_xml_types[initializer_dict.get('value_type')]
        self.subclass_initializer = {}

        if 'subclass_initializer' in initializer_dict:
            for sub_element, init_str in initializer_dict['subclass_initializer'].items():
                self.subclass_initializer[sub_element] = game.initializers[init_str]

        self.init_type = self.INIT_TYPE_ENGINE

    def apply(self, base_object) -> None:
        """
        Apply the initializer.

        :param base_object: the BaseObject to initialize
        """
        pass
