'''Input: List of ground truth test images, 
		  directory containing test gt images,
		  directiry containing predicted images,
		  List of images generated by corresponding predicted expression(optional),
		  number of samples that need to be displayed(optional, default=240),
		  visualization tree on/off(optional, default=off)

	Output: html file that displays gt, predicted pairs
'''
import os, sys
import numpy as np
import math
numtables = 3

def genhtml(gtlist, gtdir, preddir, predlist=None, numexs=500, vistree=False):
	if predlist == None:
		predlist = gtlist
	assert len(predlist) == len(gtlist)
	randind = np.random.permutation(range(len(gtlist)))[:numexs]
	filepath = os.path.dirname(preddir)+'/index.html'
	fp = open(filepath, 'w')
	fp.write('''
			<!DOCTYPE html>
			<html lang="en">
			<head>
			<meta charset="UTF-8">
			<title>Vanilla RNN: 7step with 500k Samples</title>
			</head>
			<body>
			<h2>7 step programs (trained with 500k samples)</h2>
			''')
	exspertable = math.ceil(numexs/numtables)
	count = 0
	height = str(600)
	width = str(240)
	for i in range(numtables):
		fp.write('''
			<table width="200" border="5" style="float:left;">
			<tr>
			<th>S.No.</th>
			<th>GROUND TRUTH</th>
			<th>PREDICTED IMAGE</th>
			</tr>
			''')
		for j in range(i*exspertable, min((i+1)*exspertable, numexs)):
			count = count+1
			if 'err' in predlist[randind[j]]:
				color = ' bgcolor=red'
			else:
				color=''
			fp.write('''
				<tr>
				<td height="''' + height + '" width="' + width + '"' + color + '>' + str(count) + '</td><td height="' + height + '" width="' + width + '"><img src=''' + 'gt' + '/' + gtlist[randind[j]]+ ' alt='+ gtlist[randind[j]]+' width="'+ width +'"/> </td>'
				+'<td  height="' + height + '" width="' + width + '"><img src=' + 'pred'+'/'+predlist[randind[j]]+ ' alt='+ predlist[randind[j]] + ' width="'+ width +'''"/> </td>
				</tr>''')


if __name__ == '__main__':
	gtdir = "/home/rishabh/Documents/experiment/large7step/gt"
	preddir = "/home/rishabh/Documents/experiment/large7step/pred"
	# fp = open("/home/rishabh/Documents/experiment/3step/result.txt", 'r')
	gtlist = []
	predlist = []
	for filename in os.listdir(gtdir):
		if ".png" in filename and 'vis'in filename:
			gtlist.append(filename)
			if os.path.isfile(preddir+'/'+filename):
				predlist.append(filename)
			else:
				predlist.append('err'+filename)
	# for line in fp:
	# 	temp = line.strip('\n').split(', ')
	# 	gtlist.append(temp[0])
	# 	predlist.append(temp[1])
	genhtml(gtlist, gtdir, preddir, predlist=predlist)
