package com.capgemini.reports.r01;

import com.capgemini.reports.common.AbstractSubmitServlet;
import com.capgemini.reports.definition.r01report.ExecutionR01;
import com.capgemini.reports.reports.parameters.EffectivityDate;
import com.capgemini.reports.reports.parameters.Generics;
import com.capgemini.reports.reports.parameters.MonoMulti;
import com.capgemini.reports.reports.parameters.ProductUnit;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * This class receives demands to perform a R01 report.<br>
 * It prepares the data and creates the appropriate {@link ExecutionR01} instance that will carry out the job.
 */
@WebServlet("/report_r01/submit")
public class SubmitServletR01 extends AbstractSubmitServlet<ExecutionR01> {

    private static final long serialVersionUID = 6034599018215480837L;

    @Override
    public ExecutionR01 prepareReport(final HttpServletRequest request) {

        // Obtaining report parameters
        String productID = request.getParameter("product_id");
        String unitStr = request.getParameter("unit");
        String generic = request.getParameter("generic");
        String genericAssemb = request.getParameter("generic_assemb");
        String effectivityStr = request.getParameter("datepicker");
        String monoMultiStr = request.getParameter("mono_multi");
        String isSuperModelBOMStr = request.getParameter("modellist");
        boolean isSuperModelBOM;
        if ( isSuperModelBOMStr != null) {
            if (isSuperModelBOMStr.equals("on")) {
                isSuperModelBOM = true;
            } else {
                isSuperModelBOM = false;
            }
        } else {
            isSuperModelBOM = false;
        }

        // Checking product-unit relationship
        ProductUnit productUnit = new ProductUnit(productID, unitStr);

        // Checking mono-multi
        MonoMulti monoMulti = MonoMulti.build(monoMultiStr);

        // Checking generics
        Generics genericParam = new Generics(productUnit, generic, genericAssemb);

        // Checking effectivity
        EffectivityDate effectivity = new EffectivityDate(effectivityStr);

        return new ExecutionR01(productUnit, genericParam, monoMulti, effectivity, isSuperModelBOM);
    }
}
