# -*- coding: utf-8 -*-

##############################################################################
#
# ScoDoc
#
# Copyright (c) 1999 - 2023 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
#
##############################################################################

"""
Vues "modernes" des formsemestre
Emmanuel Viennet, 2023
"""

from flask import flash, redirect, render_template, url_for
from flask import g, request

from app.decorators import (
    scodoc,
    permission_required,
)
from app.forms.formsemestre import change_formation
from app.models import Formation, FormSemestre
from app.scodoc import sco_formations, sco_formation_versions
from app.scodoc.sco_permissions import Permission
from app.views import notes_bp as bp
from app.views import ScoData


@bp.route(
    "/formsemestre_change_formation/<int:formsemestre_id>", methods=["GET", "POST"]
)
@scodoc
@permission_required(Permission.ScoImplement)
def formsemestre_change_formation(formsemestre_id: int):
    """Propose de changer un formsemestre de formation.
    Cette opération est bien sûr impossible... sauf si les deux formations sont identiques.
    Par exemple, on vient de créer une formation, et on a oublié d'y associé un formsemestre
    existant.
    """
    formsemestre = FormSemestre.get_formsemestre(formsemestre_id)
    formation_dict = sco_formations.formation_export_dict(
        formsemestre.formation, export_external_ues=True, ue_reference_style="acronyme"
    )
    formations = [
        formation
        for formation in Formation.query.filter_by(
            dept_id=formsemestre.dept_id, acronyme=formsemestre.formation.acronyme
        )
        if formation.id != formsemestre.formation.id
        and sco_formation_versions.formations_are_equals(
            formation, formation2_dict=formation_dict
        )
    ]
    form = change_formation.gen_formsemestre_change_formation_form(formations)
    if request.method == "POST" and form.validate:
        if not form.cancel.data:
            new_formation_id = form.radio_but.data
            if new_formation_id is None:  # pas de choix radio
                flash("Pas de formation sélectionnée !")
                return render_template(
                    "formsemestre/change_formation.j2",
                    form=form,
                    formations=formations,
                    formsemestre=formsemestre,
                    sco=ScoData(formsemestre=formsemestre),
                )
            else:
                new_formation: Formation = Formation.query.filter_by(
                    dept_id=g.scodoc_dept_id, formation_id=new_formation_id
                ).first_or_404()
                sco_formation_versions.formsemestre_change_formation(
                    formsemestre, new_formation
                )
                flash("Formation du semestre modifiée")
        return redirect(
            url_for(
                "notes.formsemestre_status",
                scodoc_dept=g.scodoc_dept,
                formsemestre_id=formsemestre_id,
            )
        )
    # GET
    return render_template(
        "formsemestre/change_formation.j2",
        form=form,
        formations=formations,
        formsemestre=formsemestre,
        sco=ScoData(formsemestre=formsemestre),
    )
