import logging
import os

import cfgtools
from helper import import_pydevd, config_logger
from mbcontextmessagehandler import MbContextMessageBus, MbContextMessageHandler
from messagehandlerbuilder import NKDSCtrlMessageHandlerBuilder
from msgbus import MBEasyContext

def main():
    import_pydevd(os.environ["LOADERCFG"], 9135, False)
    config_logger(os.environ["LOADERCFG"], "NKDSCTRL")

    logger = logging.getLogger("NKDSCTRL")
    logger.setLevel(logging.DEBUG)
    proxy_comp_name = "KdsController"

    mb_context = MBEasyContext("NKDSCTRL")
    message_bus = MbContextMessageBus(mb_context)

    required_services = "ProductionSystem|RemoteOrder"
    message_handler = MbContextMessageHandler(
        message_bus,
        "NKDSCTRL",
        "NKDSCTRL",
        required_services,
        None
    )
    config = cfgtools.read(os.environ["LOADERCFG"])

    message_handler_builder = NKDSCtrlMessageHandlerBuilder(
        proxy_comp_name,
        mb_context,
        message_bus,
        config,
        logger
    )
    message_handler.event_handler_builder = message_handler_builder

    return message_handler.handle_events()
