import logging

from sysactions import send_message
from msgbus import TK_SYS_ACK, FM_PARAM
from model.customexception import ErrorGettingNFType, ErrorGettingCertificateExpirationDays
from bustoken import TK_FISCALWRAPPER_GET_NF_TYPE, TK_FISCALWRAPPER_GET_CERTIFICATE_EXPIRATION_DAYS


class FiscalWrapperInteractor(object):

    def __init__(self):
        self.logger = logging.getLogger("PosActions")

    @staticmethod
    def get_nf_type():
        # type: () -> str

        response = send_message("FiscalWrapper", TK_FISCALWRAPPER_GET_NF_TYPE, FM_PARAM, None, timeout=30000000)
        if response.token == TK_SYS_ACK:
            return response.data.upper()

        raise ErrorGettingNFType(response.data)

    @staticmethod
    def get_certificate_expiration_days():
        # type: () -> int

        response = send_message(
            dest_name="FiscalWrapper",
            token=TK_FISCALWRAPPER_GET_CERTIFICATE_EXPIRATION_DAYS,
            format=FM_PARAM,
            data='',
            timeout=30000000,
        )

        if response.token == TK_SYS_ACK:
            return int(response.data)

        raise ErrorGettingCertificateExpirationDays(response.data)
