# testing.py
""" 
- This python script runs the user-specified script in Config_Files/global_config.py
"""
import time
import sys
import os

""" import necessary util functions """
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # add parent directory to path
import utils
from scripts import *
import Config_Files.global_config as gc

try:
    # Look for a running instance of a capture utility (defined in the Config_Files/global_config.py file)
    driver = utils.NintendoSwitchAPI().register_window(name=gc.capture_utility_name, nth=0)
    wx, wy = driver.get_window_rect()[:2]

    print()

    controller = utils.getController()
    print("Controller Config: {} found successfully!".format(controller["@name"]))
    del controller["@name"]
    

    print ("You have {} seconds to Enable Capture Mode on the MaxAim Di controller".format(gc.leeway))
    time.sleep(gc.leeway)
    
    driver.clear_console()
    
    try:
        # count the number of encounters
        encounters = 0
        while True:
            """ Run the script that is defined in the Config_Fibles/global_config.py file """
            # load a module dynamically from the scripts folder
            # and launch the 'run' function from that module
            # from https://stackoverflow.com/questions/17142090/how-to-get-reference-to-module-by-string-name-and-call-its-method-by-string-name
            print("Running Script: {}".format(gc.running_script_name))
            print("On Encounter {}".format(encounters))
            getattr(globals()[gc.running_script_name],'run')(driver,controller)
            encounters += 1
            # clear the console 
            driver.clear_console()
    except KeyboardInterrupt:
            print ('Interrupted')
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)
except Exception as e:
    print(e)
    try:
        sys.exit(0)
    except SystemExit:
        os._exit(0)

# testing.py
""" 
- This python script runs the user-specified script in Config_Files/global_config.py
"""
import time
import sys
import os

""" import necessary util functions """
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # add parent directory to path
import utils
from scripts import *
import Config_Files.global_config as gc

try:
    # Look for a running instance of a capture utility (defined in the Config_Files/global_config.py file)
    driver = utils.NintendoSwitchAPI().register_window(name=gc.capture_utility_name, nth=0)
    wx, wy = driver.get_window_rect()[:2]

    print()

    controller = utils.getController()
    print("Controller Config: {} found successfully!".format(controller["@name"]))
    del controller["@name"]
    

    print ("You have {} seconds to Enable Capture Mode on the MaxAim Di controller".format(gc.leeway))
    time.sleep(gc.leeway)
    
    driver.clear_console()
    
    try:
        # count the number of encounters
        encounters = 0
        while True:
            """ Run the script that is defined in the Config_Fibles/global_config.py file """
            # load a module dynamically from the scripts folder
            # and launch the 'run' function from that module
            # from https://stackoverflow.com/questions/17142090/how-to-get-reference-to-module-by-string-name-and-call-its-method-by-string-name
            print("Running Script: {}".format(gc.running_script_name))
            print("On Encounter {}".format(encounters))
            getattr(globals()[gc.running_script_name],'run')(driver,controller)
            encounters += 1
            # clear the console 
            driver.clear_console()
    except KeyboardInterrupt:
            print ('Interrupted')
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)
except Exception as e:
    print(e)
    try:
        sys.exit(0)
    except SystemExit:
        os._exit(0)

# testing.py - This python script runs the user-specified script in Config_Files/global_config.py import necessary util functions # add parent directory to path # Look for a running instance of a capture utility (defined in the Config_Files/global_config.py file) # count the number of encounters Run the script that is defined in the Config_Fibles/global_config.py file # load a module dynamically from the scripts folder # and launch the 'run' function from that module # from https://stackoverflow.com/questions/17142090/how-to-get-reference-to-module-by-string-name-and-call-its-method-by-string-name # clear the console