from typing import Any

from application.interactor import MediaUpdaterInteractor
from application.model import AsyncUuidMessageProcessor
from messagebus import DefaultToken, DataType, Message
from application.util import settings


class MediaUpdaterProcessor(AsyncUuidMessageProcessor):
    def __init__(self, interactor):
        # type: (MediaUpdaterInteractor) -> None

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

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

        return data or self._do_not_remove_this_function_bug_in_the_library()

    def call_business(self, _):
        # type: (Any) -> Any

        settings.clear_status()
        return self.interactor.update_media(True)

    def format_response(self, message_bus, message, _, __, response):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK, DataType.param, response))

    def format_exception(self, message_bus, message, _, __, exception):
        reply_message = Message(DefaultToken.TK_SYS_NAK, DataType.param, str(type(exception).__name__))
        message_bus.reply_message(message, reply_message)

    def format_parse_exception(self, message_bus, message, _, __, exception):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))

    @staticmethod
    def _do_not_remove_this_function_bug_in_the_library():
        return ''
