
from pyexpat import model
from time import sleep

from sqlalchemy import false
import Model.model_manager as model_manager
import persistence
from utility.app_logging import logger_name, init as init_logging
import logging
import View.view_manager as view


logger = logging.getLogger(logger_name)
__is_running__ = False
__stop_app__ = False
def init() :


    init_logging()


    logger.debug(__name__)


    logger.info("")
    logger.info("")
    logger.info("")
    logger.info("-------         welcome to Open Internet Service Provider         -------")
    logger.info("")
    logger.info("")
    logger.info("")
    logger.info("     ██████  ██████  ███████ ███    ██     ██ ███████ ██████    ")
    logger.info("    ██    ██ ██   ██ ██      ████   ██     ██ ██      ██   ██   ")
    logger.info("    ██    ██ ██████  █████   ██ ██  ██     ██ ███████ ██████    ")
    logger.info("    ██    ██ ██      ██      ██  ██ ██     ██      ██ ██        ")
    logger.info("     ██████  ██      ███████ ██   ████     ██ ███████ ██        ")
    logger.info("")
    logger.info("")

    logger.info(" --- INITIALIZATION PHASE ---")

    logger.info("initializing Persistence Engine (Database)...")

    persistence.init()
    model_manager.init()
    logger.info("Persistence Engine initialization Done ")


    logger.info("initializing View Engine - Rest API")
    view.init()
    logger.info("View Engine - Rest API initialization Done ")


    logger.info(" --- END OF INITIALIZATION PHASE ---")

import signal

def keyboardInterruptHandler(signal, frame):
    logger.info("KeyboardInterrupt (ID: {}) has been caught. Cleaning up...".format(signal))
    stop()

signal.signal(signal.SIGINT, keyboardInterruptHandler)



def main() :
    logger.info("Running View Engine - Rest API...")
    view.run()
    global __is_running__
    global __stop_app__
    __is_running__ = True


    logger.info("Open ISP is now Running ! ")
    while(not __stop_app__) :
        sleep(0.5)


    logger.debug("exiting main loop")

    __is_running__ = False



def stop() :
    view.stop()
    global __stop_app__
    global __is_running__
    __stop_app__ = True
    logger.info("Application Stopped, now exiting...")



def isRunning() :
    global __is_running__
    return __is_running__

