from typing import Optional
from xml.etree import cElementTree as eTree

from application.repository.pos.tablemgr import TablePictureRepository


class TableService(object):
    def __init__(
        self,                           # type: TableService
        table_picture_repository,       # type: TablePictureRepository
    ):
        # type: (...) -> None

        self.table_picture_repository = table_picture_repository

    def get_table_picture(
        self,                   # type: TableService
        pos_id,                 # type: int
        order_picture,          # type: eTree
    ):
        # type: (...) -> Optional[eTree]

        table_id = None
        table_id_key = order_picture.findall(".//OrderProperty[@key='TABLE_ID']")
        if table_id_key:
            table_id = table_id_key[0].get("value")

        if table_id:
            return self.table_picture_repository.get_table_info(
                pos_id=pos_id,
                table_id=table_id
            )
