from typing import (
    Any,
    Optional,
)

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

from dailygoals_services.interactor.update_total_sold import UpdateTotalSoldTableInteractor
from externals.util import LoggerUtil

logger = LoggerUtil.get_logger_name(__name__)


class UpdateTotalSoldTableProcessor(UuidMessageProcessor):
    def __init__(self, interactor):
        # type: (UpdateTotalSoldTableInteractor) -> None

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

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

        return data

    def call_business(self, params):
        # type: (Any) -> None

        self.interactor.update(params)

    def format_response(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Event]
        request,        # type: Any
        response,       # type: Any
    ):
        # type: (...) -> None

        logger.info("Total sold table updated for request")

    def format_exception(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Event]
        request,        # type: Any
        exception,      # type: Exception
    ):
        # type: (...) -> None

        exception_name = str(type(exception).__name__)
        logger.info("Error updating total sold table from request: {}".format(exception_name))

    def format_parse_exception(
        self,
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Optional[Event]
        request,        # type: Any
        exception,      # type: Exception
    ):
        # type: (...) -> None

        logger.info("Error getting parsing request: {}".format(str(exception)))
