#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os, cherrypy, kid

# use logging
import logging, logging.config
log = logging.getLogger("div")

# --------------------------------------------------------------------------------
import page
from construction import UnderConstruction
from search.search import Search,Units,U_AdvancedSearch, U_SimpleSearch,GBIFagreement
from search.units.popup import Popup
#from search.map.map import Map
from preferences.preferences import Preferences
from thesaurusconf.thesaurusConf import ThesaurusConf
from help.help import Help
from about.about import About,AboutBG, AboutB, AboutAck,AboutData,AboutHer
from registry.registry import Host,Datasources,Registry,Collections

from contrib.contributors import Contributors

from links.links import Links

from partner.partner import Partner

from results.units.result import U_Preview,U_Results,U_Syncon,U_Details,U_Annotation
from results.units.alert import QuotaAlert

from requestHandler.props import *
from requestHandler.units.queryManager import U_QueryManager
from biocase.downloading.downloadManager import Confirmation, Validation
import caractersHandling as _carac
from thesaurus.ThesaurusLoader import TransformXmlForThesaurus

#from spreadsheets.spreadsheet import Spreadsheet

from cherrypy import _cperror, _cputil as ut	
import StringIO,smtplib,sha,random,time


retry=0
def handle_error():
	import StringIO,string
	cherrypy.response.status = 500
	myPage = page.Page()
	mess = str(cherrypy._cputil.formatExc())
	
	global retry
	
	try:
		if 'insecure string pickle' in mess:
			if retry == 0:
				retry=1
				cherrypy.response.status = 302
				
				comingFrom = cherrypy.request.headerMap.get('Referer','%s'%myPage.getCfgObj().webroot)
				cherrypy.response.headers['Location'] = comingFrom 
				return
			
		if 'mismatched tag' in mess and 'annotation.py' in mess:
			file=open("%s"%cherrypy.config.get('erreurAnnotation'),"r")
			retry=1
		else:
			file = open("%s"%cherrypy.config.get('erreur'),"r")
			retry=1
		contenu = file.read()
		file.close()
	except Exception,e:
		log.info(e)
		contenu= ["<html><body>Sorry, an error occured with the server.<br/> Please try again later.	<br/><br/>If this error occures again, you might have to delete your cookies for the current domaine and browser cache.</body></html>"]
	
	if retry ==1:
		
		cherrypy.response.body = contenu
		#cherrypy.response.body = ["<html><body>Sorry, an error occured with the server.<br/> Please try again later.	<br/><br/>If this error occures again, you might have to delete your cookies for the current domaine and browser cache.</body></html>"]
		fromaddr =("From: %s"%myPage.getCfgObj().server.websiteTitle)
		toaddrs  = string.splitfields('%s'%myPage.getCfgObj().server.debugMail)
		subject = 'Error in %s'%myPage.getCfgObj().server.websiteTitle
		
		try:
			mess+="\n language: %s"%cherrypy.session.get('lang','nolang')
			mess+="\n full session"
			for k in cherrypy.session.keys():
				mess+="\n \t %s %s"%(k,str(cherrypy.session.get(k)))
		except:
			pass
		if "googlebot" not in mess.lower() and "google" not in mess.lower() and 'too many values to unpack' not in mess:
			body =  string.join((
				"Subject: %s" % subject,
				"",
				mess), "\r\n")
			# The actual mail send
			server = smtplib.SMTP('%s'%myPage.getCfgObj().server.smtp)
			try:
				server.sendmail(fromaddr, toaddrs, body)
			except Exception, e:
				log.info("pb with the smtp for logging errors %s" %e)
			retry=0
			server.quit()
	
class Empty(object):
	pass

class Site(page.Page):
	#_instance = None
	##singleton
	#def __new__( cls, *args, **kwargs ):
		#'''Init the website. Read global configs and update the CherryPy settings.'''
		#if not cls._instance:
			#cls._instance = super( Site, cls ).__new__(
				#cls, *args, **kwargs )
			#self=cls._instance
			#page.Page.__init__(self)
			#return cls._instance
		

	@cherrypy.expose
	def deleteSession(self):
		try:
			#delete the old cookie
			cherrypy.response.simple_cookie['%s'%self.getCfgObj().server.cookieName]['expires'] = 0
		except Exception,e:
			log.info("couldn't erase the session %s"%e)
			pass
		self.redirect('/index')
		
	@cherrypy.expose
	def index(self, *args, **kwargs):
		if len(args) > 0:
			if 'session' in args:
				for k in cherrypy.session.keys():
					cherrypy.session.pop(k)

		action =  cherrypy.session.get('action',"")
		if 'unitName' not in action:
			action = action+"; giveFocus('unitName')"
			
		cherrypy.session['message']=''
		
		#load the specimen of the day from the database and save the data in the session
		self.loadSpecimenOfTheDay()
		
		javascript='<script src="%s/static/php/dojotree/teleport_xmlrpc.js" type="text/javascript"/><script src="%s/static/php/dojotree/suggest.js" type="text/javascript"/>'%(self.getCfgObj().server.webroot,self.getCfgObj().server.webroot)
		
		### temporary index page change
		f = open("/home/biocase/vh/webapp/static/startpage.html")
		content = "".join(f.readlines())
		f.close
		return content; # following code won't be executed => no kid templates
		### /temporary
		
