# coding: utf-8

import json
import cherrypy
from app import database

#-------------------------------------------------------
class Eval(object):
#-------------------------------------------------------

    exposed = True # gilt für alle Methoden

    #-------------------------------------------------------
    def __init__(self):
    #-------------------------------------------------------
        self.db = database.Database()

    #-------------------------------------------------------
    def GET(self, cat=None):
    #-------------------------------------------------------
        self.db.CheckDates()
        self.db.ReadAll()
        data = {}
        Studenten = self.db.data['Studenten']
        Lehrender = self.db.data['Lehrender']
        Firmen = self.db.data['Firmen']
        Angebote = self.db.data['Angebote']

        data['category'] = cat
        if(cat == 'Firmen'):
            for keyFirma, valueFirma in sorted(Firmen.items()):
                data[valueFirma['Name']] = {}
                data[valueFirma['Name']]['Name'] = valueFirma['Name']
                data[valueFirma['Name']]['Angebote'] = {}
                data[valueFirma['Name']]['Angebote']['Angebot'] = {}
                data[valueFirma['Name']]['Angebote']['aktuell'] = {}
                data[valueFirma['Name']]['Angebote']['abgeschlossen'] = {}

                for keyAngebote, valueAngebote in Angebote.items():
                    if(valueAngebote['Firma'] == valueFirma['Name']):
                        data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']] = {}
                        data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['Name'] = valueAngebote['Name']
                        if(valueAngebote['Student'] != ''):
                            data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['Student'] = Studenten[valueAngebote['Student']]['Name']
                        else:
                            data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['Student'] = ''
                        if(valueAngebote['Lehrender'] != ''):
                            data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['Lehrender'] = Lehrender[valueAngebote['Lehrender']]['Name']
                        else:
                            data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['Lehrender'] = ''
                        data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['ZeitraumVon'] = valueAngebote['ZeitraumVon']
                        data[valueFirma['Name']]['Angebote'][valueAngebote['Status']][valueAngebote['Name']]['ZeitraumBis'] = valueAngebote['ZeitraumBis']

        elif(cat == 'Studenten'):
            for keyStudent, valueStudent in Studenten.items():
                data[valueStudent['Name']] = {}
                data[valueStudent['Name']]['Angebote'] = {}
                data[valueStudent['Name']]['Angebote']['Angebot'] = {}
                data[valueStudent['Name']]['Angebote']['aktuell'] = {}
                data[valueStudent['Name']]['Angebote']['abgeschlossen'] = {}
                for keyAngebot, valueAngebot in Angebote.items():
                    if(valueAngebot['Student'] == valueStudent['id']):
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot] = {}
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Lehrender'] = Lehrender[valueAngebot['Lehrender']]['Name']
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Status'] = valueAngebot['Status']
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Praxisphase'] = valueAngebot['Name']
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Firma'] = valueAngebot['Firma']
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['ZeitraumVon'] = valueAngebot['ZeitraumVon']
                        data[valueStudent['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['ZeitraumBis'] = valueAngebot['ZeitraumBis']
        elif(cat == 'Lehrender'):
            for keyLehrende, valueLehrende in Lehrender.items():
                data[valueLehrende['Name']] = {}
                data[valueLehrende['Name']]['Angebote'] = {}
                data[valueLehrende['Name']]['Angebote']['Angebot'] = {}
                data[valueLehrende['Name']]['Angebote']['aktuell'] = {}
                data[valueLehrende['Name']]['Angebote']['abgeschlossen'] = {}
                for keyAngebot, valueAngebot in Angebote.items():
                    if(valueAngebot['Lehrender'] == valueLehrende['id']):
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot] = {}
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Student'] = Studenten[valueAngebot['Student']]['Name']
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Status'] = valueAngebot['Status']
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Praxisphase'] = valueAngebot['Name']
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['Firma'] = valueAngebot['Firma']
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['ZeitraumVon'] = valueAngebot['ZeitraumVon']
                        data[valueLehrende['Name']]['Angebote'][valueAngebot['Status']][keyAngebot]['ZeitraumBis'] = valueAngebot['ZeitraumBis']
        print(data)
        return json.dumps(data)