import logging
import sysactions
import time

from bustoken import TK_SITEF_CONFIRM_FINISH
from msgbus import FM_PARAM, TK_SYS_ACK
from sysactions import mbcontext

logger = logging.getLogger("TableActions")


@sysactions.action
def confirm_finish_eft_transactions(pos_id, status):
    sitef_comp_name = "Sitef%02d" % int(pos_id)
    data = str(pos_id) + ";" + status
    for try_num in range(1, 4):
        try:
            ten_minutes_timeout = 600000000
            ret = mbcontext.MB_EasySendMessage(
                sitef_comp_name,
                TK_SITEF_CONFIRM_FINISH,
                FM_PARAM,
                data,
                ten_minutes_timeout
            )
            if ret.token == TK_SYS_ACK:
                logger.info("Success confirming finished eft transaction. {}".format(pos_id))
                return
            else:
                message = "ACK not received when confirming finish. {} - {} - {}"
                logger.info(message.format(pos_id, ret.token, ret.data))
            time.sleep(1)

        except Exception as _:
            logger.info("Error confirming finish")