#		####
#		####thesaurus staff----------------------------------------------		
#		####
#		listStr = self.getCfgObj().checklist.thesauri
#		tmp = listStr.split("(")
#		tklist={}
#		for cell in tmp:
#			tmp2 = cell.split(")")
#			for case in tmp2:
#				cell = case.split(",")
#				if len(cell)==2:
#					tklist[cell[0]]=cell[1]
#		cherrypy.session['checklistDic'] = tklist
#		self.checklist = cherrypy.session.get('checklistDic').keys()
#		
#		try:
#			self.checklistsdict = cherrypy.session.get('checklists')
#			tmp = self.checklistsdict.keys()
#		except:
#			tmp = TransformXmlForThesaurus().loadReferences()
#			self.checklistsdict = cherrypy.session.get('checklists')
#		thesaurus={}
#		for key in self.checklistsdict.keys():
#			if key in self.checklist:
#				thesaurus[key]=self.checklistsdict.get(key)
		
		#clist = cherrypy.session.get('metaCountryList')
		##load the list of country for the metadata search and save it in the session
		#if clist is None:
			#self.loadCountryList()
		if len(args) > 0:
			if 'session' in args:
				reason = "Session expired"
#				return self.render(filePath='', fileName='_index.kid', body='index', title='%s index'%self.getCfgObj().server.websiteTitle,reason = reason, specimenName=cherrypy.session.get('specimenName'), unitID=cherrypy.session.get('unitID'), collectionID=cherrypy.session.get('collectionID'), institutionID=cherrypy.session.get('institutionID'), resourceKey=cherrypy.session.get('resourceKey'), specBioKey=cherrypy.session.get('specBioKey'),action=action,javascript=javascript, specimenofthedaypic=cherrypy.session.get('specimenofthedaypic'),thesaurusList=thesaurus)
				return self.render(filePath='', fileName='_index.kid', body='index', title='%s index'%self.getCfgObj().server.websiteTitle,reason = reason, specimenName=cherrypy.session.get('specimenName'), unitID=cherrypy.session.get('unitID'), collectionID=cherrypy.session.get('collectionID'), institutionID=cherrypy.session.get('institutionID'), resourceKey=cherrypy.session.get('resourceKey'), specBioKey=cherrypy.session.get('specBioKey'),action=action,javascript=javascript, specimenofthedaypic=cherrypy.session.get('specimenofthedaypic'))

				#specBioKey=cherrypy.session.get('specBioKey'),  countryList=cherrypy.session.get('metaCountryList'),action=action, specimenofthedaypic=cherrypy.session.get('specimenofthedaypic'))
			else:
				reason=None
				#return self.redirect('/index')
		
			
