import logging
import sysactions

from sysactions import mbcontext
from typing import Optional

from tef import PrintReceipt

logger = logging.getLogger("PosActions")


def reprint_receipt(
    pos_id,                 # type: str
    receipt,                # type: str
    fiscalization_type="",  # type: Optional[str]
):
    # type: (...) -> bool

    eft_check = _check_eft_value(
        pos_id=pos_id,
        eft_value=receipt,
        error_message="$PAYMENT_HAS_NO_TEF",
    )
    if not eft_check:
        return False

    printer_receipt = PrintReceipt(
        mb_context=mbcontext,
        logger=logger,
    )

    reprinted = printer_receipt.reprint_receipt(
        pos_id=pos_id,
        receipt=receipt,
        fiscalization_type=fiscalization_type,
    )

    return reprinted


def _check_eft_value(
    pos_id,         # type: str
    eft_value,      # type: str
    error_message,  # type: str
):
    # type (...) -> bool

    if eft_value in ['', None]:
        sysactions.show_messagebox(
            posid=pos_id,
            message=error_message,
            title="$REPRINT_TEF_ERROR",
            buttons="$OK",
        )
        return False

    return True
