import menusystem
import unittest
import os
import sys
import time

from subprocess import check_output
from subprocess import CalledProcessError

import ClydeLog
import ClydeUnitTest
import TestRig

RIG_PORT = ['/dev/ttyACM0', '/dev/ttyACM1']

testid = 0

# Handler functions
def run_test_session(names, board):
  print("")
  input = get_input("Do you want to run the first test? (Y/n) ")
  next = input == 'y' or input == 'Y' or input == ''
  print("")
  while next:
    run_tests(names, board)
    input = get_input("Do you want to run the next test? (Y/n) ")
    next = input == 'y' or input == 'Y' or input == ''
    print("")

def run_main_test_session(first_names, second_names, board):
  setMainPins()
  #loadSerial()
  print("Found test rig: %s" % 'MAIN');
  #print("Last serial: %s" % lastSerial);
  input = get_input("Do you want to run the first test? (Y/n) ")
  next = input == 'y' or input == 'Y' or input == ''
  print("")
  while next:
    run_main_tests(first_names, second_names, board)
    input = get_input("Do you want to run the next test? (Y/n) ")
    next = input == 'y' or input == 'Y' or input == ''
    print("")
    
def get_input(prompt):
  while True:
    s = raw_input(prompt)
    if set(s).issubset("ynYNqQ") or s == "":
      return s
    print("Please answer 'y' for yes, or 'n' for no.")
    
def run_tests(names, board):
  global testid
  
  names = names.split();

  if (TestRig.rig.board is not None):
    TestRig.rig.reset();
    
  allTests = unittest.TestSuite()
  
  for name in names:
    module = __import__(name)
    tests = unittest.defaultTestLoader.loadTestsFromModule(module)
    allTests.addTests(tests)
  
  testid += 1
  runner = ClydeUnitTest.ClydeTestRunner(testid)
  result = runner.run(board, allTests)
  
  if (TestRig.rig.board is not None):
    TestRig.rig.reset()
    TestRig.rig.led('GREEN' if result.wasSuccessful() else 'RED', 'ON')

def run_main_tests(first_names, second_names, board):
  global testid
  
  #first set of tests
  first_names = first_names.split();
    
  allTests = unittest.TestSuite()
  
  for name in first_names:
    module = __import__(name)
    tests = unittest.defaultTestLoader.loadTestsFromModule(module)
    allTests.addTests(tests)
    
  testid += 1 
  runner = ClydeUnitTest.ClydeTestRunner(testid)
  result = runner.run(board, allTests)
 
  if not result.wasSuccessful():
    if TestRig.rig.board is not None:
      TestRig.rig.reset()
      TestRig.rig.led('RED', 'ON')
    return
  
  start = int(round(time.time() * 1000))
  found = False
  timeout = 15000.0
  while not found and (int(round(time.time() * 1000)) - start < timeout):
    sys.stdout.write("\rLoading [ %.3fs ]" % (int(int(round(time.time() * 1000)) - start) / 1000.0))
    sys.stdout.flush()
    output = check_output(["lsusb"])
    if "1d50:609f" in output:
      found = True
    time.sleep(1)
  
  sys.stdout.write("\n\n")
  
  time.sleep(2)
  TestRig.rig.connect(RIG_PORT)

  #second set of tests
  second_names = second_names.split();

  if (TestRig.rig.board is not None):
    TestRig.rig.reset();
    
  allTests = unittest.TestSuite()
  
  for name in second_names:
    module = __import__(name)