import os
import argparse
import time
from argparse import RawTextHelpFormatter

description = ("Agent Wrapper for Testing")

parser = argparse.ArgumentParser(description=description,
                                    formatter_class=RawTextHelpFormatter)
parser.add_argument('--num_reps', default="2")
parser.add_argument('--num_tests', default="10")

args = parser.parse_args()

repetitions = int(args.num_reps)
tests = int(args.num_tests)

invalid_threshold = 3

f = open("test_agent.txt","w")
f.write("")
f.close()

for test in range(tests):
    counter = 0
    invalid_count = 0
    print("\nRunning autonomous agent\n")
    # run autonomous agent
    while counter < repetitions:
        # f = open("test_log.txt","a")
        # f.write("\nautonomous,")
        # f.close()

        f = open("test_agent.txt","a")
        f.write("autonomous\n")
        f.close()

        os.system('python3 autonomous_agent.py')
        time.sleep(2)
        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("autonomous: scenario repetition " + str(counter))

    counter = 0
    invalid_count = 0

    print("\nRunning cautious human agent\n")
    # run cautious human agent
    while counter < repetitions:
        # f = open("test_log.txt","a")
        # f.write("\ncautious,")
        # f.close()

        f = open("test_agent.txt","a")
        f.write("cautious\n")
        f.close()
        # time.sleep(5)
        os.system('python3 cautious_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("cautious human: scenario repetition " + str(counter))


    counter = 0
    invalid_count = 0

    print("\nRunning aggressive human agent\n")
    # run aggressive human agent
    while counter < repetitions:
        f = open("test_agent.txt","a")
        f.write("aggressive\n")
        f.close()

        # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST
        # f.write("aggressive,")
        # f.close()
        # time.sleep(5)
        os.system('python3 aggressive_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("aggressive human: scenario repetition " + str(counter))

    counter = 0
    invalid_count = 0
    print("\nRunning reconfiguration agent\n")
    # run reconfiguration agent
    while counter < repetitions:
        f = open("test_agent.txt","a")
        f.write("reconfiguration\n")
        f.close()

        # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST
        # f.write("reconfiguration,")
        # f.close()
        # time.sleep(5)
        os.system('python3 reconfiguration_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("reconfiguration: scenario repetition " + str(counter))


import os
import argparse
import time
from argparse import RawTextHelpFormatter

description = ("Agent Wrapper for Testing")

parser = argparse.ArgumentParser(description=description,
                                    formatter_class=RawTextHelpFormatter)
parser.add_argument('--num_reps', default="2")
parser.add_argument('--num_tests', default="10")

args = parser.parse_args()

repetitions = int(args.num_reps)
tests = int(args.num_tests)

invalid_threshold = 3

f = open("test_agent.txt","w")
f.write("")
f.close()

for test in range(tests):
    counter = 0
    invalid_count = 0
    print("\nRunning autonomous agent\n")
    # run autonomous agent
    while counter < repetitions:
        # f = open("test_log.txt","a")
        # f.write("\nautonomous,")
        # f.close()

        f = open("test_agent.txt","a")
        f.write("autonomous\n")
        f.close()

        os.system('python3 autonomous_agent.py')
        time.sleep(2)
        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("autonomous: scenario repetition " + str(counter))

    counter = 0
    invalid_count = 0

    print("\nRunning cautious human agent\n")
    # run cautious human agent
    while counter < repetitions:
        # f = open("test_log.txt","a")
        # f.write("\ncautious,")
        # f.close()

        f = open("test_agent.txt","a")
        f.write("cautious\n")
        f.close()
        # time.sleep(5)
        os.system('python3 cautious_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("cautious human: scenario repetition " + str(counter))


    counter = 0
    invalid_count = 0

    print("\nRunning aggressive human agent\n")
    # run aggressive human agent
    while counter < repetitions:
        f = open("test_agent.txt","a")
        f.write("aggressive\n")
        f.close()

        # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST
        # f.write("aggressive,")
        # f.close()
        # time.sleep(5)
        os.system('python3 aggressive_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("aggressive human: scenario repetition " + str(counter))

    counter = 0
    invalid_count = 0
    print("\nRunning reconfiguration agent\n")
    # run reconfiguration agent
    while counter < repetitions:
        f = open("test_agent.txt","a")
        f.write("reconfiguration\n")
        f.close()

        # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST
        # f.write("reconfiguration,")
        # f.close()
        # time.sleep(5)
        os.system('python3 reconfiguration_agent.py')
        time.sleep(2)

        # with open("test_log.txt", "r") as f:
        #     last_line = f.readlines()[-1]
        #     valid = last_line.split(",")[-1]
        # if valid == "VALID" or invalid_count >= invalid_threshold:
        #     counter += 1
        # else:
        #     invalid_count += 1
        counter += 1
        print("reconfiguration: scenario repetition " + str(counter))


# run autonomous agent # f = open("test_log.txt","a") # f.write("\nautonomous,") # f.close() # with open("test_log.txt", "r") as f: #     last_line = f.readlines()[-1] #     valid = last_line.split(",")[-1] # if valid == "VALID" or invalid_count >= invalid_threshold: #     counter += 1 # else: #     invalid_count += 1 # run cautious human agent # f = open("test_log.txt","a") # f.write("\ncautious,") # f.close() # time.sleep(5) # with open("test_log.txt", "r") as f: #     last_line = f.readlines()[-1] #     valid = last_line.split(",")[-1] # if valid == "VALID" or invalid_count >= invalid_threshold: #     counter += 1 # else: #     invalid_count += 1 # run aggressive human agent # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST # f.write("aggressive,") # f.close() # time.sleep(5) # with open("test_log.txt", "r") as f: #     last_line = f.readlines()[-1] #     valid = last_line.split(",")[-1] # if valid == "VALID" or invalid_count >= invalid_threshold: #     counter += 1 # else: #     invalid_count += 1 # run reconfiguration agent # f = open("test_agent.txt","w") #***APPEND IN RANDOM_TEST # f.write("reconfiguration,") # f.close() # time.sleep(5) # with open("test_log.txt", "r") as f: #     last_line = f.readlines()[-1] #     valid = last_line.split(",")[-1] # if valid == "VALID" or invalid_count >= invalid_threshold: #     counter += 1 # else: #     invalid_count += 1