from typing import (
    List,
)
from xml.etree import cElementTree as eTree

from ._Type import JSON
from ._util import (
    get_time_stamp,
    safe_json_loads,
)


class TableStoreFormatter:

    def __init__(self):
        return

    def get_table_store_picture_dict(
        self,
        table_store_picture,          # type: eTree.Element
    ):
        # type: (...) -> JSON

        table_picture_dict = {
            "Tables": self._get_tables(table_store_picture=table_store_picture),
        }

        return table_picture_dict

    def _get_tables(
        self,
        table_store_picture,        # type: eTree.Element
    ):
        # type: (...) -> List

        tables = []
        for table in table_store_picture.findall(".//Table"):
            table_picture_dict = {
                "id": table.get("id"),
                "name": table.get("name"),
                "type": int(table.get("type") or 1),
                "typeDescr": table.get("typeDescr"),
                "seats": int(table.get("seats") or 1),
                "sector": table.get("sector"),
                "subSector": table.get("subSector"),
                "status": int(table.get("status") or 1),
                "statusDescr": table.get("statusDescr"),
                "serviceId": int(table.get("serviceId")) if table.get("serviceId") else None,
                "businessPeriod": table.get("businessPeriod"),
                "userId": table.get("userId"),
                "serviceSeats": int(table.get("serviceSeats") or 0),
                "startTS": get_time_stamp(table.get("startTS")),
                "lastUpdateTS": get_time_stamp(table.get("lastUpdateTS")),
                "statusHistory": self._get_table_status_history(table.get("statusHistory")),
                "tipAmount": float(table.get("tipAmount") or 0),
                "tipRate": table.get("tipRate"),
                "orders": int(table.get("orders") or 0),
                "Properties": self._get_table_properties(table),
            }

            tables.append(table_picture_dict)

        return tables

    @staticmethod
    def _get_table_status_history(
        status_history,     # type: str
    ):
        # type: (...) -> List

        if status_history is None:
            return []

        status_history = safe_json_loads(status_history)
        histories = []
        for status in status_history:
            table_picture_dict = {
                "status": int(status.get("status") or 1),
                "timestamp": get_time_stamp(status.get("timestamp")),
            }

            histories.append(table_picture_dict)

        return histories

    @staticmethod
    def _get_table_properties(
        table,      # type: eTree.Element
    ):
        # type: (...) -> List

        tables = []
        for table_property in table.findall(".//Properties"):
            occupied_seats = []
            occupied_seats_property = table_property.get("occupiedSeats")
            if occupied_seats_property is not None:
                occupied_seats = occupied_seats_property.split(";")

            table_picture_dict = {
                "mode": table_property.get("MODE"),
                "tipType": table_property.get("TIP_TYPE"),
                "userLevel": int(table_property.get("USER_LEVEL") or 0),
                "occupiedSeats": occupied_seats,
            }

            tables.append(table_picture_dict)

        return tables

