#!python

# Outer sheel for error handling

import os
from subprocess import Popen, PIPE
import sys

#from error_parsing import format_error

### Helper functions
#strip bad characters
def strip_bad(string):
    bad = ['(', ')', '&', '*', '^', '$', '<', '>', '\\', '/']
    return ''.join([ch for ch in string if ch not in bad])

#error formatting
def format_error(err):
    
    split_err = err.split('\n')

    if split_err[-2].find('SyntaxError') != -1:

        error_type = strip_bad(split_err[-2])
        error_info = strip_bad(split_err[-5])
        
    else:
        
        error_type = strip_bad(split_err[-2])
        error_info = strip_bad(split_err[-4])

    #return error_info, error_type
    return error_info + '--' + error_type

#ascii art
def ascii_error(err_message):

    #err_message = "You certainly messed something up."
    #if len(sys.argv) > 1:
    #    err_message = ' '.join(sys.argv[1:])

    one_line = False
    err_line_1 = err_message.split('--')[0]
    try:
        err_line_2 = err_message.split('--')[1]
    except:
        one_line = True
        err_line_2 = err_line_1
    
    if len(err_line_1) >= len(err_line_2):
        max_length = len(err_line_1)
        long_line_label = 1
    else:
        max_length = len(err_line_2)
        long_line_label = 2
    
    #Ascii Art from https://www.asciiart.eu/animals/cats
    #Anonymous artist
    ascii_cat = """
 ,_     _    / /
 |\\\_,-~/   /'
 / _  _ |    ,--.
(  @  @ )   / ,-'
 \\  _T_/-._( (
 /         `. \\
|         _  \\ |
 \\ \\ ,  /      |
  || |-_\\__   /
 ((_/`(____,-'
"""

    s1 = " " * 16 + "_" * (max_length + 6) 
    s2 = " " * 15 + "/" + " " * (max_length + 6) + "\\"

    if not one_line:
        if long_line_label == 1:
            length_diff = len(err_line_1) - len(err_line_2)
            s3 = " " * 15 + "|" + " " * 3 + err_line_1 + " " * 3 + "|"
            s4 = " " * 15 + "|" + " " * 3 + err_line_2 + " " * length_diff + " " * 3 + "|"
        elif long_line_label == 2:
            length_diff = len(err_line_2) - len(err_line_1)
            s3 = " " * 15 + "|" + " " * 3 + err_line_1 + " " * length_diff + " " * 3 + "|"
            s4 = " " * 15 + "|" + " " * 3 + err_line_2 + " " * 3 + "|"
    else:
        s34 = " " * 15 + "|" + " " * 3 + err_message + " " * 3 + "|"
    
    s5 = " " * 15 + "\\" + " " * 2 + "_" * (max_length + 4) + "/"
    s6 = " " * 14 + "/  /"

    if not one_line:
        speech_bubble = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + '\n' + s6
    else:
        speech_bubble = s1 + "\n" + s2 + "\n" + s34 + "\n" + s5 + '\n' + s6
    
    print("\n\n\n" + speech_bubble + ascii_cat + "\n\n\n")
    return




# Get command to run
if len(sys.argv) < 1:
    print("Enter command to run as command line argument")
    sys.exit()
else:
    command = sys.argv[1:]

# Run command and capture error
p = Popen(command, stderr=PIPE)
err = p.communicate()[1].decode("utf-8")

#Check if there is an error
if err.upper().find("ERROR") == -1:
    #no error
    ##os.system("python ascii_error.py 'Everything worked!'")
    ascii_error('Everything worked!')
else:
    #error
    formatted_error = format_error(err)
    ##os.system("python ascii_error.py " + formatted_error) 
    ascii_error(formatted_error)

