# Original done by Juan Felipe Beltran
# football app -- Can't tell whether inputs are coming from one site two times
# or two sites one time each.
import web, random


urls = ('/chat','chat_server',
		'/football','football_server')

# relates after the url /xxx to the server that should be called.

# What does this template.render thing. 
render = web.template.render('templates')


class Constants:
	x = 10
	chat = []
	users = {}
	colors = ['red','blue','green']
	football_messages = []


lastx = ""
class football_server:
	def GET(self):
		return render.football()

	def POST(self):
		form = web.input()
		action = form['action']

		# print "Action is: ", action
		if action == 'send':
			message = form['message']
			Constants.football_messages.append(message)
			if message =='clear':
				Constants.football_messages=[]
			return 'action/send' + str(len(Constants.football_messages))
		if action == 'check':
			if 2 == len(Constants.football_messages):
				x =  '<h1>' + generateoutcome(Constants.football_messages) + '</h1>'
				# Constants.football_messages = [] # erases things too quickly, which may be find for final deployment
				if (lastx != x):
					print "x is ", x
				return x
			else:
				if  3 == len(Constants.football_messages):
				# In this way, we don't just keep adding
				# to Constants.football_messages.
					Constants.football_messages = [Constants.football_messages[-1]]
				return 'action/check' + str(len(Constants.football_messages))

# mymessage is a standin for computing the results of two plays
def generateoutcome(mymessage):
   out = mymessage[0] + '_____' + mymessage[1]
   return out


# Unused for now
class chat_server:
	def GET(self):
		return render.chatroom("Change this to change the tab title!")

	def POST(self):
		form = web.input()
		ip = web.ctx['ip']
		action = form['action']

		if action == 'submit':
			if ip not in Constants.users.keys():
				Constants.users[ip]= Constants.colors.pop()
			message = form['message']
			if message.strip():
				message= message.replace('<',"&lt;").replace('>',"&gt;")
				element = "<div style='color:{0}'>{1}: {2}</div>".format(Constants.users[ip], ip, message)
				Constants.chat.append(element)
		return '<br>'.join(Constants.chat)


if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()



