import sco_formations
import json
import random

# La variable context est définie par le script de lancement
# l'affecte ainsi pour évietr les warnins pylint:
context = context  # pylint: disable=undefined-variable
REQUEST = REQUEST  # pylint: disable=undefined-variable
import scotests.sco_fake_gen as sco_fake_gen  # pylint: disable=import-error
import sco_moduleimpl

G = sco_fake_gen.ScoFake(context.Notes)
G.verbose = False

file = open("scotests/export_formation1.xml")
doc = file.read()
file.close()


# --- Création de la formation

f = sco_formations.formation_import_xml(doc=doc)

# --- Création des semestres

sem1 = G.create_formsemestre(
    formation_id=f[0],
    semestre_id=1,
    date_debut="01/09/2020",
    date_fin="01/02/2021",
)

sem3 = G.create_formsemestre(
    formation_id=f[0],
    semestre_id=3,
    date_debut="01/09/2020",
    date_fin="01/02/2021",
)

sem2 = G.create_formsemestre(
    formation_id=f[0],
    semestre_id=2,
    date_debut="02/02/2021",
    date_fin="01/06/2021",
)

sem4 = G.create_formsemestre(
    formation_id=f[0],
    semestre_id=4,
    date_debut="02/02/2021",
    date_fin="01/06/2021",
)


# --- Implémentation des modules

li_module = context.Notes.do_module_list()
mods_imp = []
for mod in li_module:
    if mod["semestre_id"] == 1:
        formsemestre_id = sem1["formsemestre_id"]
    elif mod["semestre_id"] == 2:
        formsemestre_id = sem2["formsemestre_id"]
    elif mod["semestre_id"] == 3:
        formsemestre_id = sem3["formsemestre_id"]
    else:
        formsemestre_id = sem4["formsemestre_id"]

    mi = G.create_moduleimpl(
        module_id=mod["module_id"],
        formsemestre_id=formsemestre_id,
        responsable_id="bach",
    )
    mods_imp.append(mi)

# --- Création des étudiants

etuds = []
for nom, prenom in [
    ("Semestre11", "EtudiantNumero1"),
    ("Semestre12", "EtudiantNumero2"),
    ("Semestre23", "EtudiantNumero3"),
    ("Semestre24", "EtudiantNumero4"),
    ("Semestre35", "EtudiantNumero5"),
    ("Semestre36", "EtudiantNumero6"),
    ("Semestre47", "EtudiantNumero7"),
    ("Semestre48", "EtudiantNumero8"),
]:
    etud = G.create_etud(
        nom=nom,
        prenom=prenom,
    )
    etuds.append(etud)

# --- Inscription des étudiants

for etud in etuds[0:2]:
    G.inscrit_etudiant(sem1, etud)

for etud in etuds[2:4]:
    G.inscrit_etudiant(sem2, etud)

for etud in etuds[4:6]:
    G.inscrit_etudiant(sem3, etud)

for etud in etuds[6:]:
    G.inscrit_etudiant(sem4, etud)

# --- Création d'une évaluation pour chaque UE

lim_sem1 = sco_moduleimpl.do_moduleimpl_list(
    context.Notes, formsemestre_id=sem1["formsemestre_id"], REQUEST=REQUEST
)
load_lim_sem1 = json.loads(lim_sem1)

lim_sem2 = sco_moduleimpl.do_moduleimpl_list(
    context.Notes, formsemestre_id=sem2["formsemestre_id"], REQUEST=REQUEST
)
load_lim_sem2 = json.loads(lim_sem2)

lim_sem3 = sco_moduleimpl.do_moduleimpl_list(
    context.Notes, formsemestre_id=sem3["formsemestre_id"], REQUEST=REQUEST
)
load_lim_sem3 = json.loads(lim_sem3)

lim_sem4 = sco_moduleimpl.do_moduleimpl_list(
    context.Notes, formsemestre_id=sem4["formsemestre_id"], REQUEST=REQUEST
)
load_lim_sem4 = json.loads(lim_sem4)


for moduleimpl_id, jour, description, coefficient in [
    (load_lim_sem1[1]["moduleimpl_id"], "02/09/2020", "InterroTestSemestre1", 1.0),
    (load_lim_sem1[2]["moduleimpl_id"], "03/09/2020", "InterroTestSemestre1", 1.0),
    (load_lim_sem2[1]["moduleimpl_id"], "03/02/2021", "InterroTestSemestre2", 1.0),
    (load_lim_sem2[8]["moduleimpl_id"], "04/02/2021", "InterroTestSemestre2", 1.0),
    (load_lim_sem3[3]["moduleimpl_id"], "02/09/2020", "InterroTestSemestre3", 1.0),
    (load_lim_sem3[9]["moduleimpl_id"], "03/09/2020", "InterroTestSemestre3", 1.0),
    (load_lim_sem3[15]["moduleimpl_id"], "04/09/2020", "InterroTestSemestre3", 1.0),
    (load_lim_sem4[3]["moduleimpl_id"], "03/02/2021", "InterroTestSemestre4", 1.0),
    (load_lim_sem4[9]["moduleimpl_id"], "04/02/2021", "InterroTestSemestre4", 1.0),
    (load_lim_sem4[13]["moduleimpl_id"], "05/02/2021", "InterroTestSemestre4", 1.0),
]:
    e = G.create_evaluation(
        moduleimpl_id=moduleimpl_id,
        jour=jour,
        description=description,
        coefficient=coefficient,
    )


# --- Saisie des notes des étudiants (un élève a 12, un autre a 7 pour chaque semestre)

lie1 = context.Notes.do_evaluation_list_in_formsemestre(
    formsemestre_id=sem1["formsemestre_id"]
)
lie2 = context.Notes.do_evaluation_list_in_formsemestre(
    formsemestre_id=sem2["formsemestre_id"]
)
lie3 = context.Notes.do_evaluation_list_in_formsemestre(
    formsemestre_id=sem3["formsemestre_id"]
)
lie4 = context.Notes.do_evaluation_list_in_formsemestre(
    formsemestre_id=sem4["formsemestre_id"]
)

for eval in lie1:
    for etud in etuds[:2]:
        if etud == etuds[0]:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=12.0
            )
        else:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=7.0
            )

for eval in lie2:
    for etud in etuds[2:4]:
        if etud == etuds[2]:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=12.0
            )
        else:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=7.0
            )


for eval in lie3:
    for etud in etuds[4:6]:
        if etud == etuds[4]:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=12.0
            )
        else:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=7.0
            )

for eval in lie4:
    for etud in etuds[6:]:
        if etud == etuds[6]:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=12.0
            )
        else:
            nb_changed, nb_suppress, existing_decisions = G.create_note(
                evaluation=eval, etud=etud, note=7.0
            )
