import json

from application.model import ModelJsonEncoder
from messagebus import Message, DefaultToken, DataType
from typing import Any

from application.interactor import GetVersionsInteractor
from messageprocessorutil import UuidMessageProcessor


class GetVersionsProcessor(UuidMessageProcessor):
    def __init__(self, interactor, event_name=None):
        # type: (GetVersionsInteractor, str) -> None

        super(GetVersionsProcessor, self).__init__(event_name)
        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, obj):
        return json.dumps(self.interactor.get_versions(), encoding="utf-8", cls=ModelJsonEncoder)

    def format_response(self, message_bus, message, event, input_obj, result):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK, DataType.param, result))

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

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

    @staticmethod
    def _do_not_remove_this_function_bug_in_the_library():
        return ''
