import json

from messagebus import (
    AckMessage,
    Event,
    Message,
    MessageBus,
    NakMessage,
)
from messageprocessorutil import UuidMessageProcessor
from typing import Any

from services.interactor import OrderCanceledInteractor
from services.model import OrderCanceledRequest


class OrderCanceledProcessor(UuidMessageProcessor):
    def __init__(
        self,
        order_canceled_interactor,    # type: OrderCanceledInteractor
    ):
        # type: (...) -> None

        super(OrderCanceledProcessor, self).__init__()
        self.order_canceled_interactor = order_canceled_interactor

    def parse_data(
        self,
        data,           # type: str
        event_type,     # type: str
    ):
        # type: (...) -> OrderCanceledRequest

        request_json = json.loads(data)

        return OrderCanceledRequest(
            pos_id=request_json["posId"],
            order_id=request_json["orderId"],
        )

    def call_business(
        self,
        request,  # type: OrderCanceledRequest
    ):
        # type: (...) -> None

        self.order_canceled_interactor.process_request(request)

    def format_response(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Event
        request,        # type: OrderCanceledRequest
        result,         # type: None
    ):
        # type: (...) -> None

        return self._do_not_reply_to_asynchronous_event()

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

        return self._do_not_reply_to_asynchronous_event()

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

        return self._do_not_reply_to_asynchronous_event()

    @staticmethod
    def _do_not_reply_to_asynchronous_event():
        return None
