# -*- coding: utf-8 -*-

import logging
import os

from helper import config_logger, import_pydevd
from messagehandler import MessageHandler
from msgbus import MBEasyContext
import cfgtools

from rupture_event_handler import RuptureEventHandler
from rupture_events import RuptureEvents
from rupture_repository import RuptureRepository
from rupture_service import RuptureService
from services import CleanRuptureThread

LOADER_CFG = os.environ["LOADERCFG"]
REQUIRED_SERVICES = "DeliveryPersistence"
SERVICE_NAME = "Ruptura"


def main():
    import_pydevd(LOADER_CFG, 9142)

    config_logger(LOADER_CFG, SERVICE_NAME)
    logger = logging.getLogger(SERVICE_NAME)
    config = cfgtools.read(LOADER_CFG)

    mb_context = MBEasyContext(SERVICE_NAME)

    rupture_repository = RuptureRepository(mb_context, logger)
    rupture_service = RuptureService(mb_context, logger, rupture_repository, config)
    event_handler = RuptureEventHandler(mb_context, logger, rupture_service)
    message_handler = MessageHandler(mb_context, SERVICE_NAME, SERVICE_NAME, REQUIRED_SERVICES, None)

    message_handler.set_event_handler(event_handler)
    message_handler.subscribe_queue_events([
        RuptureEvents.OrderModified,
    ])
    message_handler.subscribe_non_reentrant_events([
        RuptureEvents.FullRuptureRequest,
        RuptureEvents.CleanRupture,
    ])

    if config.find_value("Rupture.CleanRuptureTimeWindow.Enabled", "false").lower() == "true":
        clean_rupture_thread = CleanRuptureThread(config, rupture_service)
        clean_rupture_thread.start()

    rupture_service.load_daily_dishes_products()
    message_handler.handle_events()


if __name__ == "__main__":
    main()
