# -*- coding: utf-8 -*-
import sysactions
import logging

from posot import OrderTakerException
from custom_sysactions import user_authorized_to_execute_action, block_action_if_order_items_not_resolved

from actions.discount.clean_discounts import clean_order_payments
from actions.util import is_delivery_order
from actions.delivery.save_delivery_order import save_delivery_order

logger = logging.getLogger("PosActions")


@sysactions.action
@user_authorized_to_execute_action
@block_action_if_order_items_not_resolved
def doStoreOrder(pos_id, totalize="none", show_popup=True, *args):
    from posactions import doTotal, fill_customer_properties, customer_info_config
    model = sysactions.get_model(pos_id)
    sysactions.check_current_order(pos_id, model=model, need_order=True)

    pos_ot = sysactions.get_posot(model)
    xml_order = sysactions.get_current_order(model)
    if not clean_order_payments(pos_id, pos_ot, xml_order, True, True):
        return

    if sysactions.get_current_order(model).get("type") != "SALE":
        sysactions.show_messagebox(pos_id, message="$THIS_ORDER_TYPE_CANNOT_BE_STORED", icon="warning")
        return

    pod_function = sysactions.get_posfunction(model) if sysactions.get_podtype(model) in ("DT", "FC") else sysactions.get_podtype(model)
    if totalize == "none" and pod_function == "OT":
        totalize = "true"

    if (totalize.lower() != "true" or pod_function == "OT") and customer_info_config is not None:
        customer_document_config = customer_info_config[int(pos_id)]['document']
        customer_name_config = customer_info_config[int(pos_id)]['name']
        request_customer_info = "onSave" in customer_document_config or "onSave" in customer_name_config
        if request_customer_info:
            get_document = "onSave" in customer_document_config
            get_name = "onSave" in customer_name_config
            fill_customer_properties(model, pod_function, pos_id, pos_ot, get_doc=get_document, get_name=get_name)

    if totalize.lower() == "true":
        if doTotal(pos_id, screen_number="") == "Error":
            return

    order = model.find("CurrentOrder/Order")
    # Pre-venda
    pre_venda = None
    if pod_function == "OT":
        for prop in order.findall("CustomOrderProperties/OrderProperty"):
            key, value = prop.get("key"), prop.get("value")
            if key == "PRE_VENDA":
                pre_venda = value
                break
    try:
        pos_ot.storeOrder(pos_id)

        if pre_venda:
            sysactions.show_messagebox(pos_id, "Pre Venda: PV%010d" % int(pre_venda))
        elif _save_order_on_remote_order(order, pos_id, pos_ot):
            if show_popup:
                sysactions.show_messagebox(pos_id, "$ORDER_SAVED")
        else:
            sysactions.show_messagebox(pos_id, "$ERROR_SAVING_ORDER")
    except OrderTakerException as ex:
        sysactions.show_messagebox(pos_id, message="$ERROR_CODE_INFO|%d|%s" % (ex.getErrorCode(), ex.getErrorDescr()))

    order = model.find("CurrentOrder/Order")
    order_id = order.get("orderId")
    logger.debug("Pedido Salvo - Order %s - PosId %s" % (order_id, pos_id))


def _save_order_on_remote_order(order, pos_id, pos_ot):
    model = sysactions.get_model(pos_id)
    if is_delivery_order(model):
        order_id = order.get("orderId")

        try:
            save_delivery_order(order_id)
        except Exception as _:
            session = sysactions.get_operator_session(model)
            pos_ot.recallOrder(int(pos_id), int(order_id), session)
            return False

    return True
