from typing import Any

from interactor import BaseInteractor
from messagebus import Message, DefaultToken
from messageprocessorutil import UuidMessageProcessor
from model.log import LogEntry


class EventProcessor(UuidMessageProcessor):
    def __init__(self, interactor, is_message=False, dummy=False):
        # type: (BaseInteractor, bool, bool) -> None
        super(EventProcessor, self).__init__()
        self.interactor = interactor
        self.is_message = is_message
        self.dummy = dummy

    def parse_data(self, data, event_type):
        # type: (str, str) -> Any

        return data

    def call_business(self, log_entry):
        # type: (LogEntry) -> None
        if log_entry is not None and not self.dummy:
            self.interactor.execute(log_entry)

    def format_response(self, message_bus, message, event, input_obj, result):
        if self.is_message:
            message_bus.reply_message(
                message=message,
                reply_message=Message(DefaultToken.TK_SYS_ACK),
            )
        elif event.synchronous:
            message_bus.reply_event(
                event=event,
                reply_message=Message(DefaultToken.TK_SYS_NAK),
            )

    def format_exception(self, message_bus, message, event, input_obj, exception):
        if self.is_message:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK))
        elif event.synchronous:
            message_bus.reply_event(event, Message(DefaultToken.TK_SYS_NAK))

    def format_parse_exception(self, message_bus, message, event, data, exception):
        self.format_exception(message_bus, message, event, data, exception)

