from typing import (
    Dict,
    Optional,
)
from xml.etree import ElementTree

from ._TableStatus import TableStatus


class SimpleTable(object):
    def __init__(
        self,                               # type: SimpleTable
        table_id,                           # type: str
        name=None,                          # type: Optional[str]
        seats=1,                            # type: int
        status=TableStatus.Available,       # type: TableStatus
        pos_id=None,                        # type: Optional[int]
        is_tab=False,                       # type: bool
        sector="",                          # type: str
    ):
        self.table_id = table_id
        self.seats = seats
        self.name = name or (None if is_tab else str(self.table_id))
        self.status = status
        self.pos_id = pos_id
        self.is_tab = is_tab
        self.sector = sector

    @staticmethod
    def from_xml(
        xml,  # type: ElementTree.Element
    ):
        is_tab = xml.attrib["id"].startswith("TAB-")
        table_id = xml.attrib["id"]
        return SimpleTable(
            table_id=table_id,
            name=xml.attrib["name"] if xml.get("name") else None,
            seats=int(xml.attrib["seats"]),
            status=TableStatus(int(xml.attrib["status"])),
            pos_id=int(xml.get("currentPOSId")) if xml.get("currentPOSId") else None,
            is_tab=is_tab,
            sector=xml.attrib["sector"] if xml.get("sector") else None,
        )

    def get_table_xml_attrib(self):
        # type: (...) -> Dict[str, str]
        return {
            "id": str(self.table_id),
            "seats": str(self.seats),
            "name": self.name or "",
            "status": str(self.status.value),
            "statusDescr": self.status.name,
            "type": "2" if self.is_tab else "1",
            "typeDescr": "Tab" if self.is_tab else "Seat",
        }
