import json

from messageprocessorutil import UuidMessageProcessor
from messagebus import NakMessage, AckMessage
from production.model import ProductionModelJsonEncoder

from production.repository import ProductRepository
from production.interactor import GetCoursesInteractor


class GetCoursesProcessor(UuidMessageProcessor):
    def __init__(self, product_repository):
        # type: (ProductRepository) -> None

        super(GetCoursesProcessor, self).__init__()
        self.interactor = GetCoursesInteractor(product_repository)

    def parse_data(self, _):
        return ''

    def call_business(self, obj):
        return self.interactor.execute()

    def format_response(self, message_bus, message, event, input_obj, result):
        result = json.dumps(result, encoding="utf-8", cls=ProductionModelJsonEncoder)
        message_bus.reply_message(message, AckMessage(result))

    def format_exception(self, message_bus, message, event, input_obj, exception):
        message_bus.reply_message(message, NakMessage())

    def format_parse_exception(self, message_bus, message, event, data, exception):
        message_bus.reply_message(message, NakMessage())
