'''
Code to accept ride submissions that are generated by the initial form.
SubmissionAck handles the initial user submission.

'''
__all__ = (
    "SubmissionAck",
    "SubmissionDisplay",
    "SubmissionCancel",
    "RideCancel",
)

import os
import jinja2

import cgi 
import webapp2
import jinja2

import string
import datetime

from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.api import mail

from ridelib.email import *

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True
)

from ridelib.ridedata import *

# This is the first place that we land when they press 'submit'
class SubmissionAck(webapp2.RequestHandler):
    def post(self):

        # Should there be some input validation here?
        ridename = cgi.escape(self.request.get('name'))
        ridedate = cgi.escape(self.request.get('date'))
        ridestart = cgi.escape(self.request.get('startinglocation'))
        ridedescription = cgi.escape(self.request.get('description'))

        # Convert the date into a native object so that it can 
        # Get stuffed into the database entry
        
        dt = datetime.datetime.strptime(ridedate, "%m/%d/%Y")

        # print repr(dt)


       # Get their email address from their google info.
        user = users.get_current_user()

        if user:
            greeting = 'Ride Submission by ' + user.nickname()
        else:
            greeting = 'You must be logged in to get a confirmation email'
            self.redirect(users.create_login_url(self.request.uri))

        # Load it up into a NDB entry.
        listing = RideDataItem(
            version = 3,
            name = ridename,
            startdate = dt.date(),
            startlocation = ridestart,
            description = ridedescription,
            submitter_email = user.email()
            )

        # Now its time to stash this data into the database and retrieve the key
        # So that it can be shared with the user in URL form.        
        key = listing.put()
        ridedbkey = key.urlsafe()
   
        template_values = {
            'greeting': greeting,
            'ridename': ridename,
            'ridedate': ridedate,
            'ridestart': ridestart,
            'ridedescription': ridedescription,
            'ridedbkey': ridedbkey,
        }

        template = JINJA_ENVIRONMENT.get_template('submissionack.html')
        self.response.write(template.render(template_values))

        email_submission_confirm(key)
        

# Here is a class that can be used to display a database entry.
class SubmissionDisplay(webapp2.RequestHandler):
    def get(self):
        textkey = self.request.get('dbkey')

        # There should be some error checking here.
     
        entrykey = ndb.Key(urlsafe=textkey)        
        ridelisting = entrykey.get()
       
        template_values = {
            'ridename': ridelisting.name,
            'ridestartlocation': ridelisting.startlocation,
            'ridestartdate': ridelisting.startdate,
            'ridedescription': ridelisting.description,
            'creation': ridelisting.created,
            'modified': ridelisting.modified,
            'ridedbkey': textkey,
        }
 
        template = JINJA_ENVIRONMENT.get_template('submissionview.html')
        self.response.write(template.render(template_values))

# For future use.
class SubmissionDisplayAll(webapp2.RequestHandler):
    def get(self):
         self.response.write('<html><body>Placeholder</html></body>')

# There needs to be a path for the original ride submission to 
# be cancelled by the user after before it's approved. 
class SubmissionCancel(webapp2.RequestHandler):
    def get(self):
         self.response.write('<html><body>Submission Cancelled</html></body>')

# There needs to be a path for the original ride submission to 
# be cancelled by the user after before it's approved. 
class RideCancel(webapp2.RequestHandler):
    def get(self):
         self.response.write('<html><body>Ride Cancelled</html></body>')
















