from application.interactor import LoyaltyAvailableInteractor
from application.model import AsyncUuidMessageProcessor
from messagebus import DefaultToken, DataType, Message


class LoyaltyAvailableProcessor(AsyncUuidMessageProcessor):
    def __init__(self, interactor):
        # type: (LoyaltyAvailableInteractor) -> None

        super(LoyaltyAvailableProcessor, self).__init__()
        self.interactor = interactor

    def parse_data(self, _):
        # it does not expect any data
        pass

    def call_business(self, _):
        # type: (None) -> bool

        return self.interactor.is_loyalty_available()

    def format_response(self, message_bus, message, event, request, response):
        if response:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK))
        else:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK))

    def format_exception(self, message_bus, message, event, input_obj, exception):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))

    def format_parse_exception(self, message_bus, message, event, data, exception):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))
