from xml.etree.ElementTree import Element
from pymasep.common.environment import Environment


class EnvironmentInterface(Environment):
    """
    Represents the environment of the game (Interface part). This object keeps all dynamic value of the game.
    This environment choose the action of the agent controlled by the interface

    :param id_env: the id of the environment. If None, a new uuid will be set
    """

    def __init__(self, id_env: str = None) -> None:
        super().__init__(id_env=id_env)
        self.controlled_agent = None

    def choose_action(self) -> None:
        """
        tell the agent controlled by interface to choose their action
        """
        if self.controlled_agent is not None:
            self.controlled_agent.choose_action()
            if self.controlled_agent.action:
                self.logger.info('choose_action() '
                                 + self.controlled_agent.name
                                 + ' ' + str(self.controlled_agent.action))
            else:
                self.logger.info('choose_action() '
                                 + self.controlled_agent.name
                                 + ' None')

    def update_current_next(self, observation_xml: Element, additional_info) -> None:
        """
        update the current state of the interface from the observation

        :param observation_xml: observation as xml
        :param additional_info: additional information
        """
        self.clear_all_state_elements()
        self.current_state = self.create_object(xml_node=observation_xml, parent=None)
        self.current_step = self.current_state.step
        self.current_additional_info = additional_info
        self.logger.info('State : ' + str(self.current_state))
        self.logger.info('Additional info : ' + str(self.current_additional_info))
