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

##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2022 Emmanuel Viennet.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   Emmanuel Viennet      emmanuel.viennet@viennet.net
#
##############################################################################

"""API ScoDoc 9
"""
# PAS ENCORE IMPLEMENTEE, juste un essai
# Pour P. Bouron, il faudrait en priorité l'équivalent de
# Scolarite/Notes/moduleimpl_withmodule_list (alias scodoc7 do_moduleimpl_withmodule_list)
# Scolarite/Notes/evaluation_create
# Scolarite/Notes/evaluation_delete
# Scolarite/Notes/formation_list
# Scolarite/Notes/formsemestre_list
# Scolarite/Notes/formsemestre_partition_list
# Scolarite/Notes/groups_view
# Scolarite/Notes/moduleimpl_status
# Scolarite/setGroups
from datetime import datetime

from flask import jsonify, request, g, send_file
from sqlalchemy.sql import func

from app import db, log
from app.api import bp, requested_format
from app.api.auth import token_auth
from app.api.errors import error_response
from app import models
from app.models import FormSemestre, FormSemestreInscription, Identite
from app.models import ApcReferentielCompetences
from app.scodoc.sco_permissions import Permission


@bp.route("/list_depts", methods=["GET"])
@token_auth.login_required
def list_depts():
    depts = models.Departement.query.filter_by(visible=True).all()
    data = [d.to_dict() for d in depts]
    return jsonify(data)


@bp.route("/etudiants/courant", methods=["GET"])
@token_auth.login_required
def etudiants():
    """Liste de tous les étudiants actuellement inscrits à un semestre
    en cours.
    """
    # Vérification de l'accès: permission Observateur sur tous les départements
    # (c'est un exemple à compléter)
    if not g.current_user.has_permission(Permission.ScoObservateur, None):
        return error_response(401, message="accès interdit")

    query = db.session.query(Identite).filter(
        FormSemestreInscription.formsemestre_id == FormSemestre.id,
        FormSemestreInscription.etudid == Identite.id,
        FormSemestre.date_debut <= func.now(),
        FormSemestre.date_fin >= func.now(),
    )
    return jsonify([e.to_dict_bul(include_urls=False) for e in query])


######################## Departements ##################################


@bp.route("/departements", methods=["GET"])
@token_auth.login_required
def departements():
    """
    Liste des ids de départements
    """
    depts = models.Departement.query.filter_by(visible=True).all()
    data = [d.id for d in depts]
    return jsonify(data)


@bp.route("/departements/<string:dept>/etudiants/liste/<int:sem_id>", methods=["GET"])
@token_auth.login_required
def liste_etudiants(dept, *args, sem_id):  # XXX TODO A REVOIR
    """
    Liste des étudiants d'un département
    """
    # Test si le sem_id à été renseigné ou non
    if sem_id is not None:
        # Récupération du/des depts
        list_depts = models.Departement.query.filter(
            models.Departement.acronym == dept,
            models.FormSemestre.semestre_id == sem_id,
        )
        list_etuds = []
        for dept in list_depts:
            # Récupération des étudiants d'un département
            x = models.Identite.query.filter(models.Identite.dept_id == dept.getId())
            for y in x:
                # Ajout des étudiants dans la liste global
                list_etuds.append(y)
    else:
        list_depts = models.Departement.query.filter(
            models.Departement.acronym == dept,
            models.FormSemestre.semestre_id == models.Departement.formsemestres,
        )
        list_etuds = []
        for dept in list_depts:
            x = models.Identite.query.filter(models.Identite.dept_id == dept.getId())
            for y in x:
                list_etuds.append(y)

    data = [d.to_dict() for d in list_etuds]
    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route("/departements/<string:dept>/semestres_actifs", methods=["GET"])
@token_auth.login_required
def liste_semestres_actifs(dept):  # TODO : changer nom
    """
    Liste des semestres actifs d'un départements donné
    """
    # Récupération de l'id du dept
    dept_id = models.Departement.query.filter(models.Departement.acronym == dept)
    # Puis ici récupération du FormSemestre correspondant
    depts_actifs = models.FormSemestre.query.filter_by(
        etat=True,
        dept_id=dept_id,
    )
    data = [da.to_dict() for da in depts_actifs]

    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route("/referentiel_competences/<int:referentiel_competence_id>")
@token_auth.login_required
def referentiel_competences(referentiel_competence_id):
    """
    Le référentiel de compétences
    """
    ref = ApcReferentielCompetences.query.get_or_404(referentiel_competence_id)
    return jsonify(ref.to_dict())


