package com.capgemini.reports.r23;

import com.capgemini.reports.common.AbstractSubmitServlet;
import com.capgemini.reports.definition.r23report.ExecutionR23;
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 R23 report.<br>
 * It prepares the data and creates the appropriate {@link ExecutionR23} instance that will carry out the job.
 */
@WebServlet("/report_r23/submit")
public class SubmitServletR23 extends AbstractSubmitServlet<ExecutionR23> {
    private static final long serialVersionUID = -7391550108287583064L;

    @Override
    public ExecutionR23 prepareReport(final HttpServletRequest request) {

        // Obtaining report parameters
        String productID = request.getParameter("product_id");
        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");
        String isWithFrozenUnitStr = request.getParameter("with_frozen_unit");
        String unitStr = request.getParameter("unit");
        String fromUnitStr = request.getParameter("from_unit");
        String frozenSinceLessThanStr = request.getParameter("frozen_since_less_than");

        boolean isSuperModelBOM;
        if ( isSuperModelBOMStr != null) {
            if (isSuperModelBOMStr.equals("on")) {
                isSuperModelBOM = true;
            } else {
                isSuperModelBOM = false;
            }
        } else {
            isSuperModelBOM = false;
        }

        boolean isWithFrozenUnit;
        if ( isWithFrozenUnitStr != null) {
            if (isWithFrozenUnitStr.equals("on")) {
                isWithFrozenUnit = true;
            } else {
                isWithFrozenUnit = false;
            }
        } else {
            isWithFrozenUnit = false;
        }

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

        ProductUnit productUnitForUnit = new ProductUnit(productID, unitStr);

        ProductUnit productUnitFromUnit = new ProductUnit(productID, fromUnitStr);

        // 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 ExecutionR23(productUnit, genericParam, monoMulti, effectivity, isSuperModelBOM, isWithFrozenUnit, productUnitForUnit, productUnitFromUnit, frozenSinceLessThanStr);
    }
}
