import logging

from sysactions import action
from typing import Optional

from ..eft import release_wait_confirmation

logger = logging.getLogger("EftActions")


@action
def callback_mobile_reprint(
    pos_id,              # type: str
    data,                # type: str
    result_code=None,    # type: Optional[str]
    message=None,        # type: Optional[str]
):
    # type: (...) -> None

    try:
        if not data or result_code != "0":
            error = "Error mobile reprint: {} | {} | {}".format(result_code, message, data)
            logger.error(error)
            release_wait_confirmation(
                pos_id=pos_id,
                status="0",
            )
            return

        logger.debug("Success mobile reprint: {0}".format(data))
        release_wait_confirmation(
            pos_id=pos_id,
            status="1",
            data=data,
        )
    except (Exception,):
        logger.exception("Error cancel transactions mobile: {0}".format(data))
        release_wait_confirmation(
            pos_id=pos_id,
            status="0",
        )
