import os
import webapp2
import jinja2
from rot import rot

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True) # -> need to add this somehow
res = ""
class Handler(webapp2.RequestHandler):

	def write(self, *a, **kw):
		self.response.out.write(*a, **kw)

	def render_str(self, template, **params):
		t = jinja_env.get_template(template)
		return t.render(params)

	def render(self, template, **kw):
		self.write(self.render_str(template, **kw))

class MainPage(Handler):

	def get(self):
		self.render('/rot13.html', result=res)

	def post(self):
		content = self.request.get("text")
		if content and not content.isspace():
			res = rot(content)
		else:
			res = "No input found. Please try again."
		self.redirect('/rot13.html', result=res)

app = webapp2.WSGIApplication([('/', MainPage),], debug=True)