from messagebus import (
    Message,
    DefaultToken,
    DataType,
    Event,
    MessageBus,
)
from messageprocessorutil import UuidMessageProcessor
from application.interactor import HasPendingLoaderUpdateInteractor
from typing import (
    Any,
    Optional,
)


class HasPendingLoaderUpdateProcessor(UuidMessageProcessor):

    def __init__(
        self,           # type: HasPendingLoaderUpdateProcessor
        interactor,     # type: HasPendingLoaderUpdateInteractor
    ):
        # type: (...) -> None
        super(HasPendingLoaderUpdateProcessor, self).__init__()

        self.interactor = interactor

    def parse_data(
        self,           # type: HasPendingLoaderUpdateProcessor
        _,              # type: str
        event_type,     # type: str
    ):
        # type: (...) -> None

        return

    def call_business(
        self,   # type: HasPendingLoaderUpdateProcessor
        _,      # type: str
    ):
        # type: (...) -> bool

        return self.interactor.get_loader_pending_update()

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

        reply_data = {"loader_has_pending_update": result}

        reply_message = Message(
            token=DefaultToken.TK_SYS_ACK,
            data_type=DataType.param,
            data=reply_data,
        )
        message_bus.reply_message(
            message=message,
            reply_message=reply_message,
        )

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

        exception_name = str(type(exception).__name__)
        message_bus.reply_message(
            message=message,
            reply_message=Message(
                token=DefaultToken.TK_SYS_NAK,
                data_type=DataType.param,
                data=exception_name,
            )
        )

    def format_parse_exception(
        self,           # type: HasPendingLoaderUpdateProcessor
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Event
        data,           # type: str
        exception,      # type: Exception
    ):
        # type: (...) -> None

        return
