import os
import subprocess
import sys
import signal

from ..debug import dbprint, db_exec_info

from .syntaxhi import color_line

from .tester import runWithInput

# TODO: Handle infinite loops

print_src_msg = '''
+------------------------+
| Printing File Contents |
+------------------------+
'''

run_in_interpreter_msg = '''
+------------------------------+
| Launching Python Interpreter |
|  (Press Ctrl+D to quit...)   |
+------------------------------+    
'''

run_tests_msg = '''
+---------------------------+
| Calling Tests For Session |
+---------------------------+
'''

# Display the contents of the student's homework file
# for manual inspection and partial credit. Displays 
# with line numbers for easy reference.
def printCode(file_path):
    print(print_src_msg)

    try:
        # Check to see if we're running linux (syntax highlighting only supports Linux terminals)
        linux = (os.name == "posix")
            
        print(file_load_msg)

        fin = open(file_path,'r')
        contents = fin.readlines()
        fin.close()
        
        # Print out the code line-by-line
        for i in range(len(contents)):
            if linux:
                print(str(i+1).rjust(4,'_'),': ', syntaxhi.color_line(contents[i]), end = '')
            else:
                print(str(i+1).rjust(4,'_'),': ', contents[i], end = '')
            
        print('\n')
        print('\nFile: ',file_path)
    except:
        print('Failed to open file: {}\n'.format(file_path))
        dbprint(db_exec_info())

def runCode(file_path, interactive=False):

    # Store current directory so we can switch back to it
    current_dir = os.getcwd()

    file_dir, file_name = os.path.split(file_path)
    os.chdir(file_dir)

    # Store the original handler and set a new one
    original_sigint = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, _graceful_handler)

    # Call the student code
    if interactive:
        subprocess.call([sys.executable, '-i', file_name])
    else:
        subprocess.call([sys.executable, file_name])

    # Restore the handler
    signal.signal(signal.SIGINT, original_sigint)

    # Return to original directory
    os.chdir(current_dir)

def runInteractive(file_path):
    print(run_in_interpreter_msg)
    runCode(file_path, interactive=True)

def runTests(file_path, test):
    print(run_tests_msg)
    mod_load_error = False

    # Store the original handler and set a new one
    original_sigint = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, _graceful_handler)

    out, err = runWithInput(file_path, open(test).read())
    if out:
        out = out.replace('>>>','\n').replace('...','\n')
    if err:
        err = err.replace('>>>','\n').replace('...','\n')
    print('Output from', test, '\n------')
    print(out)
    if not err.replace('\n','').replace(' ','')=='':
        print('Errors from', test, '\n------')
        print(err)

    # Restore the handler
    signal.signal(signal.SIGINT, original_sigint)

def _graceful_handler(signum, frame):
    print("\n\033[91mLooks like you pushed Ctrl-C.\033[0m")
    print("Feel free to push it again if you actually want to quit the script.\n")


@@ -478,20 +478,16 @@
 -------+
-    
 %0D%0A'''%0D%0A%0D
@@ -715,17 +715,16 @@
 Displays
- 
 %0D%0A# with
@@ -963,52 +963,8 @@
 x%22)%0D
-%0A            %0D%0A        print(file_load_msg)%0D
 %0A%0D%0A 
@@ -1054,24 +1054,16 @@
 lose()%0D%0A
-        
 %0D%0A      
@@ -1194,33 +1194,33 @@
 r(i+1).rjust(4,'
-_
+ 
 '),': ', syntaxh
@@ -1216,17 +1216,8 @@
  ', 
-syntaxhi.
 colo
@@ -1307,17 +1307,17 @@
 just(4,'
-_
+ 
 '),': ',
@@ -1341,28 +1341,16 @@
  = '')%0D%0A
-            
 %0D%0A      
@@ -3219,14 +3219,12 @@
 cript.%5Cn%22)%0D%0A
-%0D%0A

Fix errors in syntax highlighting