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

import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.visitor.AbstractBOMLineHolderVisitor;
import com.capgemini.reports.definition.r23report.visitors.R23VisitorKeys;
import com.capgemini.reports.definition.r23report.visitors.snecmaobject.R23ComponentVisitor;
import com.capgemini.reports.definition.r23report.visitors.snecmaobject.R23ReceptorVisitor;
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 R23 report.
 */
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class R23BOMLineHolderVisitor extends AbstractBOMLineHolderVisitor<R23VisitorKeys, String> {
    private static final long serialVersionUID = -3036778839181191276L;

    public R23BOMLineHolderVisitor() {
        super(new R23ReceptorVisitor(), new R23ComponentVisitor(), EffectivityBehaviourHelper.visitOnlyUnitManagedParts(),
                new R23AltOrDetBehaviour(), false, true, false);
    }

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

    @Override
    protected Map<R23VisitorKeys, String> visitThisComponent(final BOMLineHolder aBomLine) {
        Map<R23VisitorKeys, 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(R23VisitorKeys.ALT, aBomLine.getBomAlt());
            aMap.put(R23VisitorKeys.SB, aBomLine.getBomSb());
            aMap.put(R23VisitorKeys.QUANTITE_CUMULEE, aBomLine.getBomQtyCumul());
            aMap.put(R23VisitorKeys.PANACHE, aBomLine.getBomPanache());
            aMap.put(R23VisitorKeys.QUANTITE, aBomLine.getBomQty());
            aMap.put(R23VisitorKeys.UIN, aBomLine.getUin());
        }
        return aMap;
    }

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

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

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

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

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

    @Override
    protected void addToReport(final Map<R23VisitorKeys, String> aReceptor, final Map<R23VisitorKeys, 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);
    }
}
