package com.capgemini.reports.jasper.fillers;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.Box.Filler;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * This class builds a simple report.
 */
public class MonoFiller implements JasperFiller {
    private static final Logger LOGGER = LoggerFactory.getLogger(MonoFiller.class);
    private final JasperReport compiledTemplate;
    private final Collection<Map<String, ?>> data;
    private final Map<String, Object> parameters;

    /**
     * Builds a basic Report {@link Filler}.
     *
     * @param compiledTemplate the {@link JasperReport} used as template
     * @param parameters       some report-wide parameters, stored as a {@link Map} with their name as a String key
     * @param data             a {@link List} of {@link Map Maps} representing a series of objects to be added to the report one after the other
     */
    public MonoFiller(final JasperReport compiledTemplate, final Collection<Map<String, ?>> data, final Map<String, Object> parameters) {
        this.compiledTemplate = compiledTemplate;
        this.data = data;
        this.parameters = parameters;
    }

    @Override
    public final JasperPrint generate() throws JRException {
        LOGGER.debug("JASPER filling");
        long start = System.currentTimeMillis();
        JasperPrint print = JasperFillManager.fillReport(compiledTemplate, parameters, new JRMapCollectionDataSource(data));
        LOGGER.debug("JASPER report Filling time : {} ms, {} pages expected", System.currentTimeMillis() - start, print.getPages().size());
        return print;
    }
}