####################### Etudiants ##################################


@bp.route("/etudiant/<int:etudid>", methods=["GET"])
@token_auth.login_required
def etudiant(etudid):
    """
    Un dictionnaire avec les informations de l'étudiant correspondant à l'id passé en paramètres.
    """
    etud: Identite = Identite.query.get_or_404(etudid)
    return jsonify(etud.to_dict_bul())


@bp.route("/etudiant/<int:etudid>/semestre/<int:sem_id>/bulletin", methods=["GET"])
@token_auth.login_required
def etudiant_bulletin_semestre(etudid, sem_id):
    """
    Le bulletin d'un étudiant en fonction de son id et d'un semestre donné
    """
    # return jsonify(models.BulAppreciations.query.filter_by(etudid=etudid, formsemestre_id=sem_id))
    return error_response(501, message="Not implemented")


@bp.route(
    "/formsemestre/<int:formsemestre_id>/departements/<string:dept>/etudiant/nip/<int:NIP>/releve",
    methods=["GET"],
)
@bp.route(
    "/formsemestre/<int:formsemestre_id>/departements/<string:dept>/etudiant/id/<int:etudid>/releve",
    methods=["GET"],
)
@bp.route(
    "/formsemestre/<int:formsemestre_id>/departements/<string:dept>/etudiant/ine/<int:numScodoc>/releve",
    methods=["GET"],
)
@token_auth.login_required
def etudiant_bulletin(formsemestre_id, dept, etudid, format="json", *args, size):
    """
    Un bulletin de note
    """
    formsemestres = models.FormSemestre.query.filter_by(id=formsemestre_id)
    depts = models.Departement.query.filter_by(acronym=dept)
    etud = ""

    data = []
    if args[0] == "short":
        pass
    elif args[0] == "selectevals":
        pass
    elif args[0] == "long":
        pass
    else:
        return "erreur"

    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route(
    "/etudiant/<int:etudid>/semestre/<int:formsemestre_id>/groups", methods=["GET"]
)
@token_auth.login_required
def etudiant_groups(etudid: int, formsemestre_id: int):
    """
    Liste des groupes auxquels appartient l'étudiant dans le semestre indiqué
    """
    semestre = models.FormSemestre.query.filter_by(id=formsemestre_id)
    etudiant = models.Identite.query.filter_by(id=etudid)

    groups = models.Partition.query.filter(
        models.Partition.formsemestre_id == semestre,
        models.GroupDescr.etudiants == etudiant,
    )
    data = [d.to_dict() for d in groups]
    # return jsonify(data)

    return error_response(501, message="Not implemented")


#######################" Programmes de formations #########################


@bp.route("/formations", methods=["GET"])
@bp.route("/formations/<int:formation_id>", methods=["GET"])
@token_auth.login_required
def formations(formation_id: int):
    """
    Liste des formations
    """
    formations = models.Formation.query.filter_by(id=formation_id)
    data = [d.to_dict() for d in formations]
    # return jsonify(data)

    return error_response(501, message="Not implemented")


@bp.route("/formations/formation_export/<int:formation_id>", methods=["GET"])
@token_auth.login_required
def formation_export(formation_id: int, export_ids=False):
    """
    La formation, avec UE, matières, modules
    """
    return error_response(501, message="Not implemented")


###################### UE #######################################


@bp.route(
    "/departements/<string:dept>/formations/programme/<string:sem_id>", methods=["GET"]
)
@token_auth.login_required
def eus(dept: str, sem_id: int):
    """
    Liste des UES, ressources et SAE d'un semestre
    """
    return error_response(501, message="Not implemented")


######## Semestres de formation ###############


@bp.route("/formations/formsemestre/<int:formsemestre_id>", methods=["GET"])
@bp.route("/formations/apo/<int:etape_apo>", methods=["GET"])
@token_auth.login_required
def formsemestre(
    id: int,
):
    """
    Information sur les formsemestres
    """
    return error_response(501, message="Not implemented")


############ Modules de formation ##############


@bp.route("/formations/moduleimpl/<int:moduleimpl_id>", methods=["GET"])
@bp.route(
    "/formations/moduleimpl/<int:moduleimpl_id>/formsemestre/<int:formsemestre_id>",
    methods=["GET"],
)
@token_auth.login_required
def moduleimpl(id: int):
    """
    Liste de moduleimpl
    """
    return error_response(501, message="Not implemented")


########### Groupes et partitions ###############


