import json

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

from services.model import POSAlertNotificationRequest
from services.interactor import POSAlertNotificationInteractor


class POSAlertNotificationProcessor(UuidMessageProcessor):
    def __init__(
        self,
        pos_alert_notification_interactor,    # type: POSAlertNotificationInteractor
    ):
        # type: (...) -> None

        super(POSAlertNotificationProcessor, self).__init__()
        self.pos_alert_notification_interactor = pos_alert_notification_interactor

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

        request_json = json.loads(s=data)

        return POSAlertNotificationRequest(event_id=request_json.get('id'))

    def call_business(
        self,
        request,    # type: dict
    ):
        # type: (...) -> str

        return self.pos_alert_notification_interactor.process_request(request=request)

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

        return self._do_not_reply_to_asynchronous_event()

    def format_parse_exception(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        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: Optional[Any]
        input_obj,      # type: POSAlertNotificationRequest
        exception,      # type: Exception
    ):
        # type: (...) -> None

        return self._do_not_reply_to_asynchronous_event()

    @staticmethod
    def _do_not_reply_to_asynchronous_event():
        return None
