import json

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

from application.interactor import RemoveBenefitInteractor
from application.model import Configurations
from typing import Tuple


class RemoveBenefitProcessor(UuidMessageProcessor):
    def __init__(self, configs, interactor):
        # type: (Configurations, RemoveBenefitInteractor) -> None

        super(RemoveBenefitProcessor, self).__init__()
        self.logger = configs.logger
        self.configs = configs
        self.interactor = interactor

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

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

        return pos_id, benefit_keys

    def call_business(self, param):
        # type: (Tuple) -> None

        pos_id, benefit_keys = param
        self.interactor.remove(pos_id, benefit_keys)

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

    def format_exception(self, message_bus, message, event, input_obj, exception):
        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):
        message_bus.reply_message(message, Message(DefaultToken.TK_SYS_NAK, DataType.param, str(exception)))
