from typing import Type, Optional
from xml.etree.ElementTree import Element

from pymasep.common.environment import Environment
from pymasep.common.controller import Controller
from pymasep.common.template import Template
from pymasep.engine.external_controller import ExternalController


class EnvironmentEngine(Environment):
    """
    Represents the environment of the game (Engine part). This object keeps all dynamic values of the game.\
    This environment handle all controllers for external agents (interface) as ExternalController

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

    @staticmethod
    def get_type_controller(xml_node: Optional[Element] = None,
                            template: Optional[Template] = None,
                            control: Optional[str] = None) -> Type[Controller]:
        """
        Get the type of controller according to a template or (exclusive) a xml node.

        :param xml_node: Xml node used to get the controller type. Use the attribut "controller".
        :param template: The template used to get controller type.
        :param control: 'interface' if the agent is controlled by interface \
        (only inside engine, and if template is used)
        :return: the class of the controller to use
        """
        if (xml_node is not None and 'control' in xml_node.attrib and xml_node.attrib['control'] is not None) or \
                (template and control == 'interface'):
            class_controller_ = ExternalController
        else:
            class_controller_ = super(EnvironmentEngine, EnvironmentEngine).get_type_controller(xml_node=xml_node,
                                                                                                template=template)

        return class_controller_
