import json

from xml.etree import cElementTree as eTree

from typing import (
    Optional,
)

from mwhelper import ModelService
from sysactions import (
    get_model,
    get_posot,
    get_podtype,
)


class StaticUnitOfWork(object):
    def __init__(
        self,
        pos_id,         # type: int
        order_picture,  # type: eTree.Element
        model=None,     # type: Optional[eTree.Element]
        table_id=None,  # type: Optional[str]
    ):
        # type: (...) -> None

        self.pos_id = pos_id
        self.table_id = table_id

        self.model = model
        if model is None:
            self.model = get_model(posid=pos_id)

        self.pos_ot = get_posot(model=self.model)
        self.order_picture = order_picture
        self._price_list = None
        self.table_xml = None

    def get_table_info(
        self,
    ):
        # type: (...) -> None

        model = self.model
        if model is None:
            model = get_model(self.pos_id)
        self.table_xml = self._get_table_xml(model)

    def set_order_picture(
        self,
        order_picture,  # type: eTree.Element
    ):
        # type: (...) -> None

        if order_picture is not None:
            self.order_picture = order_picture

    def _get_table_custom_property_info(
        self,
        table_xml,  # type: eTree.Element
    ):
        # type: (...) -> None

        for custom_property in table_xml.findall(".//Properties"):
            service_period = custom_property.get('SERVICE_PERIOD_TABLE', None)
            if service_period:
                self.special_catalog = json.loads(service_period)
                break

    def _get_table_xml(
        self,
        model,  # type: eTree.Element
    ):
        # type: (...) -> Optional[eTree.Element]

        from tablemgrapi import get_pos_ts

        table_id = self.table_id
        if not table_id:
            return

        pos_ts = get_pos_ts(model=model)
        return eTree.XML(pos_ts.getTableInfo(table_id=table_id))

    @property
    def price_list(
        self,
    ):
        # type: (...) -> str

        if self._price_list is None:
            pod_type = get_podtype(model=self.model)
            sale_type = self.order_picture.get("saleTypeDescr") if self.order_picture is not None else None
            model_service = ModelService(pos_id=self.pos_id)
            self._price_list = model_service.get_price_list_from_model(
                pod_type=pod_type,
                sale_type=sale_type,
                model=self.model,
                order_picture=self.order_picture,
            )
        return self._price_list

    @property
    def tip(
        self,
    ):
        # type: (...) -> float

        if not self.table_id:
            return 0

        if self.table_xml is None:
            self.get_table_info()

        return float(self.table_xml.get("tip", 0))

    @property
    def seats(
        self,
    ):
        # type: (...) -> int

        if not self.table_id:
            return 0

        if self.table_xml is None:
            self.get_table_info()

        return int(self.table_xml.get("serviceSeats", 0))

    @property
    def sector(
        self,
    ):
        # type: (...) -> Optional[str]

        if not self.table_id:
            return None

        if self.table_xml is None:
            self.get_table_info()
        return self.table_xml.get("sector", None)
