# -*- mode: python -*-
# -*- coding: utf-8 -

""""
Creation de 10 étudiants, formation, semestre, ue, module, absences le matin, l'apres midi, la journée compléte 
et justification d'absences, supression d'absences, création de listes d'absences, création de billet
et creation d'un groupe.

Fonction de l'API utilisé :
 - doSignaleAbsence
 - doJustifAbsence
 - AddBilletAbs
 - XMLgetBilletAbsence
 - listeBilletEtud
 - list_abs_just
 - CountAbsJust
 - ListAbsNonJust
 - ListJustifs
 - list_abs_jour
 - ListAbsInRange

Fonction renvoyant du code HTML :
 - EtatAbsences
 - SignaleAbsenceGrSemestre
 - SignaleAbsenceGrHebdo
 - EtatAbsenceDate
 - AddBilletAbsenceForm
 - AnnuleAbsenceEtud
 - ProcessBilletAbsenceForm
 - JustifAbsenceEtud
 - doAnnuleJustif

"""

import random


REQUEST = REQUEST  # pylint: disable=undefined-variable
import scotests.sco_fake_gen as sco_fake_gen  # pylint: disable=import-error
import sco_abs
import sco_abs_views
import sco_abs_notification
import ZAbsences

G = sco_fake_gen.ScoFake(xxx)
G.verbose = False

# --- Création d'étudiants
etuds = [G.create_etud(code_nip=None) for _ in range(10)]

# --- Création d'une formation
f = G.create_formation(acronyme="")
ue = G.create_ue(formation_id=f["formation_id"], acronyme="TST1", titre="ue test")
mat = G.create_matiere(ue_id=ue["ue_id"], titre="matière test")
mod = G.create_module(
    matiere_id=mat["matiere_id"],
    code="TSM1",
    coefficient=1.0,
    titre="module test",
    ue_id=ue["ue_id"],  # faiblesse de l'API
    formation_id=f["formation_id"],  # faiblesse de l'API
)

# --- Mise place d'un semestre
sem = G.create_formsemestre(
    formation_id=f["formation_id"],
    semestre_id=1,
    date_debut="01/01/2021",
    date_fin="30/06/2021",
)

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

# --- Inscription des étudiants
for etud in etuds:
    G.inscrit_etudiant(sem, etud)

# --- Création d'un groupe

_ = sco_groups.partition_create(
    formsemestre_id=sem["formsemestre_id"],
    partition_name="Eleve",
    REQUEST=REQUEST,
)
li1 = sco_groups.get_partitions_list(sem["formsemestre_id"])
_ = sco_groups.createGroup(li1[0]["partition_id"], "Groupe 1")

# --- Affectation des élèves dans des groupes

li_grp1 = sco_groups.get_partition_groups(li1[0])
for etud in etuds:
    sco_groups.set_group(etud["etudid"], li_grp1[0]["group_id"])

# --- Saisie absences
etudid = etuds[0]["etudid"]

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

_ = sco_abs_views.doSignaleAbsence(
    "22/01/2021",
    "22/01/2021",
    demijournee=1,
    etudid=etuds[1]["etudid"],
    REQUEST=REQUEST,
)

# --- Justification de certaines absences

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

# --- Test

_ = context.Absences.doSignaleAbsenceGrSemestre(
    dates="2021-01-13,2021-01-25",
    etudids=etuds[5]["etudid"] + "," + etuds[6]["etudid"],
    destination="",
    REQUEST=REQUEST,
)  #  <----- rien ne se passe (pb parametre ?)

# --- Création de billets

b1 = context.Absences.AddBilletAbsence(
    begin="2021-01-22 00:00",
    end="2021-01-22 23:59",
    etudid=etudid,
    description="abs du 22",
    justified=False,
    code_nip=etuds[0]["code_nip"],
    code_ine=etuds[0]["code_ine"],
    REQUEST=REQUEST,
)

b2 = context.Absences.AddBilletAbsence(
    begin="2021-01-15 00:00",
    end="2021-01-15 23:59",
    etudid=etudid,
    description="abs du 15",
    code_nip=etuds[0]["code_nip"],
    code_ine=etuds[0]["code_ine"],
    REQUEST=REQUEST,
)

# --- XMLgetBilletEtud

xml_bi = context.Absences.XMLgetBilletsEtud(etudid=etudid, REQUEST=REQUEST)
print(1)
print(xml_bi)
print(1)

# --- Supression de billet
li_bi = context.Absences.listeBilletsEtud(etudid=etudid, REQUEST=REQUEST, format="json")
load_li_bi = json.loads(li_bi)

# _ = context.Absences.deleteBilletAbsence(load_li_bi[1]["billet_id"], REQUEST=REQUEST)

