/* BdfServer_Get - Copyright (c) 2013-2026 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.get.streamproducers;

import fr.exemole.bdfserver.api.instruction.BdfParameters;
import fr.exemole.bdfserver.html.BdfServerHtmlProducer;
import java.text.DecimalFormatSymbols;
import java.util.Collection;
import net.fichotheque.SubsetItem;
import net.fichotheque.exportation.table.ColDef;
import net.fichotheque.exportation.table.ColDefTitleProvider;
import net.fichotheque.exportation.table.SubsetTable;
import net.fichotheque.exportation.table.SumCellConverter;
import net.fichotheque.exportation.table.TableExportConstants;
import net.fichotheque.exportation.table.TableExportContext;
import net.fichotheque.tools.exportation.table.TableExportEngine;
import net.fichotheque.utils.TableDefUtils;
import net.mapeadores.util.html.HtmlTableWriter;


/**
 *
 * @author Vincent Calame
 */
public class TableExportHtmlProducer extends BdfServerHtmlProducer {

    private final SubsetTable subsetTable;
    private final Collection<SubsetItem> subsetItems;
    private final String headerType;
    private final boolean fragment;
    private final TableExportContext tableExportContext;
    private final SumCellConverter sumCellConverter;

    public TableExportHtmlProducer(BdfParameters bdfParameters, SubsetTable subsetTable, Collection<SubsetItem> subsetItems, String headerType, boolean fragment, TableExportContext tableExportContext, SumCellConverter sumCellConverter) {
        super(bdfParameters);
        this.subsetTable = subsetTable;
        this.subsetItems = subsetItems;
        this.headerType = headerType;
        this.fragment = fragment;
        this.tableExportContext = tableExportContext;
        this.sumCellConverter = sumCellConverter;
        setReversePath("../");
    }

    @Override
    public void printHtml() {
        if (!fragment) {
            start();
        }
        this
                .TABLE()
                .__(printHeader());
        HtmlTableWriter htmlTableWriter = new HtmlTableWriter(this, new DecimalFormatSymbols(bdfUser.getFormatLocale()));
        TableExportEngine.exportSubset(subsetTable, htmlTableWriter, sumCellConverter, subsetItems);
        this
                ._TABLE();
        if (!fragment) {
            end();
        }
    }

    private boolean printHeader() {
        ColDefTitleProvider colDefTitleProvider = getColDefTitleProvider();
        if (colDefTitleProvider == null) {
            return false;
        }
        this
                .TR();
        for (ColDef colDef : subsetTable.getColDefList()) {
            this
                    .TH()
                    .__escape(colDefTitleProvider.getTitle(colDef))
                    ._TH();
        }
        this
                ._TR();
        return true;
    }

    private ColDefTitleProvider getColDefTitleProvider() {
        switch (headerType) {
            case TableExportConstants.COLUMNTITLE_HEADER:
                return TableDefUtils.getLabelColDefTitleProvider(workingLang, tableExportContext.getSourceLabelProvider(), subsetTable.getSubset());
            case TableExportConstants.COLUMNKEY_HEADER:
                return TableDefUtils.KEY_COLDEFTITLEPROVIDER;
            default:
                return null;
        }
    }


}
