from logging import getLogger

from messagebus import DataType, DefaultToken, Message
from messageprocessorutil import UuidMessageProcessor
from nperipherals_core.interactor.print_interactor import PrinterStatusInteractor
from nperipherals_core.model.device_status import DeviceStatus

logger = getLogger(__name__)


class PrinterStatusProcessor(UuidMessageProcessor):
    def __init__(self, interactor):
        # type: (PrinterStatusInteractor) -> None

        super(PrinterStatusProcessor, 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.get_device_status()

    def format_response(self, message_bus, message, event, input_obj, result):

        if result == DeviceStatus.Ok:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK, DataType.string, "0"))
        else:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.string, "1"))

    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)))
