import json

from typing import List

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

from application.interactor import GetBenefitsInteractor
from application.model import Configurations


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

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

        data = json.loads(data)
        only_visible = bool(data.get("only_visible", False))

        return only_visible

    def call_business(self, only_visible):
        # type: (bool) -> List[dict]

        return self.interactor.apply(only_visible)

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

    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)))
