from messagebus import AckMessage, NakMessage
from messageprocessorutil import UuidMessageProcessor
from zeromqeventmanager import EventManager


class KeepAliveProcessor(UuidMessageProcessor):
    def __init__(self, event_manager, event_name=None):
        # type: (EventManager, str) -> None
        super(KeepAliveProcessor, self).__init__(event_name)
        self.event_manager = event_manager

    def parse_data(self, data):
        return data.decode("utf-8")

    def call_business(self, sub_id):
        self.event_manager.keep_alive(sub_id)

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

    def format_exception(self, message_bus, message, event, input_obj, exception):
        message_bus.reply_message(message, NakMessage(str(exception).encode("utf-8")))

    def format_parse_exception(self, message_bus, message, event, data, exception):
        message_bus.reply_message(message, NakMessage(str(exception).encode("utf-8")))
