# general python libraries
import os
import urllib
import datetime
import time
import re

# web development libraries
import jinja2
import webapp2
import urllib

# GAE libraries
from google.appengine.api import files
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.ext import gql

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.blobstore import BlobInfo

from gaesessions import get_current_session

# custom libraries
from app.models import *
from app.helper_methods import *
from app.appengine_config import *
from app.user_parser import *


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

# update jinja filter:
JINJA_ENVIRONMENT.filters['urlencode'] = do_urlencode
JINJA_ENVIRONMENT.filters['validateNetID'] = validateNetID

# default lenth of time to wait before redirects
TIME_SLEEP = 0.1


class LoginPage(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'current_user': getCurrentUser(self)
        }
        template = JINJA_ENVIRONMENT.get_template('login.html')
        self.response.write(template.render(template_values))

    def post(self):
        # eventually don't allow multiple login.
        session = get_current_session()

        query_params = build_query_params(self)
        query = object_query(User, query_params)
        users = query.fetch()
        # blank submissions are unauthorized
        if len(query_params) == 0:
            self.redirect('login/unauthorized?'+urllib.urlencode(query_params))
        elif len(users) == 0:
            self.redirect('login/unauthorized?'+urllib.urlencode(query_params))
        else:
             # for hacking purposes only
            #user = User(netID="admin", user_type="administrator")
            #user.put()
            user = query.get()
            session['user'] = user
            self.redirect('/')

