from io import StringIO

from cfgtools import Configuration
from messagebus import (
    AckMessage,
    InternalErrorMessage,
)
from messageprocessorutil import UuidMessageProcessor


class GetFullConfigProcessor(UuidMessageProcessor):
    def __init__(self, store_config: Configuration):
        super().__init__()

        self.store_config = store_config

    def parse_data(self):
        return None

    def call_business(self, obj):
        return self.store_config

    def format_response(self, message_bus, message, event, input_obj, result: Configuration):
        with StringIO() as f:
            result.write(f)
            f.seek(0)
            config_str = f.read()

        message_bus.reply_message(
            message=message,
            reply_message=AckMessage(data=config_str.encode()),
        )

    def format_exception(self, message_bus, message, event, input_obj, exception):
        message_bus.reply_message(
            message=message,
            reply_message=InternalErrorMessage(ex=exception),
        )

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