from typing import Type
try:
    from xml.etree.cElementTree import Element
except ImportError:
    from xml.etree.ElementTree import Element


from ._PosFunction import PosFunction
from ._PosType import PosType
from ._UserControlType import UserControlType


class WorkingMode(object):
    def __init__(
        self,                   # type: WorkingMode
        pos_type,               # type: PosType
        pos_function,           # type: PosFunction
        user_control_type,      # type: UserControlType
    ):
        # type: (...) -> None

        self.pos_type = pos_type
        self.pos_function = pos_function
        self.user_control_type = user_control_type

    @classmethod
    def from_xml(
        cls,        # type: Type[WorkingMode]
        xml,        # type: Element
    ):
        return cls(
            pos_type=PosType(xml.attrib["podType"]),
            pos_function=PosFunction(xml.attrib["posFunction"]),
            user_control_type=UserControlType(xml.attrib["usrCtrlType"]),
        )

    def to_xml(
        self,       # type: WorkingMode
    ):
        # type: (...) -> Element
        return Element(
            "WorkingMode",
            attrib={
                "podType": self.pos_type.value,
                "posFunction": self.pos_function.value,
                "usrCtrlType": self.user_control_type.value,
            },
        )

    def __eq__(self, other):
        if not isinstance(other, WorkingMode):
            return False

        same_type = self.pos_type == other.pos_type
        same_function = self.pos_function == other.pos_function
        same_user_control_type = self.user_control_type == other.user_control_type

        return same_type and same_function and same_user_control_type
