import json
import logging

from logging import Logger

from bustoken import TK_NCOUPON_TEST_CONNECTION
from msgbus import FM_PARAM, TK_SYS_ACK
from sysactions import send_message


class NCouponInteractor(object):
    def __init__(self):
        # type: () -> None

        self.logger = logging.getLogger("PosActions")   # type: Logger

    def test_connection(
        self,
        pos_id,     # type: bool
    ):
        # type: (...) -> bool

        try:
            data = json.dumps({
                "posId": pos_id
            })
            response = send_message("nCoupon", TK_NCOUPON_TEST_CONNECTION, FM_PARAM, data, timeout=30000000)
            return response.token == TK_SYS_ACK
        except Exception as _:
            self.logger.exception("Error testing nCoupon connection")
            return False
