from xml.etree import cElementTree as eTree

from sysactions import (
    get_model,
    has_current_order,
    get_current_order,
)

from actions.order.order_was_already_saved import order_was_already_saved
from actions.login.pause_user import pause_user
from actions.table.store_table_service import store_table_service
from api.order import get_order_service
from .. import logger


def close_active_order_and_pause_user(
    pos_id,         # type: str
    void_reason,    # type: str
):
    # type: (...) -> None

    try:
        model = get_model(posid=pos_id)
        if has_current_order(model=model):
            _close_order(
                model=model,
                pos_id=pos_id,
                void_reason=void_reason,
            )

        logger.debug("Call pause user")
        pause_user(
            pos_id=pos_id,
        )
    except Exception as ex:
        logger.exception("Close active order because", exc_info=ex)


def _close_order(
    model,          # type: eTree.XML
    pos_id,         # type: str
    void_reason,    # type: str
):
    # type: (...) -> None

    current_order = get_current_order(model=model)
    table_id = current_order.find("CustomOrderProperties/OrderProperty/[@key='TABLE_ID']")
    if table_id is not None:
        _close_table_service(
            pos_id=pos_id,
            order=current_order,
            received_void_reason=void_reason,
        )
    else:
        _close_quick_service_order(
            model=model,
            pos_id=pos_id,
            order=current_order,
            received_void_reason=void_reason,
        )


def _close_quick_service_order(
    model,                  # type: eTree.XML
    pos_id,                 # type: str
    order,                  # type: eTree.Element
    received_void_reason,   # type: str
):
    # type: (...) -> None

    logger.debug("Start close quick service order")
    if order.get("state") not in ("IN_PROGRESS", "TOTALED"):
        return

    if order_was_already_saved(order=order):
        logger.debug("Store order was already saved")
        order_service = get_order_service(pos_id=pos_id)
        order_service.store_order()
        return

    logger.debug("Do void without authorization")
    from actions.void.do_void_order import do_void_without_authorization
    do_void_without_authorization(
        pos_id=pos_id,
        received_void_reason=received_void_reason,
    )


def _close_table_service(
    pos_id,                 # type: str
    order,                  # type: eTree.Element
    received_void_reason,   # type: str
):
    # type: (...) -> None

    logger.debug("Start close table service")
    if order.get("state") == "IN_PROGRESS" and not order_was_already_saved(order=order):
        logger.debug("Do void without authorization")
        from actions.void.do_void_order import do_void_without_authorization
        do_void_without_authorization(
            pos_id=pos_id,
            received_void_reason=received_void_reason,
        )

    logger.debug("Store table service")
    store_table_service(
        pos_id=pos_id,
    )
