import json

from messagebus import DefaultToken, DataType, Message
from messageprocessorutil import UuidMessageProcessor

from application.interactor import OnOrderTotalInteractor
from application.model import Configurations


class OnOrderTotalProcessor(UuidMessageProcessor):
    def __init__(self, configs, interactor):
        # type: (Configurations, OnOrderTotalInteractor) -> None
        super(OnOrderTotalProcessor, self).__init__()
        self.logger = configs.logger
        self.configs = configs
        self.interactor = interactor

    def parse_data(self, data, event_type):
        # type: (str, str) -> (int, bool)

        data = json.loads(data)
        pos_id = int(data.get("pos_id"))

        return pos_id

    def call_business(self, pos_id):
        # type: (int) -> None

        self.interactor.execute(pos_id)

    def format_response(self, message_bus, message, event, request, response):
        if message:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_ACK, DataType.string, ""))

    def format_exception(self, message_bus, message, event, input_obj, exception):
        if message:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))

    def format_parse_exception(self, message_bus, message, event, data, exception):
        if message:
            message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))
