import os

from msgbus import MBEasyContext

from configurator import Configurator
from helper import import_pydevd
from mbcontextmessagehandler import (
    MbContextMessageBus,
    MbContextMessageHandler,
)
from messagehandlerbuilder import DeviceManagerMessageHandlerBuilder


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

    loader_path = os.environ["LOADERCFG"]

    import_pydevd(loader_path=loader_path, port=9167)

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

    mb_context = MBEasyContext(name="DeviceManager")
    message_bus = MbContextMessageBus(mbcontext=mb_context)

    message_handler_builder = DeviceManagerMessageHandlerBuilder(
        configs=configs,
        message_bus=message_bus,
        mb_context=mb_context,
        logger=logger,
        input_logger=logger,
        callback_logger=logger,
    )
    message_handler = MbContextMessageHandler(
        message_bus=message_bus,
        service_name="DeviceManager",
        service_type="DeviceManager",
        required_services="",
        event_handler_builder=message_handler_builder,
    )

    logger.info("Device Manager started...")

    return message_handler.handle_events()
