import os

from configurator import Configurator
from mbcontextmessagehandler import MbContextMessageBus, MbContextMessageHandler
from messagehandlerbuilder import ServiceMessageHandlerBuilder
from model.event import all_events, ListenedEvents
from msgbus import MBEasyContext

from helper import import_pydevd


def main():
    # type: () -> None

    loader_path = os.environ["LOADERCFG"]
    import_pydevd(loader_path, 9176, False)

    configurator = Configurator(loader_path)
    configs = configurator.read_loader_configs()

    logger = configurator.configure_logger()
    logger.setLevel(configs.log_level)

    mb_context = MBEasyContext(configs.component_name)
    message_bus = MbContextMessageBus(mb_context)
    service_message_handler_builder = ServiceMessageHandlerBuilder(logger, configs, message_bus)

    message_handler = MbContextMessageHandler(
        message_bus,
        configs.component_name,
        configs.component_name,
        "",
        service_message_handler_builder
    )

    events = all_events()
    events.remove(ListenedEvents.ORDER_MODIFIED)

    message_handler.subscribe_reentrant_events(events)

    logger.debug("Starting Message Bus [{}] ...".format(configs.component_name))
    return message_handler.handle_events()


if __name__ == "__main__":
    main()
