from django_unicorn.components import UnicornView
from BasicData.models import Annee, Ecue, Etudiant, EtudiantUe, Evaluation, Grade, GradeMentionUfr, Mention, Note, Parcours, ParcoursSemestreUe, Semestre, Session, TypeEvaluation, Ue, Ufr, ParcoursSemestre


class NoteComponentView(UnicornView):
    annees = Annee.objects.all()
    ufrs = Ufr.objects.all()
    parcours = Parcours.objects.all()
    semestres = Mention.objects.all()
    sessions = Session.objects.all()
    typeEvaluations = TypeEvaluation.objects.all()

    ues = Ue.objects.all()
    ecues = Ecue.objects.filter()
    evaluations = Evaluation.objects.filter()
    etudiants = None
    notes = None
    moyennes = None
    search = ""
    

    def mount(self):
        self.anneeId = 0
        self.ufrId = 0
        self.parcourId = 0
        self.semestreId = 0
        self.ueId = 0
        self.ecueId = 0
        self.sessionId = 0
        self.evaluationId = 0



    def resetEtudiants(self):
        parcoursSemestre = ParcoursSemestre.objects.get(parcours__id=self.parcourId, semestre__id=self.semestreId)
        parcourSemestreUe = ParcoursSemestreUe.objects.get(parcoursSemestre=parcoursSemestre, annee=Annee.objects.last())
        self.etudiants = EtudiantUe.objects.filter(parcourSemestreUe=parcourSemestreUe, annee=Annee.objects.last(), ue__id=self.ueId)


    def resetUeId(self):
        self.ueId = 0
        self.ues = Ue.objects.filter(ufr_id=self.ufrId)
        self.resetEcueId()
        self.resetEtudiants()

    def resetEcueId(self):
        self.ecueId = 0
        self.ecues = Ecue.objects.filter(ue_id=self.ueId)
        # self.resetEtudiants()

    def resetEvaluationId(self):
        self.evaluationId = 0
        self.evaluations = Evaluation.objects.filter(
            annee_id=self.anneeId, 
            ecue_id=self.ecueId, 
            session_id=self.sessionId
            )

    def creeerEvaluation(self):
        Evaluation.objects.create(
            annee_id=self.anneeId, 
            ecue_id=self.ecueId, 
            session_id=self.sessionId
            )

    def getNote(self, etudiantId, evaluationId):
        try:
            return Note.objects.get(etudiant_id=etudiantId, evaluation_id=evaluationId)
        except Note.DoesNotExist:
            note = Note()
            note.save()
            return note


    def updated_anneeId(self, value):
        self.anneeId = value
        # self.resetUeId()
        # self.resetEcueId()
        # self.resetEvaluationId()

    def updated_ufrId(self, value):
        self.ufrId = value
        self.resetUeId()

    def updated_parcoursId(self, value):
        self.parcourId = value
    
    def updated_semestreId(self, value):
        self.semestreId = value
    
    def updated_ueId(self, value):
        self.ueId = value
        self.resetEtudiants()
        self.resetEcueId()

    def updated_ecueId(self, value):
        self.ecueId = value
        print("ue = ", self.ueId)
        self.resetEvaluationId()

    def updated_sessionId(self, value):
        self.sessionId = value
        self.resetEvaluationId()

    def enregistrer(self):
        for session, infos in self.notes.items():
            for etudiantId, values in infos.items():
                note_avg = []
                for evaluationId, note in values.items():
                    try:
                        n = Note.objects.get(etudiant_id=etudiantId, evaluation_id=evaluationId)
                        n.note = note
                        n.save()
                    except Note.DoesNotExist:
                        etudiant = Etudiant.objects.get(etudiantId)
                        evaluation = Etudiant.objects.get(evaluationId)
                        n = Note(etudiant=etudiant, evaluation=evaluation)
                        n.save()

                    note_avg.append(n)

    def valider(self):
        for session, infos in self.notes.items():
            for etudiantId, values in infos.items():
                for evaluationId, note in values.items():
                    try:
                        n = Note.objects.get(etudiant_id=etudiantId, evaluation_id=evaluationId)
                        n.isBlocked = True
                    except Note.DoesNotExist:
                        pass
