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

from actions.delivery.delivery_address_information import insert_or_update_customer
from sysactions import action, show_any_dialog, show_messagebox
from custom_sysactions import block_action_if_order_items_not_resolved

logger = logging.getLogger("PosActions")


@action
@block_action_if_order_items_not_resolved
def delivery_address(pos_id):
    try:
        customer = show_any_dialog(pos_id, "deliveryAddress", "", "", "", "", "", 6000000, "", "", "", "", "", False)
        customer_json = json.loads(customer)
        if customer_json:
            insert_or_update_customer(pos_id, customer_json)
            return True
    except Exception as _:
        logger.exception("Error inserting or updating customer")
        show_messagebox(pos_id, message="$CUSTOMER_ERROR_ON_UPDATE")

    return False
