package com.capgemini.reports.definition.r01report.visitors.bomlineholder;

import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.visitor.AbstractBOMLineHolderVisitor;
import com.capgemini.reports.definition.r01report.visitors.R01VisitorKeys;
import com.capgemini.reports.definition.r01report.visitors.snecmaobject.R01ComponentVisitor;
import com.capgemini.reports.definition.r01report.visitors.snecmaobject.R01ReceptorVisitor;
import com.capgemini.reports.releasestatus.EffectivityBehaviourHelper;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * Visitor of an expansion of BOMLineHolder for the R01 report.
 */
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class R01BOMLineHolderVisitor extends AbstractBOMLineHolderVisitor<R01VisitorKeys, String> {
    private static final long serialVersionUID = -2136631482354508026L;

    public R01BOMLineHolderVisitor() {
        super(new R01ReceptorVisitor(), new R01ComponentVisitor(), EffectivityBehaviourHelper.visitAnyPart(),
            new R01AltOrDetBehaviour(), true, true, true);
    }

    @Override
    protected Map<R01VisitorKeys, String> visitThisReceptor(final BOMLineHolder aBomLine) {
        return aBomLine.getSnecmaObject().accept(receptorVisitor);
    }

    @Override
    protected Map<R01VisitorKeys, String> visitThisComponent(final BOMLineHolder aBomLine) {
        Map<R01VisitorKeys, String> aMap = aBomLine.getSnecmaObject().accept(componentVisitor);

        // Put the BOMLine properties into the map
        if ((aBomLine.getSnecmaObject().isGeneric()) || (aBomLine.getSnecmaObject().isPart()) || aBomLine.getSnecmaObject().isSuperModel()) {
            aMap.put(R01VisitorKeys.ALT, aBomLine.getBomAlt());
            aMap.put(R01VisitorKeys.SB, aBomLine.getBomSb());
            aMap.put(R01VisitorKeys.QUANTITE_CUMULEE, aBomLine.getBomQtyCumul());
            aMap.put(R01VisitorKeys.PANACHE, aBomLine.getBomPanache());
            aMap.put(R01VisitorKeys.QUANTITE, aBomLine.getBomQty());
            aMap.put(R01VisitorKeys.UIN, aBomLine.getUin());
        }
        return aMap;
    }

    @Override
    protected void visitSpecificFirstLine(final BOMLineHolder aTopBomLine) {
        Map<R01VisitorKeys, String> theFirstLine = aTopBomLine.getSnecmaObject().accept(getComponentVisitor());
        theReportTable.add(theFirstLine);
    }

    @Override
    protected void mergeReceptorAndRepereReceptor(final Map<R01VisitorKeys, String> aReceptor, final Map<R01VisitorKeys, String> aRepereReceptor) {
        aReceptor.putAll(aRepereReceptor);
    }

    @Override
    protected void mergeComponentAndRepereComponent(final Map<R01VisitorKeys, String> aComponent, final Map<R01VisitorKeys, String> aRepereComponent) {
        aComponent.putAll(aRepereComponent);
    }

    @Override
    protected void mergeRepereReceptorandAdmin(final Map<R01VisitorKeys, String> aRepereReceptor, final Map<R01VisitorKeys, String> anAdmin) {
        aRepereReceptor.putAll(anAdmin);
    }

    @Override
    protected void mergeRepereComponentandAdmin(final Map<R01VisitorKeys, String> aRepereComponent, final Map<R01VisitorKeys, String> anAdmin) {
        aRepereComponent.putAll(anAdmin);
    }

    @Override
    protected void addToReport(final Map<R01VisitorKeys, String> aReceptor, final Map<R01VisitorKeys, String> aComponent) {
        // Put the Receptor and Repere Receptor in the Component and modify the Component
        // Do not modify the receptor
        aComponent.putAll(aReceptor);

        // Add to the R21 report
        theReportTable.add(aComponent);
    }
}
