package com.capgemini.reports.builder;

import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r25report.ExecutionR25;
import com.capgemini.reports.exceptions.InsufficientParametersException;
import com.capgemini.reports.execution.AbstractExecutionReport;
import com.capgemini.reports.reports.parameters.ProductUnit;
import com.google.common.base.Joiner;

import java.util.Map;

/** Builds a R25 Report from language, part Id, product, unit, and "type d'impression". */
public class R25Builder implements ReportBuilder {

  @Override
  public AbstractExecutionReport<?, ?> build(Map<ReportCriteriaEnum, String> data) throws InsufficientParametersException {
    // Get the request raw data
    String language = data.get(ReportCriteriaEnum.LANGUAGE);

    // Check presence of all required args
    if (language == null) {
      String dataStr = Joiner.on('|').withKeyValueSeparator("=").useForNull("null").join(data);
      throw new InsufficientParametersException("Missing parameter for R25 launch. Obtained: " + dataStr);
    }

    // Build the report
    return make(data, language);
  }


  protected ExecutionR25 make(Map<ReportCriteriaEnum, String> data, String language) throws InsufficientParametersException {
    /* Get the request raw data */
    String product = data.get(ReportCriteriaEnum.PRODUCT);
    String unitStr = data.get(ReportCriteriaEnum.UNIT);
    String typeImpression = data.get(ReportCriteriaEnum.TYPE_IMPRESSION);
    ProductUnit aProductUnit = new ProductUnit(product, unitStr);

    /* Check presence of all required args */
    if (product == null || typeImpression == null) {
      String dataStr = Joiner.on('|').withKeyValueSeparator("=").useForNull("null").join(data);
      throw new InsufficientParametersException("Missing parameter for R25 launch. Obtained: " + dataStr);
    }
    return new ExecutionR25(aProductUnit, typeImpression, language);

  }
}