from typing import Any

from application.model import AsyncUuidMessageProcessor
from messagebus import (
    DefaultToken,
    DataType,
    Message,
    MessageBus,
    Event
)
from application.interactor import ChromeUpdaterInteractor


class ChromeUpdaterProcessor(AsyncUuidMessageProcessor):
    def __init__(
        self,
        interactor    # type: ChromeUpdaterInteractor
    ):
        # type: (...) -> None

        super(ChromeUpdaterProcessor, self).__init__()
        self.interactor = interactor

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

        return str(data) != "noValidation"

    def call_business(
        self,
        data     # type: str
    ):
        # type: (...) -> Any

        return self.interactor.update_chrome(data)

    @staticmethod
    def format_response(
        message_bus,  # type: MessageBus
        message,      # type: Message
        event,        # type: Event
        _,
        response      # type: Any
    ):
        # type: (...) -> None

        reply_message = Message(
            token=DefaultToken.TK_SYS_ACK,
            data_type=DataType.param,
            data=response
        )
        if message:
            message_bus.reply_message(
                message=message,
                reply_message=reply_message
            )
        elif event:
            message_bus.reply_event(
                event=event,
                reply_message=reply_message
            )

    def format_exception(
        self,
        message_bus,      # type: MessageBus
        message,          # type: Message
        event,            # type: Event
        _,
        exception         # type: Any
    ):
        # type: (...) -> None

        self._reply_exception(
            event=event,
            exception=exception,
            message=message,
            message_bus=message_bus
        )

    def format_parse_exception(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Event
        _,
        exception       # type: Any
    ):
        # type: (...) -> None

        self._reply_exception(
            event=event,
            exception=exception,
            message=message,
            message_bus=message_bus
        )

    @staticmethod
    def _reply_exception(
        event,          # type: Event
        exception,      # type: Any
        message,        # type: Message
        message_bus     # type: MessageBus
    ):
        # type: (...) -> None

        reply_message = Message(
            token=DefaultToken.TK_SYS_NAK,
            data_type=DataType.param,
            data=str(exception)
        )
        if message:
            reply_message = Message(
                token=DefaultToken.TK_SYS_NAK,
                data_type=DataType.param,
                data=str(type(exception).__name__)
            )
            message_bus.reply_message(
                message=message,
                reply_message=reply_message
            )
        elif event:
            message_bus.reply_event(
                event=event,
                reply_message=reply_message
            )

    @staticmethod
    def _do_not_remove_this_function_bug_in_the_library():
        return ''
