from freemarker.template import *
from freemarker.ext.jython import JythonWrapper
from java.io import FileWriter
from java.io import File
from java.lang import System

import os

from seasonutils import Season
import fall2005

season = Season()
fall2005.defineSeason(season)
cfg = Configuration()
cfg.setDirectoryForTemplateLoading(File(os.getcwd()));
wrapper = JythonWrapper();

cfg.setObjectWrapper(wrapper); 
dict = {}
dict["weeks"] = season.weeks;
temp = cfg.getTemplate("schedule.html.ftl");

out = FileWriter("schedule.html");
temp.process(wrapper.wrap(dict), out);
out.flush(); 
out.close();

lastweek = 0;
currweek = 0;
x = 0;
for week in season.weeks:
	x = 0;
	currweek +=1
	print "checking week: %d" % (currweek)
	for game in week.days[0].games:
#		if len(week.days[0].games) == 0 and len(week.days[1].games) == 0:
		if game.result != None:
			x = 1;
	if x == 0:
		break;
		
lastweek = currweek - 1;


dict = {}
dict["standings"] = standings = season.buildStandings();
dict["lastweekwithresults"] = lastweek;

temp = cfg.getTemplate("standings.html.ftl");
out = FileWriter("standings.html");
temp.process(wrapper.wrap(dict),out);
out.flush();
out.close();