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

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

"""
Formulaires configuration logos

Contrib @jmp, dec 21
"""
import re

from flask import flash, url_for, redirect, render_template
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import SelectField, SubmitField, FormField, validators, FieldList
from wtforms.fields.simple import StringField, HiddenField

from app import AccessDenied
from app.models import Departement
from app.models import ScoDocSiteConfig
from app.scodoc import sco_logos, html_sco_header
from app.scodoc import sco_utils as scu
from app.scodoc.sco_config_actions import (
    LogoDelete,
    LogoUpdate,
    LogoInsert,
    BonusSportUpdate,
)

from flask_login import current_user

from app.scodoc.sco_logos import find_logo

JAVASCRIPTS = html_sco_header.BOOTSTRAP_MULTISELECT_JS + []

CSSSTYLES = html_sco_header.BOOTSTRAP_MULTISELECT_CSS

# class ItemForm(FlaskForm):
#     """Unused Generic class to document common behavior for classes
#     * ScoConfigurationForm
#     * DeptForm
#     * LogoForm
#     Some or all of these implements:
#         * Composite design pattern (ScoConfigurationForm and DeptForm)
#             - a FieldList(FormField(ItemForm))
#             - FieldListItem are created by browsing the model
#             - index dictionnary to provide direct access to a SubItemForm
#             - the direct access method (get_form)
#         * have some information added to be displayed
#             - information are collected from a model object
#     Common methods:
#         * build(model) (not for LogoForm who has no child)
#             for each child:
#                 * create en entry in the FieldList for each subitem found
#                 * update self.index
#                 * fill_in additional information into the form
#                 * recursively calls build for each chid
#             some spécific information may be added after standard processing
#             (typically header/footer description)
#          * preview(data)
#             check the data from a post and build a list of operations that has to be done.
#             for a two phase process:
#               * phase 1 (list all opérations)
#               * phase 2 (may be confirmation and execure)
#                   - if no op found: return to the form with a message 'Aucune modification trouvée'
#                   - only one operation found: execute and go to main page
#                   - more than 1 operation found. asked form confirmation (and execution if confirmed)
#
#     Someday we'll have time to refactor as abstract classes but Abstract FieldList makes this a bit complicated
#     """

#  Terminology:
# dept_id : identifies a dept in modele (= list_logos()). None designates globals logos
# dept_key : identifies a dept in this form only (..index[dept_key], and fields 'dept_key').
#           'GLOBAL' designates globals logos (we need a string value to set up HiddenField
GLOBAL = "_"


def dept_id_to_key(dept_id):
    if dept_id is None:
        return GLOBAL
    return dept_id


def dept_key_to_id(dept_key):
    if dept_key == GLOBAL:
        return None
    return dept_key


class AddLogoForm(FlaskForm):
    """Formulaire permettant l'ajout d'un logo (dans un département)"""

    dept_key = HiddenField()
    name = StringField(
        label="Nom",
        validators=[
            validators.regexp(
                r"^[a-zA-Z0-9-]*$",
                re.IGNORECASE,
                "Ne doit comporter que lettres, chiffres ou -",
            ),
            validators.Length(
                max=20, message="Un nom ne doit pas dépasser 20 caractères"
            ),
            validators.DataRequired("Nom de logo requis (alphanumériques ou '-')"),
        ],
    )
    upload = FileField(
        label="Sélectionner l'image",
        validators=[
            FileAllowed(
                scu.LOGOS_IMAGES_ALLOWED_TYPES,
                f"n'accepte que les fichiers image {', '.join(scu.LOGOS_IMAGES_ALLOWED_TYPES)}",
            ),
            validators.DataRequired("Fichier image manquant"),
        ],
    )
    do_insert = SubmitField("ajouter une image")

    def __init__(self, *args, **kwargs):
        kwargs["meta"] = {"csrf": False}
        super().__init__(*args, **kwargs)

    def validate_name(self, name):
        dept_id = dept_key_to_id(self.dept_key.data)
        if dept_id == GLOBAL:
            dept_id = None
        if find_logo(logoname=name.data, dept_id=dept_id) is not None:
            raise validators.ValidationError("Un logo de même nom existe déjà")

    def select_action(self):
        if self.data["do_insert"]:
            if self.validate():
                return LogoInsert.build_action(self.data)
        return None


