import json

from messageprocessorutil import UuidMessageProcessor
from messagebus import (
    NakMessage,
    AckMessage,
    MessageBus,
)

from production.interactor import PickListOrdersInteractor
from production.repository import ProductionRepository
from typing import (
    Dict,
    Optional,
)


class PickListOrdersProcessor(UuidMessageProcessor):

    def __init__(
        self,
        message_bus,                 # type: MessageBus
        production_repository,       # type: ProductionRepository
    ):
        # type: (...) -> None

        super(PickListOrdersProcessor, self).__init__()

        self.interactor = PickListOrdersInteractor(
            message_bus=message_bus,
            production_repository=production_repository,
        )

    def parse_data(
        self,
        data,        # type: str
    ):
        data = json.loads(
            s=data,
        )

        return data

    def call_business(
        self,
        obj,        # type: Dict
    ):
        # type: (...) -> Optional[Dict]

        return self.interactor.execute(
            obj=obj,
        )

    def format_response(self, message_bus, message, event, input_obj, result):
        message_bus.reply_message(
            message=message,
            reply_message=AckMessage(json.dumps(result)),
        )

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

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