package com.capgemini.reports.builder;

import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r15r16r17reports.r16report.ExecutionR16;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.capgemini.reports.reports.parameters.ProductUnit;
import com.google.common.base.Joiner;

import java.util.Map;

/** Builds a R15 Report from language, part Id, product, unit, and "type d'impression". */
public class R16Builder extends AbstractR15R16R17Builder<ExecutionR16> {

    @Override
    protected ExecutionR16 make(Map<ReportCriteriaEnum, String> data, String language) throws InsufficientParametersException {
        // Get the request raw data
        String idTopGeneric = data.get(ReportCriteriaEnum.PART_ID); // It's actually a Gen for R16
        String product = data.get(ReportCriteriaEnum.PRODUCT);
        String unitStr = data.get(ReportCriteriaEnum.UNIT);
        String typeImpression = data.get(ReportCriteriaEnum.TYPE_IMPRESSION);

        // Check presence of all required args
        if (idTopGeneric == null || product == null || unitStr == null || typeImpression == null) {
            String dataStr = Joiner.on('|').withKeyValueSeparator("=").useForNull("null").join(data);
            throw new InsufficientParametersException("Missing parameter for R01 launch. Obtained: " + dataStr);
        }
        int unit;
        try {
            unit = Integer.parseInt(unitStr);
        } catch (NumberFormatException e) {
            throw new InsufficientParametersException("Unit parameter is not an int: " + unitStr, e);
        }
        ProductUnit aProductUnit = new ProductUnit(product, unit);

        // Build the report
        return new ExecutionR16(aProductUnit, idTopGeneric, typeImpression, language);
    }


}
