#!/usr/bin/env python
#
# Copyright 2006 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext.webapp import template
from models.submittedShifts import SubmittedShifts
from models.employee import Employee
import json
import time
from datetime import date
from datetime import timedelta
import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
		userName = None
		if self.request.cookies.get('our_token'):    #the cookie that should contain the access token!
			userName = Employee.checkToken(self.request.cookies.get('our_token'))

		today = date.today()
		today = today 
		if(int(today.strftime("%U"))%2 == 0):
			today = today - timedelta(days = 7)
		sunday0 = today
		monday0 = today
		tuesday0 = today
		wednesday0 = today
		thursday0 = today
		friday0 = today
		saturday0 = today
		
		if (today.weekday() == 6):
			sunday0 = today
			monday0 = sunday0 + timedelta(days=1)
			tuesday0 = monday0 + timedelta(days=1)
			wednesday0 = tuesday0 + timedelta(days = 1)
			thursday0 = wednesday0 + timedelta(days = 1)
			friday0 = thursday0 + timedelta(days = 1)
			saturday0 = friday0 + timedelta(days = 1)
			
		if (today.weekday() == 0):
			monday0 = today
			tuesday0 = monday0 + timedelta(days=1)
			wednesday0 = tuesday0 + timedelta(days = 1)
			thursday0 = wednesday0 + timedelta(days = 1)
			friday0 = thursday0 + timedelta(days = 1)
			saturday0 = friday0 + timedelta(days = 1)
			sunday0 = saturday0 - timedelta(days = 6)
			
		if (today.weekday() == 1):
			tuesday0 = today
			wednesday0 = tuesday0 + timedelta(days = 1)
			thursday0 = wednesday0 + timedelta(days = 1)
			friday0 = thursday0 + timedelta(days = 1)
			saturday0 = friday0 + timedelta(days = 1)
			sunday0 = saturday0 - timedelta(days = 6)
			monday0 = sunday0 + timedelta(days=1)
			
		if (today.weekday() == 2):
			wednesday0 = today
			thursday0 = wednesday0 + timedelta(days = 1)
			friday0 = thursday0 + timedelta(days = 1)
			saturday0 = friday0 + timedelta(days = 1)
			sunday0 = saturday0 - timedelta(days = 6)
			monday0 = sunday0 + timedelta(days=1)
			tuesday0 = monday0 + timedelta(days=1)
			
		if (today.weekday() == 3):
			thursday0 = today
			friday0 = thursday0 + timedelta(days = 1)
			saturday0 = friday0 + timedelta(days = 1)
			sunday0 = saturday0 - timedelta(days = 6)
			monday0 = sunday0 + timedelta(days=1)
			tuesday0 = monday0 + timedelta(days=1)
			wednesday0 = tuesday0 + timedelta(days = 1)
			
		if (today.weekday() == 4):
			friday0 = today
			saturday0 = friday0 + timedelta(days = 1)
			sunday0 = saturday0 - timedelta(days = 6)
			monday0 = sunday0 + timedelta(days=1)
			tuesday0 = monday0 + timedelta(days=1)
			wednesday0 = tuesday0 + timedelta(days = 1)
			thursday0 = wednesday0 + timedelta(days = 1)
			
		if (today.weekday() == 5):
			saturday0 = today
			sunday0 = saturday0 - timedelta(days = 6)
			monday0 = sunday0 + timedelta(days=1)
			tuesday0 = monday0 + timedelta(days=1)
			wednesday0 = tuesday0 + timedelta(days = 1)
			thursday0 = wednesday0 + timedelta(days = 1)
			friday0 = thursday0 + timedelta(days = 1)		
		
		if saturday0:
			sunday1 = saturday0 + timedelta(days=1)
			monday1 = saturday0 + timedelta(days=2)
			tuesday1 = saturday0 + timedelta(days=3)
			wednesday1 = saturday0 + timedelta(days=4)
			thursday1 = saturday0 + timedelta(days=5)
			friday1 = saturday0 + timedelta(days=6)
			saturday1 = saturday0 + timedelta(days=7)
		
		template_variables = {}
		if userName:
			template_variables['userName'] = userName.userName
			template_variables['sunday0'] = "%d/%d"%(sunday0.day ,sunday0.month)
			template_variables['monday0'] = "%d/%d"%(monday0.day ,monday0.month)
			template_variables['tuesday0'] = "%d/%d"%(tuesday0.day ,tuesday0.month)
			template_variables['wednesday0'] = "%d/%d"%(wednesday0.day ,wednesday0.month)
			template_variables['thursday0'] = "%d/%d"%(thursday0.day ,thursday0.month)
			template_variables['friday0'] = "%d/%d"%(friday0.day ,friday0.month)
			template_variables['saturday0'] = "%d/%d"%(saturday0.day ,saturday0.month)
			template_variables['sunday1'] = "%d/%d"%(sunday1.day ,sunday1.month)
			template_variables['monday1'] = "%d/%d"%(monday1.day ,monday1.month)
			template_variables['tuesday1'] = "%d/%d"%(tuesday1.day ,tuesday1.month)
			template_variables['wednesday1'] = "%d/%d"%(wednesday1.day ,wednesday1.month)
			template_variables['thursday1'] = "%d/%d"%(thursday1.day ,thursday1.month)
			template_variables['friday1'] = "%d/%d"%(friday1.day ,friday1.month)
			template_variables['saturday1'] = "%d/%d"%(saturday1.day ,saturday1.month)

			

			#### First week ####
			
			
			sunday0date = date(sunday0.year, sunday0.month, sunday0.day)
			
			# Sunday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(sunday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(sunday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(sunday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Sunday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Sunday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Sunday0NightAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Sunday0 morning info:	
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(sunday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(sunday0date, 1, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(sunday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Sunday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Sunday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Sunday0MorningAssignBeforeStandBy'] = assignBeforeStandBy

				
			# Sunday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(sunday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(sunday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(sunday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Sunday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Sunday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Sunday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			
			
			
			
			monday0date = date(monday0.year, monday0.month, monday0.day)	
			
				
			# Monday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(monday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(monday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(monday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Monday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Monday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Monday0NightAssignBeforeStandBy'] = assignBeforeStandBy
				
			# Monday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(monday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(monday0date, 1, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(monday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Monday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Monday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Monday0MorningAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Monday0 noon info:		
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(monday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(monday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(monday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Monday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Monday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Monday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			
			

			tuesday0date = date(tuesday0.year, tuesday0.month, tuesday0.day)
			
				
			# Tuesday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(tuesday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(tuesday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(tuesday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Tuesday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Tuesday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Tuesday0NightAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Tuesday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(tuesday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(tuesday0date, 1, 1)
			assignBeforeThird = SubmittedShifts.checkIfAssignAlready(tuesday0date, 1, 2)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(tuesday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Tuesday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Tuesday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeThird:
				template_variables['Tuesday0MorningAssignBeforeThird'] = assignBeforeThird
				
			if assignBeforeStandBy:
				template_variables['Tuesday0MorningAssignBeforeStandBy'] = assignBeforeStandBy

			# Tuesday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(tuesday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(tuesday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(tuesday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Tuesday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Tuesday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Tuesday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			

			
			wednesday0date = date(wednesday0.year, wednesday0.month, wednesday0.day)
		
		
			# Wednesday0 night info:	
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(wednesday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(wednesday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(wednesday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Wednesday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Wednesday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Wednesday0NightAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Wednesday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(wednesday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(wednesday0date, 1, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(wednesday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Wednesday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Wednesday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Wednesday0MorningAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Wednesday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(wednesday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(wednesday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(wednesday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Wednesday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Wednesday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Wednesday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			
			
			
			thursday0date = date(thursday0.year, thursday0.month, thursday0.day)
				
				
			# Thursday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(thursday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(thursday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(thursday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Thursday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Thursday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Thursday0NightAssignBeforeStandBy'] = assignBeforeStandBy
				
			# Thursday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(thursday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(thursday0date, 1, 1)
			assignBeforeThird = SubmittedShifts.checkIfAssignAlready(thursday0date, 1, 2)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(thursday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Thursday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Thursday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeSecond:
				template_variables['Thursday0MorningAssignBeforeThird'] = assignBeforeThird
				
			if assignBeforeStandBy:
				template_variables['Thursday0MorningAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Thursday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(thursday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(thursday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(thursday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Thursday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Thursday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Thursday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			
			
			
			friday0date = date(friday0.year, friday0.month, friday0.day)
				
				
			# Friday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(friday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(friday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(friday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Friday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Friday0NighAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Friday0NighAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Friday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(friday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(friday0date, 1, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(friday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Friday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Friday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Friday0MorningAssignBeforeStandBy'] = assignBeforeStandBy

			# Friday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(friday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(friday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(friday0date, 2, 3)
			
			if assignBeforeHead:
				template_variables['Friday0NoonAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Friday0NoonAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Friday0NoonAssignBeforeStandBy'] = assignBeforeStandBy
			
			
			
			saturday0date = date(saturday0.year, saturday0.month, saturday0.day)
				
				
			# Saturday0 night info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(saturday0date, 0, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(saturday0date, 0, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(saturday0date, 0, 3)
			
			if assignBeforeHead:
				template_variables['Saturday0NightAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Saturday0NightAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Saturday0NightAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Saturday0 morning info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(saturday0date, 1, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(saturday0date, 1, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(saturday0date, 1, 3)
			
			if assignBeforeHead:
				template_variables['Saturday0MorningAssignBeforeHead'] = assignBeforeHead
				
			if assignBeforeSecond:
				template_variables['Saturday0MorningAssignBeforeSecond'] = assignBeforeSecond
				
			if assignBeforeStandBy:
				template_variables['Saturday0MorningAssignBeforeStandBy'] = assignBeforeStandBy
			
			# Saturday0 noon info:
			assignBeforeHead = SubmittedShifts.checkIfAssignAlready(saturday0date, 2, 0)
			assignBeforeSecond = SubmittedShifts.checkIfAssignAlready(saturday0date, 2, 1)
			assignBeforeStandBy = SubmittedShifts.checkIfAssignAlready(saturday0date, 2, 3)
			
			if assignBeforeHead: