from functools import reduce
from urllib import request
from io import BytesIO
#import barcode

from django import template
import datetime
import operator

from BasicData.custom_fonctions import calcTime
from BasicData.models import Note, SyllabusEcue, Enseignant, Reclamations, EnseignantUfr, Etudiant, Evaluation, Ue, \
    Ecue, Annee, Deliberation, DeliberationUe, EmploiDuTemps2, SeanceCours

# déclarations de la librairie
from django.db.models import Sum

from BasicData.templatetags.custom_tag import get_etudiant_moyenne_ue3, check_session2_ue, \
    get_etudiant_session_2_moy_ue, get_credit_ue
from BasicData.templatetags.custom_tag_obj import get_etudiant_list

register = template.Library()
# -----------------

# TODO: Today date
y = datetime.datetime.now().year
m = datetime.datetime.now().month
d = datetime.datetime.now().day
date = str(d) + "-" + str(m) + "-" + str(y)


# @register.filter(name='barcode_generate')
# def barcode_generate(uid):
#     rv = BytesIO()
#     # code = barcode.get('code128', b, writer=SVGWriter())
#     code = barcode.get('code128', uid,
#     writer=barcode.writer.SVGWriter())
#     code.write(rv)
#
#     rv.seek(0)
#     # get rid of the first bit of boilerplate
#     rv.readline()
#     rv.readline()
#     rv.readline()
#     rv.readline()
#     # read the svg tag into a string
#     svg = rv.read()
#     return svg.decode("utf-8")


# TODO: GET MOYENNE DE CLASSE ===========
@register.filter(name='get_moyenne_de_classe')
def get_moyenne_de_classe(args,ue):
    get_annee = args[0]
    niveau = args[1]
    ufr = args[2]

    etudiants = get_etudiant_list([get_annee.id,niveau.id,ufr.id])

    cred_valids_S2 = 0
    ue_non_valides_S2 = 0
    total_moy_ue_CD_S2 = 0
    total_credit_S2 = 0
    stat_total_moy = 0
    moySem_CD_S2 =0

    for etudiant in etudiants:
        # bonus = get_compens_note([etudiant, get_annee], ue)  # Note de compensation
        MUE = get_etudiant_moyenne_ue3([etudiant, get_annee], ue)
        delibUe = DeliberationUe.objects.filter(ue=ue, annee=get_annee, session__id=1).last()
        sd = 10
        if delibUe:
            sd = delibUe.note

        if MUE >= sd:
            # total_moy_ue_CD_S2 += (MUE + bonus) * get_credit_ue(ue)
            total_moy_ue_CD_S2 += MUE * get_credit_ue(ue)
            cred_valids_S2 += get_credit_ue(ue)
        elif check_session2_ue(etudiant, [ue, get_annee]) > 0:
            moy_s2 = get_etudiant_session_2_moy_ue(etudiant, [ue, get_annee])

            # total_moy_ue_CD_S2 += (moy_s2 + bonus) * get_credit_ue(ue)
            total_moy_ue_CD_S2 += moy_s2 * get_credit_ue(ue)
            if moy_s2 >= sd:
                cred_valids_S2 += get_credit_ue(ue)
            else:
                ue_non_valides_S2 += 1
        else:
            # total_moy_ue_CD_S2 += (MUE + bonus) * get_credit_ue(ue)
            total_moy_ue_CD_S2 += MUE * get_credit_ue(ue)
            ue_non_valides_S2 += 1

        if total_credit_S2 == 0:
            pass
        else:
            # 1 on calcul la moyenne
            moySem_CD_S2 = total_moy_ue_CD_S2 / total_credit_S2

        stat_total_moy += moySem_CD_S2
    if len(etudiants):
        stat_moy = stat_total_moy / len(etudiants)
    else:
        stat_moy = 0

    return stat_moy