#!/usr/bin/env python

import sys
import pickle

print "<html>"
print "<head>"
print "<style>"
print "table {border-collapse: collapse; table-layout: fixed;}"
print "td {border-style: solid; border-width: 1px; border-color: grey;}"
print "</style>"
print "</head>"

print "<body>"

print "<h1>Imago test</h1>"

print "<pre>"

res = pickle.load(open(sys.argv[1], 'r'))

total = len(res)

failed = [(name,error) for (name, time, output, error, r_code) in res
          if r_code > 0]
print "failed tests:", str(len(failed)) + '/' + str(total)

for (name, error) in failed:
    print "    ", name

print ""

n = 0
t = 0
correct = 0

for (name, time, output, error, r_code) in res:
    if r_code > 0:
        continue
    n += 1
    t += time
    ref = open(name.split('.')[0] + '.txt').read()
    if output == ref:
        correct += 1
    else:
        #TODO find and record the error
        pass

print "average runtime:", (t/n)
print "correct:", str(correct) + '/' + str(n)

print "</pre>"

for (name, time, output, error, r_code) in res:
    print "<div>"
    print "<h2>" + name + "</h2>"
    print "<image src=\"" + name + "\" width=\"600\" style=\"float: right;\" />"
    print "<table>"
    output = output.split('\n')
    correct = open(name[:-4]+".txt").readlines()
    output = map(lambda s: s.split(), output)
    correct = map(lambda s: s.split(), correct)
    for i in range(len(output)):
        print "<tr>"
        for j in range(len(output[i])):
            if output[i][j] == correct[i][j]:
                print "<td><div style=\"width: 15px\" >{}</td>".format(output[i][j])
            else:
                print "<td><div style=\"background-color: red; \
                      width: 15px\">{}</td>".format(output[i][j])
        print "</tr>"
    print "</table>"
    print "<div style=\"clear: both;\"></div>"
    print "</div>"

print "</body>"
print "</html>"
