import web, random


urls = ('/chat','chat_server',
		'/foobar','foobar_server')
render = web.template.render('templates')


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


class foobar_server:
	def GET(self):
		return render.foobar()

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

		print action
		if action == 'send':
			message = form['message']
			Constants.foobar_messages.append(message)
			if message =='clear':
				Constants.foobar_messages=[]
			return ''
		if action == 'check':
			if 'foo' in Constants.foobar_messages and 'bar' in Constants.foobar_messages:
				return '<h1>FOOBAR!</h1>'
			else:
				return ''



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()



