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
import sco_groups_view
import sco_abs_views

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
            )

# --- Département

li_dep = context.ScoDoc.list_depts(format="json", REQUEST=REQUEST)
print(li_dep)

# --- Formation

lif = context.Notes.formation_list(format="json", REQUEST=REQUEST, formation_id=f[0])
load_lif = json.loads(lif)
assert len(load_lif) == 1
assert load_lif[0]["formation_id"] == f[0]

exp = sco_formations.formation_export(formation_id=f[0], format="json", REQUEST=REQUEST)
load_exp = json.loads(exp)
assert load_exp["acronyme"] == "DUT Info"
assert load_exp["titre_officiel"] == "DUT Informatique"
assert load_exp["formation_code"] == "FCOD2"

# --- Semestre

li_sem = context.Notes.formsemestre_list(
    formation_id=f[0], format="json", REQUEST=REQUEST
)
load_li_sem = json.loads(li_sem)
assert len(load_li_sem) == 4

# --- Création des groupes

_ = sco_groups.partition_create(
    context.Scolarite,
    formsemestre_id=sem1["formsemestre_id"],
    partition_name="Eleve 1ere annee",
    REQUEST=REQUEST,
)

li1 = sco_groups.get_partitions_list(context.Scolarite, sem1["formsemestre_id"])

_ = sco_groups.createGroup(context.Scolarite, li1[0]["partition_id"], "Groupe S1A")

li_grp1 = sco_groups.get_partition_groups(context.Scolarite, li1[0])

for etud in etuds[0:2]:
    sco_groups.set_group(context.Scolarite, etud["etudid"], li_grp1[0]["group_id"])


# --- Groupes

li_part = sco_groups.formsemestre_partition_list(
    context.Scolarite,
    formsemestre_id=sem1["formsemestre_id"],
    format="json",
    REQUEST=REQUEST,
)
load_li_part = json.loads(li_part)
assert len(load_li_part) == 2  # 2 partition (defaut et eleve 1ere annee)
assert load_li_part[0]["formsemestre_id"] == sem1["formsemestre_id"]
assert len(load_li_part[0]["group"]) == 1  # 2 groupes S1A
assert load_li_part[0]["group"][0]["group_name"] == "Groupe S1A"
assert load_li_part[0]["group"][0]["group_id"] == li_grp1[0]["group_id"]
assert (
    load_li_part[0]["partition_id"]
    == load_li_part[0]["group"][0]["partition_id"]
    == li1[0]["partition_id"]
)
assert load_li_part[0]["partition_name"] == "Eleve 1ere annee"

vue_g1 = sco_groups_view.groups_view(
    group_ids=[li_grp1[0]["group_id"]],
    format="json",
    REQUEST=REQUEST,
)
load_vue_g1 = json.loads(vue_g1)
assert len(load_vue_g1) == 2
assert load_vue_g1[0][li1[0]["partition_id"]] == li_grp1[0]["group_name"]


# --- Etudiant

etudi = context.Scolarite.etud_info(
    etudid=etuds[0]["etudid"], format="json", REQUEST=REQUEST
)
load_etudi = json.loads(etudi)
assert load_etudi["prenom"] == "Etudiantnumero1"
assert load_etudi["nom"] == "SEMESTRE11"
assert load_etudi["etudid"] == etuds[0]["etudid"]

# --- Absences

for debut, fin, demijournee in [
    ("18/01/2021", "18/01/2021", 1),
    ("19/01/2021", "19/01/2021", 0),
]:
    sco_abs_views.doSignaleAbsence(
        datedebut=debut,
        datefin=fin,
        demijournee=demijournee,
        etudid=etuds[0]["etudid"],
        REQUEST=REQUEST,
    )

nb_abs = context.Absences.CountAbs(
    etuds[0]["etudid"], debut="09/01/2020", fin="02/01/2021"
)
assert nb_abs == 2

liste_abs = sco_abs_views.ListeAbsEtud(
    etuds[0]["etudid"], format="json", REQUEST=REQUEST
)
load_liste_abs = json.loads(liste_abs)
assert len(load_liste_abs) == 2

_ = sco_abs_views.doAnnuleAbsence(
    "15/01/2021",
    "15/01/2021",
    1,
    etudid=etuds[0]["etudid"],
    REQUEST=REQUEST,
)

# --- Module

# Voir test dans test_formation.