class LogoForm(FlaskForm):
    """Embed both presentation of a logo (cf. template file configuration.html)
    and all its data and UI action (change, delete)"""

    dept_key = HiddenField()
    logo_id = HiddenField()
    upload = FileField(
        label="Remplacer l'image",
        validators=[
            FileAllowed(
                scu.LOGOS_IMAGES_ALLOWED_TYPES,
                f"n'accepte que les fichiers image {', '.join(scu.LOGOS_IMAGES_ALLOWED_TYPES)}",
            )
        ],
    )
    do_delete = SubmitField("Supprimer l'image")

    def __init__(self, *args, **kwargs):
        kwargs["meta"] = {"csrf": False}
        super().__init__(*args, **kwargs)
        self.logo = find_logo(
            logoname=self.logo_id.data, dept_id=dept_key_to_id(self.dept_key.data)
        ).select()
        self.description = None
        self.titre = None
        self.can_delete = True
        if self.dept_key.data == GLOBAL:
            if self.logo_id.data == "header":
                self.can_delete = False
                self.description = ""
                self.titre = "Logo en-tête"
            if self.logo_id.data == "footer":
                self.can_delete = False
                self.titre = "Logo pied de page"
                self.description = ""
        else:
            if self.logo_id.data == "header":
                self.description = "Se substitue au header défini au niveau global"
                self.titre = "Logo en-tête"
            if self.logo_id.data == "footer":
                self.description = "Se substitue au footer défini au niveau global"
                self.titre = "Logo pied de page"

    def select_action(self):
        if self.do_delete.data and self.can_delete:
            return LogoDelete.build_action(self.data)
        if self.upload.data and self.validate():
            return LogoUpdate.build_action(self.data)
        return None


class DeptForm(FlaskForm):
    dept_key = HiddenField()
    dept_name = HiddenField()
    add_logo = FormField(AddLogoForm)
    logos = FieldList(FormField(LogoForm))

    def __init__(self, *args, **kwargs):
        kwargs["meta"] = {"csrf": False}
        super().__init__(*args, **kwargs)

    def is_local(self):
        if self.dept_key.data == GLOBAL:
            return None
        return True

    def select_action(self):
        action = self.add_logo.form.select_action()
        if action:
            return action
        for logo_entry in self.logos.entries:
            logo_form = logo_entry.form
            action = logo_form.select_action()
            if action:
                return action
        return None

    def get_form(self, logoname=None):
        """Retourne le formulaire associé à un logo. None si pas trouvé"""
        if logoname is None:  # recherche de département
            return self
        return self.index.get(logoname, None)


def _make_dept_id_name():
    """Cette section assute que tous les départements sont traités (y compris ceux qu'ont pas de logo au départ)
    et détermine l'ordre d'affichage des DeptForm (GLOBAL d'abord, puis par ordre alpha de nom de département)
    -> [ (None, None), (dept_id, dept_name)...  ]"""
    depts = [(None, GLOBAL)]
    for dept in (
        Departement.query.filter_by(visible=True).order_by(Departement.acronym).all()
    ):
        depts.append((dept.id, dept.acronym))
    return depts


def _ordered_logos(modele):
    """sort logoname alphabetically but header and footer moved at start. (since there is no space in logoname)"""

    def sort(name):
        if name == "header":
            return " 0"
        if name == "footer":
            return " 1"
        return name

    order = sorted(modele.keys(), key=sort)
    return order