class LoginUnauthorizedPage(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        if 'netID' in query_params:
            template_values = {
                'netID':query_params['netID']
            }
        else:
            template_values = {}
        template = JINJA_ENVIRONMENT.get_template('login_unauthorized.html')
        self.response.write(template.render(template_values))

class LogoutPage(webapp2.RequestHandler):

    def post(self):

        query_params = build_query_params(self)
        template_values = {
            'netID':query_params['netID'],
            'current_user': getCurrentUser(self)
        }

    def get(self):
        session = get_current_session()
        user = getCurrentUser(self)
        if user == None:
            self.redirect('login')
        else:
            session.terminate()
            self.redirect('/')

class MainPage(webapp2.RequestHandler):

    def get(self):
        user = getCurrentUser(self)
        if user == None:
            (self.redirect('/login'))
        else:
            template_values = {
                'current_user': getCurrentUser(self),
            }
            template = JINJA_ENVIRONMENT.get_template('index.html')
            self.response.write(template.render(template_values))
#        self.response.write(user.student_netIDs)
class About(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('about.html')
        self.response.write(template.render(template_values))

class Contact(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('contact.html')
        self.response.write(template.render(template_values))


class SignupFormPage(webapp2.RequestHandler):
    # get information from the user
    def get(self):
        current_user = getCurrentUser(self)
        query_params = {'form_type': 'signup'}

        if current_user.user_type == 'faculty':
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

        elif current_user.user_type == 'student':
            # alert box if advisor netid does not exist.
            if not alreadySubmitted(self, current_user.netID, 'signup'):
                
                query_params2 = build_query_params(self)
                invalid_netID = False

                if len(query_params2) > 0:
                    if query_params2['failed']:
                        invalid_netID = True

                template_values = {
                    'current_user': getCurrentUser(self),
                    'url_linktext': getLoginStatus(self.request.uri)[1],
                    'invalid_netID': invalid_netID,
                    }
                template = JINJA_ENVIRONMENT.get_template('signup_form.html')
                self.response.write(template.render(template_values))
       
        else:
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

    def post(self):
        # get the information entered by user
        sf = SignupForm(student_name=self.request.get('student_name'),
                        form_type= 'signup',
                        class_year = int(self.request.get('class_year')),
                        coursework = self.request.get('coursework'),
                        project_title = self.request.get('project_title'),
                        description = self.request.get('description'),

                        advisor_name = self.request.get('advisor_name'),
                        advisor_netID = self.request.get('advisor_netID'),
                        advisor_department = self.request.get('advisor_department'),
                        student_netID = self.request.get('student_netID_hidden'),
                        )
        
        advisor_verified = validateNetID(sf.advisor_netID)
        
        if advisor_verified:
            sf.put()
            current_user = getCurrentUser(self)

            advisor_netID = sf.advisor_netID
            current_user.advisor_netID = advisor_netID
            current_user.put()

            query_params = {'netID': advisor_netID}
            query = object_query(Faculty, query_params)
            user_faculty = query.get()
            user_faculty.student_requests.append(sf.student_netID)
            user_faculty.student_requests.sort
            user_faculty.put()

            query_params2 = {'student_netID':sf.student_netID, 'form_type':sf.form_type}
            time.sleep(TIME_SLEEP)
            self.redirect('/forms/view?' + urllib.urlencode(query_params2))

        else:
            query_params = {'failed': True}
            self.redirect('/forms/signup?' + urllib.urlencode(query_params))
            
            
class CheckPointFormPage(webapp2.RequestHandler):

    def get(self):
        current_user = getCurrentUser(self)

        query_params = {'form_type': 'checkpoint'}
        if current_user.user_type == 'faculty':
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

        elif current_user.user_type == 'student':
             if not alreadySubmitted(self, current_user.netID, 'checkpoint'):
                 template_values = {
                     'current_user': getCurrentUser(self),
                     'url_linktext': getLoginStatus(self.request.uri)[1],
                     }
                 template = JINJA_ENVIRONMENT.get_template('checkpoint_form.html')
                 self.response.write(template.render(template_values))
                 
        else:
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

    def post(self):
        cpf = None
        current_user = getCurrentUser(self)
        if current_user.user_type == "student":
            cpf = CheckpointForm(student_name = self.request.get('student_name'),
                                 form_type = 'checkpoint',
                                 project_title = self.request.get('project_title'),
                                 advisor_name = self.request.get('advisor_name'),
                                 number_of_meetings = int(self.request.get('number_of_meetings')),
                                 student_self_assessment = self.request.get('student_self_assessment'),
                                 student_netID = self.request.get('student_netID_hidden'),
                                 )

        elif current_user.user_type  == "faculty":
            query_params = {'student_netID': self.request.get('student_netID_hidden'),'form_type':'checkpoint'}
            query = object_query(Form, query_params)
            cpf = query.get()
            cpf.advisor_read = self.request.get('advisor_read')
            cpf.advisor_more_meetings = self.request.get('advisor_more_meetings')
            cpf.student_progress_eval = self.request.get('student_progress_eval')
            cpf.advisor_comments = self.request.get('advisor_comments')
                
        cpf.put()
        query_params2 = {'student_netID':cpf.student_netID, 'form_type':cpf.form_type}
        time.sleep(TIME_SLEEP)
        self.redirect('/forms/view?' + urllib.urlencode(query_params2))


class FebruaryFormPage(webapp2.RequestHandler):

    def get(self):
        session = get_current_session()
        current_user = getCurrentUser(self)
       
        query_params = {'form_type': 'february'}
        if current_user.user_type == 'faculty':
            self.redirect('/forms/select?' + urllib.urlencode(query_params))
        
        elif current_user.user_type == 'student':
             if not alreadySubmitted(self, current_user.netID, 'february'):
                 template_values = {
                     'current_user': getCurrentUser(self),
                     'url_linktext': getLoginStatus(self.request.uri)[1],
                     }
                 template = JINJA_ENVIRONMENT.get_template('february_form.html')
                 self.response.write(template.render(template_values))
        
        else:
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

    def post(self):
        ff = None
        current_user = getCurrentUser(self)
        if current_user.user_type == 'student':
            ff = FebruaryForm(student_name = self.request.get('student_name'),
                              project_title = self.request.get('project_title'),
                              advisor_name = self.request.get('advisor_name'),
                              advisor_netID = self.request.get('advisor_netID'),
                              number_of_meetings = int(self.request.get('number_of_meetings')),
                              student_comments = self.request.get('student_comments'),
                              student_netID = self.request.get('student_netID_hidden'),
                              form_type = 'february',
                              )
            ff.put()

        elif current_user.user_type == 'faculty':
            query_params = {'student_netID': self.request.get('student_netID_hidden'), 'form_type':'february'}
            query = object_query(Form, query_params)
            ff = query.get()
            ff.advisor_read = self.request.get('advisor_read')
            ff.advisor_more_meetings = self.request.get('advisor_more_meetings')
            ff.student_progress_eval = self.request.get('student_progress_eval')
            ff.advisor_comments = self.request.get('advisor_comments')
            ff.put()
        
        query_params2 = {'student_netID':ff.student_netID, 'form_type':ff.form_type}
        time.sleep(TIME_SLEEP)
        self.redirect('/forms/view?' + urllib.urlencode(query_params2))

class SecondReaderFormPage(webapp2.RequestHandler):

    def get(self):
        current_user = getCurrentUser(self)

        query_params = {'form_type': 'second_reader'}
        if current_user.user_type == 'faculty':
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

        elif current_user.user_type == 'student':
             if not alreadySubmitted(self, current_user.netID, 'second_reader'):
                
                 query_params2 = build_query_params(self)
                 invalid_netID = False

                 if len(query_params2) > 0:
                     if query_params2['failed']:
                         invalid_netID = True
                 
                 template_values = {
                     'current_user': current_user,
                     'url_linktext': getLoginStatus(self.request.uri)[1],
                     'invalid_netID': invalid_netID,
                     }
                 template = JINJA_ENVIRONMENT.get_template('second_reader_form.html')
                 self.response.write(template.render(template_values))
                 
        else:
            self.redirect('/forms/select?' + urllib.urlencode(query_params))

    def post(self):

        current_user = getCurrentUser(self)
        srf = SecondReaderForm(student_name=self.request.get('student_name'),
                               student_netID = self.request.get('student_netID_hidden'),
                               class_year =int(self.request.get('class_year')),
                               project_title = self.request.get('project_title'),
                               description = self.request.get('description'),
                               sr_name = self.request.get('sr_name'),
                               sr_netID = self.request.get('sr_netID'),
                               sr_department = self.request.get('sr_department'),
                               form_type = 'second_reader',
                               )

        advisor_verified = validateNetID(srf.sr_netID)
        
        if advisor_verified:
            srf.put()
            
            current_user.second_reader_netID = srf.sr_netID
            current_user.put()
            
            query_params = {'netID': srf.sr_netID}
            query = object_query(Faculty, query_params)
            user_faculty = query.get()
            user_faculty.second_reader_requests.append(srf.student_netID)
            user_faculty.put()
            
            query_params2 = {'student_netID':srf.student_netID, 'form_type':srf.form_type}
            time.sleep(TIME_SLEEP)
            self.redirect('/forms/view?' + urllib.urlencode(query_params2))
        
        else:
            query_params = {'failed': True}
            self.redirect('/forms/second_reader?' + urllib.urlencode(query_params))

class Unsubmitted(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('unsubmitted.html')
        self.response.write(template.render(template_values))

class ApproveAdvisees(webapp2.RequestHandler):
    def get(self):
        

        current_user = getCurrentUser(self)
        if current_user.user_type == "student":
            self.redirect('/denied/')

        template_values = {
            'current_user': getCurrentUser(self),
            'url_linktext': getLoginStatus(self.request.uri)[1],
        }
        template = JINJA_ENVIRONMENT.get_template('approve.html')
        self.response.write(template.render(template_values))

    def post(self):
       

        current_user = getCurrentUser(self)

        approval = self.request.get('agreement')
        student = self.request.get('chosen_student')
        
        if student in current_user.student_requests:
            if approval == 'yes':
                current_user.student_netIDs.append(student)
                current_user.student_requests.remove(student)
                current_user.put()

            else:
                current_user.student_requests.remove(student)
                current_user.put()
                query_params = {'student_netID': student}
                query = object_query(Form, query_params)
                query = query.fetch()
                query_length = len(query)
                for q in range(0, query_length):
                    form = query[q]
                    form.key.delete()


        elif student in current_user.second_reader_requests:
            if approval == 'yes':
                current_user.second_reader_netIDs.append(student)
                current_user.second_reader_requests.remove(student)
                current_user.put()

            else:
                current_user.second_reader_requests.remove(student)
                current_user.put()
                query_params = {'student_netID': student, 'form_type': 'second_reader'}
                query = object_query(Form, query_params)
                form = query.get()
                form.key.delete()

        time.sleep(TIME_SLEEP)
        self.redirect('/logout')  


class FormView(webapp2.RequestHandler):
    # shows the results of what has been submitted
    def get(self):
        # calls helper method
        current_user = getCurrentUser(self)
        query_params = build_query_params(self)
        if current_user.user_type == 'student':
            if current_user.netID != query_params['student_netID']:
                self.redirect('/')
        elif current_user.user_type == 'faculty':
            if query_params['student_netID'] not in current_user.student_netIDs and query_params['student_netID'] not in current_user.second_reader_netIDs:
                self.redirect('/unauthorized')
        query = object_query(Form, query_params)
        form = query.get()
        template_values = {
            'form': form,
            'current_user': getCurrentUser(self),
            'url_linktext': getLoginStatus(self.request.uri)[1],
        }
        template = JINJA_ENVIRONMENT.get_template('view_%s.html' % form.form_type)
        self.response.write(template.render(template_values))

class FormQuery(webapp2.RequestHandler):
    # user inputs what he/she wants to query
    def get(self):
        template_values = {
            'current_user': getCurrentUser(self),
            'url_linktext': getLoginStatus(self.request.uri)[1],
        }
        template = JINJA_ENVIRONMENT.get_template('query.html')
        self.response.write(template.render(template_values))

    def post(self):
        query_params = build_query_params(self)
        self.redirect('/forms/query_results?' + urllib.urlencode(query_params))

class FormQueryResults(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        query = object_query(Form, query_params)
        # sort query results (sort_by is a link selected by the user in query_results.html)
        sort_by = self.request.get('sort_by')
        if sort_by == 'form_type':
            query = query.order(Form.form_type, Form.student_netID, Form.student_name)
        if sort_by == 'student_netID':
            query = query.order(Form.student_netID, Form.student_name)
        if sort_by == 'student_name':
            query = query.order(Form.student_name, Form.student_netID)
        if sort_by == 'advisor_netID':
            query = query.order(Form.advisor_netID, Form.student_netID, Form.student_name)
        if sort_by == 'advisor_name':
            query = query.order(Form.advisor_name, Form.student_netID, Form.student_name)

        current_user = getCurrentUser(self)
        if current_user.user_type == 'student':
            query2 = query.filter(Form.student_netID == current_user.netID)
            forms = query2.fetch()
        elif current_user.user_type == 'faculty':
            query2 = query.filter(Form.advisor_netID == current_user.netID)
            forms = query2.fetch()
        else:
            forms = query.fetch()


        template_values = {
            'forms':forms,
            'current_user': getCurrentUser(self),
            'url_linktext': getLoginStatus(self.request.uri)[1],
        }
        template = JINJA_ENVIRONMENT.get_template('query_results.html')
        self.response.write(template.render(template_values))

class FormQueryView(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        query = object_query(Form, query_params)
        form = query.fetch(1)[0]
        template_values = {
            'form':form,
            'current_user': getCurrentUser(self),
            'url_linktext': getLoginStatus(self.request.uri)[1],
        }
        template = JINJA_ENVIRONMENT.get_template('view_%s.html' % query_params['form_type'])
        self.response.write(template.render(template_values))

class FormDelete(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        query  = object_query(Form, query_params)
        form = query.fetch(1)[0]
        form.key.delete()
        self.redirect('/forms/query')

class FormInvalid(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        template_values = {
            'current_user': getCurrentUser(self),
            'form_type': query_params['form_type']
        }
        template = JINJA_ENVIRONMENT.get_template('form_invalid.html')
        self.response.write(template.render(template_values))

class NewFile(webapp2.RequestHandler):

    def get(self):

        upload_url = blobstore.create_upload_url('/files/upload')
        template_values = {
            'current_user': getCurrentUser(self),
            'upload_url': upload_url,
        }
        template = JINJA_ENVIRONMENT.get_template('upload.html')
        self.response.write(template.render(template_values))

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):

    def post(self):
        upload_files = self.get_uploads('uploadField')
        blob_info = upload_files[0]
        current_user = getCurrentUser(self)
        blob = Blob(author_netID=current_user.netID,
                    blob_prop=str(blob_info.key()),
                    blob_key=blob_info.key(),
                    filename=self.request.get('filename'),
                    extension=self.request.get('extension'),
                    upload_type=self.request.get('upload_type'),
                )
        blob.put()
        time.sleep(TIME_SLEEP)
        if blob.upload_type=='user_list':
            self.redirect('/admin/user_process_upload')
        else:
            self.redirect('/files/view_list')

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)
        #self.redirect('/view_files')

class UploadFile(webapp2.RequestHandler):
    def post(self):
        newfile = File()
        newfile.filename = self.request.get('filename')
        newfile.ext = self.request.get('ext')
        newfile.content = self.request.get('upload')
        newfile.put()
        self.redirect('/files/view_list')

class FileViewList(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self):
        blobs = object_query(Blob, {}).fetch()
        template_values = {
            'current_user': getCurrentUser(self),
            'blobs':blobs,
        }
        template = JINJA_ENVIRONMENT.get_template('view_files.html')
        self.response.write(template.render(template_values))

class FileViewSingle(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self):
        query_params = {'blob_prop':str(urllib.unquote(self.request.get('blob_key')))}
        blob = object_query(Blob, query_params).get()
        blob_reader = blobstore.BlobReader(blob.blob_key)
        file = blob_reader.readlines()
        self.response.write('<br>')

        for line in file:
            self.response.write(line)
            self.response.write('<br>')
        template_values = {
            'current_user': getCurrentUser(self),
            'blob':blob,
            'file':file,
        }
        template = JINJA_ENVIRONMENT.get_template('view_files.html')
        self.response.write(template.render(template_values))

class FileDelete(webapp2.RequestHandler):

    def get(self):
        query_params = {'blob_prop':str(urllib.unquote(self.request.get('blob_key')))}
        file = object_query(Blob, query_params).get()
        blob = BlobInfo(file.blob_key)
        blob.delete()
        file.key.delete()
        time.sleep(TIME_SLEEP)
        self.redirect('/files/view_list')

class UserProcessUpload(webapp2.RequestHandler):

    def get(self):
        query_params = {'upload_type':'user_list'}
        blob = object_query(Blob, query_params).get() #eventually make sure to overwrite old list
        blob_reader = blobstore.BlobReader(blob.blob_key)
        file = blob_reader.readlines()
        user_list = []
        added_users = []

        for line in file:
            user_list.append(line.strip().split(','))
        for user in user_list[1:]: # omit header line
            netID = user[0]
            user_type=user[1]
            if object_query(User, {'netID':netID, 'user_type':user_type}).get()==None:
                new_user = User(netID=netID, user_type=user_type)
                added_users.append(new_user)
                new_user.put()
        # self.response.write(added_users)
        template_values = {
            'current_user': getCurrentUser(self),
            'added_users': added_users
        }
        template = JINJA_ENVIRONMENT.get_template('user_list.html')
        self.response.write(template.render(template_values))

class Unauthorized(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('unauthorized.html')
        self.response.write(template.render(template_values))

class UserView(webapp2.RequestHandler):

    def get(self):
        session = get_current_session()
        if session['user'].user_type != 'administrator':
            self.redirect('/unauthorized')
        query_params = {} # empty because we want all users
        query = object_query(User, query_params)

        # sort users results (sort_by is a link selected by the user in users.html)
        sort_by = self.request.get('sort_by')
        if sort_by == 'user_type':
            query = query.order(User.user_type)
        if sort_by == 'netID':
            query = query.order(User.netID)

        users = query.fetch()
        template_values = {
            'users':users,
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('users.html')
        self.response.write(template.render(template_values))

    def post(self):
        action = self.request.get('action')
        if action == 'add':
            user_type = self.request.get('user_type')
            user_netID = self.request.get('user_netID')
            if user_type == 'student':
                user = Student(netID=user_netID, user_type='student')
            elif user_type == 'faculty':
                user = Faculty(netID=user_netID, user_type='faculty')
            else: # user_type == 'administrator':
                user = Administrator(netID=user_netID, user_type='administrator')
            validateNewUser(self, user)

class UserInvalid(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        query  = object_query(User, query_params)
        user = query.fetch(1)[0]
        template_values = {
            'user': user,
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('user_invalid.html')
        self.response.write(template.render(template_values))

class UserDelete(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        query  = object_query(User, query_params)
        query_params2 = {'student_netID': query_params['netID']}
        query2 = object_query(Form, query_params2)
        query2 = query2.fetch()
        query2_length = len(query2)
        if query2_length != 0:
            for q in range(0, query2_length):
                form = query2[q]
                form.key.delete()

        user = query.get()
        user.key.delete()
        time.sleep(TIME_SLEEP)
        self.redirect('/admin/users')

class UserViewSingle(webapp2.RequestHandler):
    # this shows the results of what has been submitted
    def get(self):
        # calls helper method
        query_params = build_query_params(self)
        query = object_query(User, query_params)
        user = query.get()
        if user.user_type == 'student':
            query_params2 = {'student_netID': user.netID}
            query2 = object_query(Form, query_params2)
            forms = query2.fetch()
        elif user.user_type == 'advisor':
            query_params2 = {'advisor_netID': user.netID}
            query2 = object_query(Form, query_params2)
            forms = query2.fetch()

        template_values = {
            'forms': forms,
            'user': user,
            'current_user': getCurrentUser(self),
        }
#        self.response.write(forms)
        template = JINJA_ENVIRONMENT.get_template('user_view.html')
        self.response.write(template.render(template_values))

class UserUpload(webapp2.RequestHandler):

    def get(self):
        upload_url = blobstore.create_upload_url('/files/upload')
        template_values = {
            'upload_url':upload_url,
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('user_upload.html')
        self.response.write(template.render(template_values))

class MessageView(webapp2.RequestHandler):

    def get(self):
        template_values = {
            'messages': getMessages(self),
            'current_user': getCurrentUser(self),
        }
        template = JINJA_ENVIRONMENT.get_template('messages.html')
        self.response.write(template.render(template_values))

    def post(self):
        message = Message(author_netID=getCurrentUser(self).netID,
                          content=self.request.get("content")
                      )
        message.put()
        time.sleep(TIME_SLEEP)
        self.redirect('messages')

class StudentSelect(webapp2.RequestHandler):

    def get(self):
        query_params = build_query_params(self)
        template_values = {
            'current_user': getCurrentUser(self),
            'form_type': query_params['form_type']
            }

        template = JINJA_ENVIRONMENT.get_template('select.html')
        self.response.write(template.render(template_values))
        self.response.write(getCurrentUser(self).student_requests)

    def post(self):
        query_params = build_query_params(self)
        query = object_query(Form, query_params)
        form = query.get()

        template_values = {
            'current_user': getCurrentUser(self),
            'form': form
            }
        if form != None:
            if form.form_type == 'checkpoint' or form.form_type == 'february':
                template = JINJA_ENVIRONMENT.get_template("%s_form.html" %form.form_type)
            else:
                template = JINJA_ENVIRONMENT.get_template("view_%s.html" % form.form_type)
            self.response.write(template.render(template_values))
        else:
            self.redirect('/forms/unsubmitted')

class StudentControlPanel(webapp2.RequestHandler):

    def get(self):
        current_user = getCurrentUser(self)
        if current_user.user_type != 'student':
            self.redirect('/unauthorized')

        query_params = {'student_netID': current_user.netID, 'form_type': 'signup' }
        query = object_query(Form, query_params)
        form = query.get()
        
        not_init = False
        if form == None:
            not_init = True
        
        advisor_netID = None
        approved_advisor = True

        sr_netID = None
        approved_sr = "Approved"

        checkpoint = "Not Submitted"
        february = "Not Submitted"
        second_reader = "Not Submitted"

        if not not_init:
            advisor_netID = form.advisor_netID
            query_params = {'netID': advisor_netID}
            query = object_query(Faculty, query_params)
            advisor = query.get()

            if current_user.netID not in advisor.student_netIDs:
                approved_advisor = False

            query_params = {'student_netID': current_user.netID, 'form_type': 'second_reader' }
            query = object_query(Form, query_params)
            sr = query.get()

            if sr != None:
                sr_netID = sr.sr_netID
                query_params = {'netID': sr_netID}
                query = object_query(Faculty, query_params)
                advisor = query.get()

                if current_user.netID not in advisor.second_reader_netIDs:
                    approved_sr = "Request Pending"

            query_params = {'student_netID': current_user.netID, 'form_type': 'checkpoint'}
            query = object_query(Form, query_params)
            query = query.get()
            if query != None:
                checkpoint = "Submitted"

            query_params = {'student_netID': current_user.netID, 'form_type': 'february'}
            query = object_query(Form, query_params)
            query = query.get()
            if query != None:
                february = "Submitted"

            query_params = {'student_netID': current_user.netID, 'form_type': 'second_reader'}
            query = object_query(Form, query_params)
            query = query.get()
            if query != None:
                second_reader = "Submitted"
        

        template_values = {
            'not_init': not_init,
            'current_user': current_user,
            'advisor_netID': advisor_netID,
            'approved_advisor': approved_advisor,
            'sr_netID': sr_netID,
            'approved_sr': approved_sr,
            'signup': 'Submitted',
            'checkpoint': checkpoint,
            'february': february,
            'second_reader': second_reader
            
            }


        template = JINJA_ENVIRONMENT.get_template("student_cp.html")
        self.response.write(template.render(template_values))
    

    def post(self):        
        current_user = getCurrentUser(self)

        advisor_netID = self.request.get("advisors")
        form_type = self.request.get("forms")

        self.response.write(advisor_netID)

        if advisor_netID != "":
            query_params = {'netID': advisor_netID}
            query = object_query(Faculty, query_params)
            advisor = query.get()

            if current_user.netID in advisor.student_netIDs:
                advisor.student_netIDs.remove(current_user.netID)
                query_params = {'student_netID': current_user.netID}
                query = object_query(Form, query_params)
                query = query.fetch()
                query_length = len(query)
                for q in range(0, query_length):
                    form = query[q]
                    form.key.delete()

                current_user.advisor_netID = None
                current_user.put()

            elif current_user.netID in advisor.student_requests:
                advisor.student_requests.remove(current_user.netID)
                query_params = {'student_netID': current_user.netID}
                query = object_query(Form, query_params)
                query = query.fetch()
                query_length = len(query)
                for q in range(0, query_length):
                    form = query[q]
                    form.key.delete()

                current_user.advisor_netID = None
                current_user.put()

            elif current_user.netID in advisor.second_reader_netIDs:
                advisor.second_reader_netIDs.remove(current_user.netID)
                query_params = {'student_netID': current_user.netID, 'form_type': 'second_reader'}
                query = object_query(Form, query_params)
                form = query.get()
                form.key.delete()
            
                current_user.second_reader_netID = None
                current_user.put()

            else:
                advisor.second_reader_requests.remove(current_user.netID)
                query_params = {'student_netID': current_user.netID, 'form_type': 'second_reader'}
                query = object_query(Form, query_params)
                form = query.get()
                form.key.delete()
                
                current_user.second_reader_netID = None
                current_user.put()

            advisor.put()

        elif form_type != "":

            query_params = {'student_netID': current_user.netID, 'form_type': form_type }
            query = object_query(Form, query_params)
            form = query.get()
            form.key.delete()

            if form_type == 'second_reader':
                sr_netID = form.sr_netID
                query_params = {'netID': sr_netID }
                query = object_query(Faculty, query_params)
                sr = query.get()
                
                if current_user.netID in sr.second_reader_netIDs:
                    sr.second_reader_netIDs.remove(current_user.netID)
                    sr.put()

                else:
                    sr.second_reader_requests.remove(current_user.netID)
                    sr.put()


        self.redirect('/')
            
            

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/about', About),
    ('/contact', Contact),
    ('/login', LoginPage),
    ('/logout', LogoutPage),
    ('/login/unauthorized', LoginUnauthorizedPage),
    ('/unauthorized', Unauthorized),
    ('/forms/unsubmitted',Unsubmitted), 
    ('/forms/signup', SignupFormPage),
    ('/forms/second_reader', SecondReaderFormPage),
    ('/forms/checkpoint', CheckPointFormPage),
    ('/forms/february', FebruaryFormPage),
    ('/forms/view', FormView),
    ('/forms/query', FormQuery),
    ('/forms/query_results', FormQueryResults),
    ('/forms/query_view', FormQueryView),
    ('/forms/delete', FormDelete),
    ('/forms/invalid_entry', FormInvalid),
    ('/files/new_file', NewFile),
    ('/files/upload', UploadHandler),
    # ('/upload', UploadFile),
    ('/serve/([^/]+)?', ServeHandler),
    ('/files/view_list', FileViewList),
    ('/files/view_single',FileViewSingle),
    ('/files/delete', FileDelete),
    ('/admin/users', UserView),
    ('/admin/user_delete', UserDelete),
    ('/admin/user_view', UserViewSingle),
    ('/admin/invalid_entry', UserInvalid),
    ('/admin/user_upload', UserUpload),
    ('/admin/user_process_upload', UserProcessUpload),
    ('/messages', MessageView),
    ('/forms/approve', ApproveAdvisees),
    ('/forms/select?', StudentSelect),
    ('/student/cp', StudentControlPanel)

], debug=True)

def main():
#    run_wsgi_app(webapp_add_wsgi_middleware(application))
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

def alreadySubmitted(self, netID, form_type):
    query_params = {'student_netID': netID, 'form_type': form_type}
    query = object_query(Form, query_params)
    form = query.get()
    
    if form == None:
        return False

    else:
        template_values = {
            'current_user': getCurrentUser(self),
            'form': form
            }

        template = JINJA_ENVIRONMENT.get_template("view_%s.html" % form.form_type)
        self.response.write(template.render(template_values))
        return True