@bp.route("/partitions/<int:formsemestre_id>", methods=["GET"])
@token_auth.login_required
def partition(formsemestre_id: int):
    """
    La liste de toutes les partitions d'un formsemestre
    """
    partitions = models.Partition.query.filter_by(id=formsemestre_id)
    data = [d.to_dict() for d in partitions]

    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route(
    "/partitions/formsemestre/<int:formsemestre_id>/groups/group_ids?with_codes=&all_groups=&etat=",
    methods=["GET"],
)
@token_auth.login_required
def groups(formsemestre_id: int, group_ids: int):
    """
    Liste des étudiants dans un groupe
    """
    return error_response(501, message="Not implemented")


@bp.route(
    "/partitions/set_groups?partition_id=<int:partition_id>&groups=<int:groups>&groups_to_delete=<int:groups_to_delete>&groups_to_create=<int:groups_to_create>",
    methods=["POST"],
)
@token_auth.login_required
def set_groups(
    partition_id: int, groups: int, groups_to_delete: int, groups_to_create: int
):
    """
    Set les groups
    """
    return error_response(501, message="Not implemented")


####### Bulletins de notes ###########


@bp.route("/evaluations/<int:moduleimpl_id>", methods=["GET"])
@token_auth.login_required
def evaluations(moduleimpl_id: int):
    """
    Liste des évaluations à partir de l'id d'un moduleimpl
    """
    evals = models.Evaluation.query.filter_by(id=moduleimpl_id)
    data = [d.to_dict() for d in evals]

    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route("/evaluations/eval_notes/<int:evaluation_id>", methods=["GET"])
@token_auth.login_required
def evaluation_notes(evaluation_id: int):
    """
    Liste des notes à partir de l'id d'une évaluation donnée
    """
    evals = models.Evaluation.query.filter_by(id=evaluation_id)
    notes = evals.get_notes()

    data = [d.to_dict() for d in notes]
    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route(
    "/evaluations/eval_set_notes?eval_id=<int:eval_id>&etudid=<int:etudid>&note=<int:note>",
    methods=["POST"],
)
@token_auth.login_required
def evaluation_set_notes(eval_id: int, etudid: int, note: float):
    """
    Set les notes d'une évaluation pour un étudiant donnée
    """
    return error_response(501, message="Not implemented")


############## Absences #############


@bp.route("/absences/<int:etudid>", methods=["GET"])
@bp.route("/absences/<int:etudid>/abs_just_only", methods=["GET"])
def absences(etudid: int):
    """
    Liste des absences d'un étudiant donnée
    """
    abs = models.Absence.query.filter_by(id=etudid)

    data = [d.to_dict() for d in abs]

    # return jsonify(data)
    return error_response(501, message="Not implemented")


@bp.route("/absences/abs_signale", methods=["POST"])
@token_auth.login_required
def abs_signale():
    """
    Retourne un html
    """
    return error_response(501, message="Not implemented")


@bp.route("/absences/abs_annule", methods=["POST"])
@token_auth.login_required
def abs_annule():
    """
    Retourne un html
    """
    return error_response(501, message="Not implemented")


@bp.route("/absences/abs_annule_justif", methods=["POST"])
@token_auth.login_required
def abs_annule_justif():
    """
    Retourne un html
    """
    return error_response(501, message="Not implemented")


@bp.route(
    "/absences/abs_group_etat/?group_ids=<int:group_ids>&date_debut=date_debut&date_fin=date_fin",
    methods=["GET"],
)
@token_auth.login_required
def abs_groupe_etat(
    group_ids: int, date_debut, date_fin, with_boursier=True, format="html"
):
    """
    Liste des absences d'un ou plusieurs groupes entre deux dates
    """
    return error_response(501, message="Not implemented")


################ Logos ################


@bp.route("/logos", methods=["GET"])
@token_auth.login_required
def liste_logos(format="json"):
    """
    Liste des logos définis pour le site scodoc.
    """
    return error_response(501, message="Not implemented")


@bp.route("/logos/<string:nom>", methods=["GET"])
@token_auth.login_required
def recup_logo_global(nom: str):
    """
    Retourne l'image au format png ou jpg
    """
    return error_response(501, message="Not implemented")


@bp.route("/departements/<string:dept>/logos", methods=["GET"])
@token_auth.login_required
def logo_dept(dept: str):
    """
    Liste des logos définis pour le département visé.
    """
    return error_response(501, message="Not implemented")


@bp.route("/departement/<string:dept>/logos/<string:nom>", methods=["GET"])
@token_auth.login_required
def recup_logo_dept_global(dept: str, nom: str):
    """
    L'image format png ou jpg
    """
    return error_response(501, message="Not implemented")