def _make_dept_data(dept_id, dept_name, modele):
    dept_key = dept_id_to_key(dept_id)
    data = {
        "dept_key": dept_key,
        "dept_name": dept_name,
        "add_logo": {"dept_key": dept_key},
    }
    logos = []
    if modele is not None:
        for name in _ordered_logos(modele):
            logos.append({"dept_key": dept_key, "logo_id": name})
    data["logos"] = logos
    return data


def _make_depts_data(modele):
    data = []
    for dept_id, dept_name in _make_dept_id_name():
        data.append(
            _make_dept_data(
                dept_id=dept_id, dept_name=dept_name, modele=modele.get(dept_id, None)
            )
        )
    return data


def _make_data(bonus_sport, modele):
    data = {
        "bonus_sport_func_name": bonus_sport,
        "depts": _make_depts_data(modele=modele),
    }
    return data


class ScoDocConfigurationForm(FlaskForm):
    "Panneau de configuration général"
    bonus_sport_func_name = SelectField(
        label="Fonction de calcul des bonus sport&culture",
        choices=[
            (x, x if x else "Aucune")
            for x in ScoDocSiteConfig.get_bonus_sport_func_names()
        ],
    )
    depts = FieldList(FormField(DeptForm))

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    # def _set_global_logos_infos(self):
    #     "specific processing for globals items"
    #     global_header = self.get_form(logoname="header")
    #     global_header.description = (
    #         "image placée en haut de certains documents documents PDF."
    #     )
    #     global_header.titre = "Logo en-tête"
    #     global_header.can_delete = False
    #     global_footer = self.get_form(logoname="footer")
    #     global_footer.description = (
    #         "image placée en pied de page de certains documents documents PDF."
    #     )
    #     global_footer.titre = "Logo pied de page"
    #     global_footer.can_delete = False

    # def _build_dept(self, dept_id, dept_name, modele):
    #     dept_key = dept_id or GLOBAL
    #     data = {"dept_key": dept_key}
    #     entry = self.depts.append_entry(data)
    #     entry.form.build(dept_name, modele.get(dept_id, {}))
    #     self.index[str(dept_key)] = entry.form

    # def build(self, modele):
    #     "Build the Form hierachy (DeptForm, LogoForm) and add extra data (from modele)"
    #     # if entries already initialized (POST). keep subforms
    #     self.index = {}
    #     # create entries in FieldList (one entry per dept
    #     for dept_id, dept_name in self.dept_id_name:
    #         self._build_dept(dept_id=dept_id, dept_name=dept_name, modele=modele)
    #     self._set_global_logos_infos()

    def get_form(self, dept_key=GLOBAL, logoname=None):
        """Retourne un formulaire:
         * pour un département (get_form(dept_id)) ou à un logo (get_form(dept_id, logname))
         * propre à un département (get_form(dept_id, logoname) ou global (get_form(logoname))
        retourne None si le formulaire cherché ne peut être trouvé
        """
        dept_form = self.index.get(dept_key, None)
        if dept_form is None:  # département non trouvé
            return None
        return dept_form.get_form(logoname)

    def select_action(self):
        if (
            self.data["bonus_sport_func_name"]
            != ScoDocSiteConfig.get_bonus_sport_func_name()
        ):
            return BonusSportUpdate(self.data)
        for dept_entry in self.depts:
            dept_form = dept_entry.form
            action = dept_form.select_action()
            if action:
                return action
        return None


def configuration():
    """Panneau de configuration général"""
    auth_name = str(current_user)
    if not current_user.is_administrator():
        raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name)
    form = ScoDocConfigurationForm(
        data=_make_data(
            bonus_sport=ScoDocSiteConfig.get_bonus_sport_func_name(),
            modele=sco_logos.list_logos(),
        )
    )
    if form.is_submitted():
        action = form.select_action()
        if action:
            action.execute()
            flash(action.message)
            return redirect(
                url_for(
                    "scodoc.configuration",
                )
            )
    return render_template(
        "configuration.html",
        scodoc_dept=None,
        title="Configuration ScoDoc",
        form=form,
    )