import json

from xml.etree import cElementTree as eTree

from sysactions import get_model, get_custom

from application.model.order import Order


def get_service_period(order):
    # type: (Order) -> (str, eTree.XML)

    model = get_model(order.pos_id)
    service_period = get_custom(model, 'SERVICE_PERIOD') if model else None
    service_period = json.loads(service_period) if service_period else None

    if order.table_id:
        order.get_table_info()
        if order.special_catalog:
            return order.special_catalog
        if service_period and order.sector in service_period['sectors'].keys():
            return service_period['sectors'][order.sector]

    if service_period:
        return service_period

    return {}
