from typing import (
    Optional,
    Any,
)

from messagebus import (
    Message,
    DefaultToken,
    DataType,
    MessageBus,
    TokenPriority,
    TokenCreator,
    Event,
)

from messageprocessorutil import UuidMessageProcessor

from messagebus import AckMessage, NakMessage


class PrnStatusPrintProcessor(UuidMessageProcessor):
    def __init__(
        self,
        pos_id,         # type: int
        message_bus,    # type: MessageBus
    ):
        # type: (...) -> None
        super(PrnStatusPrintProcessor, self).__init__()

        self.pos_id = pos_id
        self.message_bus = message_bus

    def parse_data(
        self,
        data,           # type: Optional[str]
        event_type,     # type: Optional[str]
    ):
        # type: (...) -> Optional[str]

        return data

    def call_business(
        self,
        receive_data,      # type: Optional[str]
    ):
        return "MOBILE_PRINT"

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

        message_bus.reply_message(
            message=message,
            reply_message=AckMessage(data=result),
        )

    def format_exception(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        input_obj,      # type: Optional[str]
        exception,      # type: Exception
    ):
        # type: (...) -> None

        self.format_parse_exception(
            message_bus=message_bus,
            message=message,
            event=event,
            data=input_obj,
            exception=exception,
        )

    @staticmethod
    def format_parse_exception(
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Any]
        data,           # type: Optional[str]
        exception,      # type: Exception
    ):
        # type: (...) -> None

        message_bus.reply_message(
            message=message,
            reply_message=NakMessage(data=exception.message),
        )
