# coding: utf-8

import cherrypy
from .database import Database_cl
from .view import View_cl

#----------------------------------------------------------
class Application_cl(object): 
#----------------------------------------------------------

	#-------------------------------------------------------
	def __init__(self):
	#-------------------------------------------------------
		# spezielle Initialisierung können hier eingetragen werden
		self.db_o = Database_cl()
		self.view_o = View_cl()
		self.liste = 0
	@cherrypy.expose

   
	#-------------------------------------------------------
	def index(self, form=None): 
	#-------------------------------------------------------
		if(form == "0"):
			return self.createList_p(form)
		elif(form == "1"):
			return self.createList_p(form)
		elif(form == None):
			if (self.liste == 0):
				self.liste = 1;
				return self.createList_p("0")
			else:
				return self.createList_p("1")
	@cherrypy.expose


	#-------------------------------------------------------
	def add(self, form=None): 
	#-------------------------------------------------------
		
		return self.createForm_p(form)
	@cherrypy.expose


	#-------------------------------------------------------
	def edit(self, id, form=None): 
	#-------------------------------------------------------
		return self.createForm_p(form, id)
	@cherrypy.expose


	#-------------------------------------------------------
	def save(self, **data_opl): 
	#-------------------------------------------------------
		# Sichern der Daten: aufgrund der Formularbearbeitung muss
		# eine vollständige HTML-Seite zurückgeliefert werden!
		# data_opl: Dictionary mit den gelieferten key-value-Paaren
		# hier müsste man prüfen, ob die Daten korrekt vorliegen!
		# HIER müssen Sie die Semesterzahl(en) ergänzen
		id_s = data_opl["id_s"] 
		data_a = [ data_opl["name1_s"] 
		,	data_opl["vorname1_s"]
		,	data_opl["matrnr1_s"]
		,	data_opl["semesteranzahl1_s"]
		,	data_opl["name2_s"]
		,	data_opl["vorname2_s"]
		,	data_opl["matrnr2_s"]
		,	data_opl["semesteranzahl2_s"]
		]

		if id_s != "None":
			# Update-Operation
			self.db_o.update_px(id_s, data_a) 
		else:
			# Create-Operation
			id_s = self.db_o.create_px(data_a)

		return self.createForm_p(id_s)
	@cherrypy.expose


	#-------------------------------------------------------
	def delete(self, id, form=None): 
	#-------------------------------------------------------
		# Eintrag löschen, dann Liste neu anzeigen
		self.db_o.delete_px(id)
		print("Delete",form)
		return self.createList_p(form)
	@cherrypy.expose
	

	#-------------------------------------------------------
	def default(self, *arguments, **kwargs): 
	#-------------------------------------------------------
		msg_s = "unbekannte Anforderung: " + \
				str(arguments) + \
				''+ \
				str(kwargs)
		raise cherrypy.HTTPError(404, msg_s)
	default.exposed= True


	#-------------------------------------------------------
	def createList_p(self, form): 
	#-------------------------------------------------------
		data_o = self.db_o.read_px()
		# mit diesen Daten Markup erzeugen 
		print("List: ", form)

		return self.view_o.createList_px(data_o, form)


	#-------------------------------------------------------
	def createForm_p(self, form, id_spl = None): 
	#-------------------------------------------------------
		if id_spl != None:
			data_o = self.db_o.read_px(id_spl)
		else:
			data_o = self.db_o.getDefault_px()
		
		# mit diesen Daten Markup erzeugen
		return self.view_o.createForm_px(id_spl, data_o, form)


# EOF