from abc import ABCMeta

from messagebus import (
    DefaultToken,
    InternalErrorMessage,
    Message,
    MessageBus,
)
from messageprocessorutil import UuidMessageProcessor
from typing import (
    Any,
    Optional,
)


class BaseProcessor(UuidMessageProcessor):
    __metaclass__ = ABCMeta

    def __init__(
        self,   # type: BaseProcessor
    ):
        # type: (...) -> None
        super(BaseProcessor, self).__init__()

    def format_response(
        self,           # type: BaseProcessor
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        request,        # type: Any
        result,         # type: Any
    ):
        # type: (...) -> None

        message_bus.reply_message(
            message=message,
            reply_message=Message(token=DefaultToken.TK_SYS_ACK),
        )

    def format_exception(
        self,           # type: BaseProcessor
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        input_obj,      # type: Any
        exception,      # type: Exception
    ):
        # type: (...) -> None

        message_bus.reply_message(
            message=message,
            reply_message=InternalErrorMessage(ex=exception),
        )

    def format_parse_exception(
        self,           # type: BaseProcessor
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        data,           # type: Any
        exception,      # type: Exception
    ):
        # type: (...) -> None

        return self.format_exception(
            message_bus=message_bus,
            message=message,
            event=event,
            input_obj=data,
            exception=exception,
        )
