from mbcontextmessagehandler import MbContextMessageBus
from messagebus import (
    Message,
    DataType,
    TokenCreator,
    TokenPriority,
    DefaultToken,
)
from application.model.customexception import ErrorSetFiscalId
from mwhelper import BaseRepository

TK_FISCALWRAPPER_SET_FISCAL_ID = TokenCreator.create_token(
    priority=TokenPriority.low,
    message_group="31",
    number="34",
)

class FiscalPOSRepository(BaseRepository):

    def __init__(
        self,           # type: FiscalPOSRepository
        message_bus,    # type: MbContextMessageBus
    ):
        super(FiscalPOSRepository, self).__init__(message_bus.mbcontext)

        self.message_bus = message_bus
        self.component_name = "FiscalWrapper"


    def set_fiscal_id(
        self,        # type: FiscalPOSRepository
        fiscal_id,   # type: int
    ):
        # type: (...) -> None

        message = Message(
            token=TK_FISCALWRAPPER_SET_FISCAL_ID,
            data_type=DataType.param,
            data=str(fiscal_id),
        )
        res = self.message_bus.send_message(
            component=self.component_name,
            message=message,
        )
        if res.token != DefaultToken.TK_SYS_ACK:
            raise ErrorSetFiscalId("Error setting fiscal id: {}".format(res.data))
