from typing import (
    Dict,
    List,
    Optional,
    Union,
)

from messagebus import DefaultToken
from messageprocessor import (
    DefaultMessageProcessorExecutorFactory,
    MessageProcessor,
    MessageProcessorCallback,
    MessageProcessorExecutorFactory,
    MessageProcessorMessageHandler,
)
from simplehttprouter import Route

from ._HttpMessageProcessor import HttpMessageProcessor
from ._RouterProcessorBuilder import RouterProcessorBuilder


class HttpMessageProcessorMessageHandler(MessageProcessorMessageHandler):

    def __init__(
        self,                   # type: HttpMessageProcessorMessageHandler
        route_processor,        # type: Union[Dict[Route, MessageProcessor], RouterProcessorBuilder]
        subject_counter=None,   # type: Optional[Dict[str, int]]
        callbacks=None,         # type: Optional[List[MessageProcessorCallback]]
        factory=None,           # type: Optional[MessageProcessorExecutorFactory]
    ):
        # type: (...) -> None

        msg_dict = {
            DefaultToken.TK_HTTP_MSG: HttpMessageProcessor(route_processor=route_processor)
        }
        executor_factory = None
        if factory is not None:
            executor_factory = factory
        elif callbacks is not None:
            executor_factory = DefaultMessageProcessorExecutorFactory(callbacks=callbacks)

        super(HttpMessageProcessorMessageHandler, self).__init__(
            None,
            msg_dict,
            executor_factory,
            subject_counter,
        )
