import json
from typing import (
    Any,
    Dict,
    Optional,
)

from messagebus import (
    AckMessage,
    Event,
    InternalErrorMessage,
    Message,
    MessageBus,
)
from messageprocessorutil import UuidMessageProcessor

from store_config.interactor import GetStoreConfigInteractor


class UpdateStoreConfigProcessor(UuidMessageProcessor):

    def __init__(
        self,
        interactor: GetStoreConfigInteractor,
    ) -> None:

        super().__init__()
        self.interactor = interactor

    def parse_data(
        self,
        data: Optional[bytes],
    ) -> None:

        return None

    def call_business(
        self,
        _,
    ) -> bool:

        return self.interactor.execute()

    def format_response(
        self,
        message_bus: MessageBus,
        message: Message,
        event: Event,
        request: Any,
        result: Dict,
    ) -> None:

        return message_bus.reply_message(
            message=message,
            reply_message=AckMessage(
                data=json.dumps(result).encode(),
            )
        )

    def format_exception(
        self,
        message_bus: MessageBus,
        message: Message,
        event: Event,
        input_obj: str,
        exception: Exception,
    ) -> None:

        message_bus.reply_message(
            message=message,
            reply_message=InternalErrorMessage(ex=exception)
        )

    def format_parse_exception(
        self,
        message_bus: MessageBus,
        message: Message,
        event: Event,
        data: bytes,
        exception: Exception,
    ) -> None:

        self.format_exception(
            message_bus=message_bus,
            message=message,
            event=event,
            input_obj=data.decode("utf-8"),
            exception=exception,
        )
