/* BdfServer_Html - Copyright (c) 2020-2025 Vincent Calame - Exemole
 * Logiciel libre donné sous triple licence :
 * 1) selon les termes de la CeCILL V2
 * 2) selon les termes de l’EUPL V.1.1
 * 3) selon les termes de la GNU GPLv3
 * Voir le fichier licences.txt
 */


package fr.exemole.bdfserver.html.consumers;

import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import net.fichotheque.Subset;
import net.fichotheque.SubsetItem;
import net.fichotheque.SubsetKey;
import net.fichotheque.corpus.FicheMeta;
import net.fichotheque.junction.Junctions;
import net.fichotheque.junction.JunctionsBySubset;
import net.fichotheque.junction.Tie;
import net.fichotheque.utils.CorpusMetadataUtils;
import net.mapeadores.util.html.HtmlAttributes;
import net.mapeadores.util.html.HtmlPrinter;
import net.mapeadores.util.html.HtmlWrapper;
import net.mapeadores.util.localisation.Lang;


/**
 *
 * @author Vincent Calame
 */
public class JunctionSelection implements Consumer<HtmlPrinter> {

    public final static HtmlWrapper TREE = Tree.tree("global-JunctionTree");
    public final static HtmlWrapper OPEN_NODE = Tree.openNode("global-JunctionOpenContent");
    private final String paramName;
    private final List<JunctionsBySubset> junctionsBySubsetList;
    private final Lang workingLang;
    private final Locale formatLocale;

    public JunctionSelection(String paramName, List<JunctionsBySubset> junctionsBySubsetList, Lang workingLang, Locale formatLocale) {
        this.paramName = paramName;
        this.junctionsBySubsetList = junctionsBySubsetList;
        this.workingLang = workingLang;
        this.formatLocale = formatLocale;
    }

    @Override
    public void accept(HtmlPrinter hp) {
        hp
                .UL("global-JunctionList");
        for (JunctionsBySubset junctionsBySubset : junctionsBySubsetList) {
            Subset subset = junctionsBySubset.getSubset();
            SubsetKey subsetKey = subset.getSubsetKey();
            hp
                    .LI()
                    .P()
                    .__(SubsetTitle.init(subset, workingLang).subsetIcon(true))
                    ._P()
                    .__(TREE, () -> {
                        for (Junctions.Entry entry : junctionsBySubset.getJunctions().getEntryList()) {
                            SubsetItem subsetItem = entry.getSubsetItem();
                            for (Tie tie : entry.getJunction().getTieList()) {
                                HtmlAttributes inputAttributes = hp.name(paramName).value(getValue(subsetKey, subsetItem, tie.getMode()));
                                hp
                                        .__(Tree.checkboxLeaf(inputAttributes, () -> {
                                            hp
                                                    .__(printMode(hp, tie.getMode(), tie.getWeight()))
                                                    .__(printTitle(hp, subsetItem));
                                        }));
                            }
                        }
                    })
                    ._LI();
        }
        hp
                ._UL();
    }

    private String getValue(SubsetKey subsetKey, SubsetItem subsetItem, String mode) {
        StringBuilder buf = new StringBuilder();
        buf.append(subsetKey);
        if (mode.length() > 0) {
            buf.append('_');
            buf.append(mode);
        }
        buf.append('/');
        buf.append(subsetItem.getId());
        return buf.toString();
    }

    private boolean printMode(HtmlPrinter hp, String mode, int weight) {
        if ((weight > 1) || (mode.length() > 0)) {
            hp
                    .__escape(" <");
            if (mode.length() > 0) {
                hp
                        .__escape(mode);
                if (weight > 1) {
                    hp
                            .__escape('_');
                }
            }
            if (weight > 1) {
                hp
                        .__append(weight);
            }
            hp
                    .__escape("> ");
            return true;
        } else {
            return false;
        }
    }

    private boolean printTitle(HtmlPrinter hp, SubsetItem subsetItem) {
        if (subsetItem instanceof FicheMeta) {
            hp
                    .__escape(CorpusMetadataUtils.getFicheTitle((FicheMeta) subsetItem, workingLang, formatLocale));
        }
        return true;
    }


}
