import sys
import time
import threading
from argparse import ArgumentParser
from appBoot.logUtil import LogUtil

def parse_args():
    parser = ArgumentParser(description="Run as a service.")
    parser.add_argument("action", choices=["start", "stop", "restart", "status"],default="start")
    parser.add_argument("--type", choices=["daemon", "service"], default="service")
    
    return parser.parse_args()

def main():               
    if sys.platform != "win32" :
        from appService.service.daemonService import DaemonService
        args = parse_args()

        daemon_service = DaemonService()
        if args.action == "start":
            daemon_service.start()
        elif args.action == "stop":
            daemon_service.stop()
        elif args.action == "status":
            daemon_service.status()

    if sys.platform == "win32":
        import servicemanager
        from appBoot.service.winService import WinService,CommandLine

        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(WinService)
        servicemanager.StartServiceCtrlDispatcher()

    # if args.type == "service":
    #     from appService.service.winService import WinService,CommandLine
        
    #     if len(sys.argv) == 1:
    #         servicemanager.Initialize()
    #         servicemanager.PrepareToHostSingle(WinService)
    #         servicemanager.StartServiceCtrlDispatcher()
    #     else:
    #         CommandLine(WinService)
    # elif args.type == "daemon":
    #     from appService.service.daemonService import DaemonService

    #     daemon_service = DaemonService()
    #     if args.action == "start":
    #         daemon_service.start()
    #     elif args.action == "stop":
    #         daemon_service.stop()
    #     elif args.action == "status":
    #         daemon_service.status()

if __name__ == "__main__":
    main()
