from logging import getLogger
from messagebus import DataType, DefaultToken, Message
from messageprocessorutil import UuidMessageProcessor
from nperipherals_core.interactor.cash_drawer_interactor import CashDrawerOpenInteractor

logger = getLogger(__name__)


class CashDrawerOpenProcessor(UuidMessageProcessor):
    def __init__(self, interactor):
        # type: (CashDrawerOpenInteractor) -> None
        super(CashDrawerOpenProcessor, self).__init__()
        self.interactor = interactor

    def parse_data(self, data, event_type):
        # we don't expect data
        pass

    def call_business(self, obj):
        return self.interactor.open_drawer()

    def format_response(self, message_bus, message, event, input_obj, result):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK, DataType.string, str(result)))

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

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