li_bi2 = context.Absences.listeBilletsEtud(
    etudid=etudid, REQUEST=REQUEST, format="json"
)
load_li_bi2 = json.loads(li_bi)

# assert len(load_li_bi2) == 1
# assert load_li_bi2[0]["description"] == "abs du 22"

# --- Créaton de listes

li_abs_just = sco_abs.list_abs_just(etudid=etudid, datedebut="01/01/2021")
nb_abs_just = sco_abs.count_abs_just(etudid, debut="01/01/2021", fin="06/30/2021")
assert len(li_abs_just) == nb_abs_just
assert li_abs_just[0]["etudid"] == etudid
assert li_abs_just[0]["matin"] == True
assert li_abs_just[0]["jour"] == datetime.date(2021, 1, 15)
assert li_abs_just[2]["jour"] == datetime.date(2021, 1, 19)
assert li_abs_just[2]["matin"] == False


li_abs_njust = context.Absences.ListeAbsNonJust(etudid=etudid, datedebut="01/01/2021")
nb_abs = context.Absences.CountAbs(etudid, debut="01/01/2021", fin="06/30/2021")
assert len(li_abs_njust) == nb_abs - nb_abs_just
assert li_abs_njust[0]["matin"] == True
assert li_abs_njust[0]["etudid"] == etudid
assert li_abs_njust[0]["jour"] == datetime.date(2021, 1, 22)


li_just = sco_abs.list_abs_justifs(etudid=etudid, datedebut="2021-01-01")
assert len(li_just) == 4
li_just2 = context.Absences.list_abs_justifs(
    etudid=etudid, datedebut="2021-01-01", datefin="2021-01-18"
)
assert len(li_just2) == 2


li_jour = sco_abs.list_abs_jour(date="01/22/2021")
assert len(li_jour) == 2
li_jour2 = sco_abs.list_abs_jour(date="01/18/2021")
assert len(li_jour2) == 1


li_range = context.Absences.ListAbsInRange(etudid, debut="01/01/2021", fin="06/01/2021")
assert len(li_range) == 5

# li_xml = context.Absences.XMLgetAbsEtud(beg_date="01/01/2021", end_date="01/06/2021", REQUEST=REQUEST)
# print(li_xml)  need etudid

# --- Annulation d'absence

# context.Absences.AnnuleAbsencesDatesNoJust( dates="22/01/2021")


# --- Fonction renvoyant du code HTML

etat_abs = context.Absences.EtatAbsences(REQUEST=REQUEST)

# cal_abs = sco_abs_views.CalAbs( REQUEST=REQUEST)  #<--- retourne html + need etudid : how?

sag = context.Absences.SignaleAbsenceGrSemestre(
    datedebut="15/01/2021",
    datefin="22/01/2021",
    group_ids=[li_grp1[0]["group_id"]],
    REQUEST=REQUEST,
)

sagh = context.Absences.SignaleAbsenceGrHebdo(
    datelundi="18/01/2021",
    group_ids=[li_grp1[0]["group_id"]],
    destination="",
    REQUEST=REQUEST,
)

grp_abs_d = context.Absences.EtatAbsencesDate(
    group_ids=[li_grp1[0]["group_id"]], date="22/01/2021", REQUEST=REQUEST
)

billet_form = context.Absences.AddBilletAbsenceForm(etudid=etudid, REQUEST=REQUEST)

# an_abs = AnnuleAbsenceEtud( REQUEST=REQUEST)   #<- retourne html + need etudid : how?

# proc = context.Absences.ProcessBilletAbsenceForm(billet_id=load_li_bi[0]["billet_id"], REQUEST=REQUEST) #error

# just_form = sco_abs_views.JustifAbsenceEtud( REQUEST=REQUEST)

# delete_just = sco_abs_views.doAnnuleJustif( datedebut0="22/01/2021", datefin0="22/01/2021", demijournee=2, REQUEST=REQUEST)


"""
Commentaire :

Certaines fonctions ont besoin de l'id de l'étudiant mais n'ont pas ce parametre comment le récupérer ?
(CalAbs, SignaleAbsenceEtud, AnnuleAbsenceEtud, JustifAbsenceEtud, doAnnuleJustif, XMLgetAbsEtud)

D'autres fonctions renvoient du HTML -> comment les tester en python?

doSignaleAbsenceGrSemestre -> ne fonctionne pas (pb au niveau de l'écriture des parametres au lancement 
                              de la fonction?)

XMLgetBilletsEtud, deleteBilletAbsence -> fonction get ne retourne rien et delete ne supprime rien
                                          (lancer fichier test_billet)

AnnuleAbsencesDatesNoJust -> error line 323, jour='2'  (2 doit être un int et non un string?)

"""