#		return self.render(filePath='', fileName='_index.kid', body='index', title='%s index'%self.getCfgObj().server.websiteTitle, reason = None, specimenName=cherrypy.session.get('specimenName'), unitID=cherrypy.session.get('unitID'), collectionID=cherrypy.session.get('collectionID'), institutionID=cherrypy.session.get('institutionID'), resourceKey=cherrypy.session.get('resourceKey'),javascript=javascript,
#		specBioKey=cherrypy.session.get('specBioKey'), 
#		 #countryList=cherrypy.session.get('metaCountryList'),
#		 action=action, specimenofthedaypic=cherrypy.session.get('specimenofthedaypic'),thesaurusList=thesaurus)
		return self.render(filePath='', fileName='_index.kid', body='index', title='%s index'%self.getCfgObj().server.websiteTitle, reason = None, specimenName=cherrypy.session.get('specimenName'), unitID=cherrypy.session.get('unitID'), collectionID=cherrypy.session.get('collectionID'), institutionID=cherrypy.session.get('institutionID'), resourceKey=cherrypy.session.get('resourceKey'),javascript=javascript,
		specBioKey=cherrypy.session.get('specBioKey'), action=action, specimenofthedaypic=cherrypy.session.get('specimenofthedaypic'))
	
	def loadSpecimenOfTheDay(self):
		'''
		for the index page
		search in the database the specimen of the day
		save the name, the unitID, the collID, the instID, the resourceKey and the picture's name in the current session
		'''
		log.info('in loadspecimenoftheday')
		if cherrypy.session.get('specimenName',"") == "":
			_cfgObj = self.getCfgObj()
			propertiesFile = _cfgObj.queriesUnitLevelLocator %_cfgObj.admin.unitsDatabase
			if not(os.access(propertiesFile, os.R_OK|os.F_OK)):
				log.debug("\tNo read access to Properties File ",propertiesFile, "Quitting")
				
			self._propsFile = props(_cfgObj.queriesUnitLevelLocator %_cfgObj.admin.unitsDatabase)
			self.qmg = U_QueryManager(self._propsFile,self.getCfgObj())
			res= self.qmg.getSpecimenOfTheDay()
			log.info("loadSpecimenOfTheDay res: %s" % res)
			
				
			if res is not None and res != []:
				cherrypy.session['specimenName']=res[0][0]
				cherrypy.session['unitID']=_carac.encodeCaracters(res[0][1])
				cherrypy.session['collectionID']=_carac.encodeCaracters(res[0][2])
				cherrypy.session['institutionID']=_carac.encodeCaracters(res[0][3])
				cherrypy.session['resourceKey']=res[0][4]
				cherrypy.session['specimenofthedaypic']=str(res[0][5]).replace('-','_')+".jpg"
			else:
				cherrypy.session['specimenName']=""
				cherrypy.session['unitID']=""
				cherrypy.session['collectionID']=""
				cherrypy.session['institutionID']=""
				cherrypy.session['resourceKey']=""
				cherrypy.session['specimenofthedaypic']=""
 
		


	#def loadCountryList(self):
		#'''
		#search the list of countries in the CORM-v2 database and store it in the current session
		#'''
		#_cfgObj = self.getCfgObj()
		#propertiesFile = _cfgObj.queriesMetadataLevelLocator
		#if not(os.access(propertiesFile, os.R_OK|os.F_OK)):
			   #log.debug("\tNo read access to Properties File ",propertiesFile, "Quitting")
		#self._propsFile = props(_cfgObj.queriesMetadataLevelLocator)
		#if cherrypy.session.get('metaCountryList',[])==[]:
			#qmg = M_QueryManager(self._propsFile,self.getCfgObj())
			#res= qmg.getCountryList()
			##log.info("country list: %s" %res)
			#if res is not None:
				#cherrypy.session['metaCountryList'] = res;
			
	@cherrypy.expose
	def default(self, *args, **kwargs):
		'''
		used for the pages, which don't already have a handler (ie. static files)
		'''
		if len(args) > 0 and 'zip' in str(args):			
			return ["<html><body>The file you asked for does no more exist.</body></html>"]	
		return self.redirect('/index')
			   
	

	cherrypy._cp_config = {'request.error_response': handle_error}		   
	ut._cp_on_error=handle_error
	
	# --- applications ---	
	search		= Search()
	search.units  = Units()
	search.units.popup = Popup()
	search.units.advancedSearch = U_AdvancedSearch()
	search.units.simpleSearch = U_SimpleSearch()
	search.units.simpleSearch.query1=U_SimpleSearch().query1
	search.units.advancedSearch.performAdvancedSearch=U_AdvancedSearch().performAdvancedSearch
	search.units.alert = QuotaAlert()
	search.units.gbifAgreement=GBIFagreement()

	search.units.confirmation = Confirmation()
	search.units.validation = Validation()
	
	search.units.preview = U_Preview()
	search.units.preview.refineQuery= U_Preview().refineQuery
	search.units.preview.see= U_Preview().see
	search.units.preview.calculate=U_Preview().calculate
	search.units.preview.upload=U_Preview().upload
	search.units.preview.seeResult=U_Preview().seeResult
	search.units.preview.performFuzzySearch=U_Preview().performFuzzySearch
	search.units.preview.dlAllFromCache=U_Preview().dlAllFromCache
	
	search.units.syncon = U_Syncon()
	
	search.units.results = U_Results()
	search.units.results.setCurrentPage = U_Results().setCurrentPage
	search.units.results.sortBy = U_Results().sortBy
	search.units.results.displayUnitsWithMulti  = U_Results().displayUnitsWithMulti
	search.units.results.dlListDetails= U_Results().dlListDetails
	search.units.results.toggleViewMode= U_Results().toggleViewMode
	
	search.units.details = U_Details()
	search.units.details.dlListDetails=U_Details().dlListDetails
	search.units.details.getDetails=U_Details().getDetails
	search.units.details.isavailable=U_Details().isavailable
	search.units.details.downloadXML=U_Details().downloadXML
	search.units.details.seeXML=U_Details().seeXML
	search.units.details.getListDetails=U_Details().getListDetails
	search.units.details.foreignServiceResults = U_Details().foreignServiceResults
	
	search.units.annotation = U_Annotation()
	search.units.annotation.seeAnnotation = U_Annotation().seeAnnotation
	search.units.annotation.seeOriginal= U_Annotation().seeOriginal
	search.units.annotation.see = U_Annotation().see
	search.units.annotation.dl = U_Annotation().dl
	search.units.annotation.compare = U_Annotation().compare

	
	registry	  = Registry()
	registry.hosts = Host()
	registry.datasources = Datasources()
	registry.collections = Collections()
	
	preferences   = Preferences()
	thesaurusConf = ThesaurusConf()
	help		  = Help()
	about		 = About()
	about.bioCASE_and_GBIF = AboutBG()
	about.bioCASE = AboutB()
	about.acknowledgement = AboutAck()
	about.terms = AboutData()
	about.herbar = AboutHer()

	contributors = Contributors()
	links = Links()
	partner = Partner()