package com.capgemini.reports.definition.r10report.lines;

import com.capgemini.framework.common.StringHelper;
import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.flattener.line.AbstractQuad;
import com.capgemini.reports.definition.r10report.DocumentExtractor;
import com.capgemini.reports.definition.r10report.R10Keys;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * Represents a {@link AbstractQuad} line, with receptor and component, both having their own repere. <br>
 * Receptors and components can be Parts or Generics.<br>
 * For the R07, the Quad accepts a TransformerR07 which will generate a custom Id for it, for sorting purposes.
 */
@Slf4j
@Getter
public class R10Quad extends AbstractQuad<List<Map<R10Keys, String>>, DocumentExtractor> {


    /**
     * Builds a 'quad' object, using receptor, its repere, a component, and its repere.
     *
     * @param recepteurBL       the {@link BOMLineHolder} receptor
     * @param repereRecepteurBL the {@link BOMLineHolder} repere of the receptor
     * @param composantBL       the {@link BOMLineHolder} composant
     * @param repereComposantBL the {@link BOMLineHolder} repere of the composant
     * @param counter           the position of this line in the Bom
     */
    public R10Quad(final TcSession session, final BOMLineHolder recepteurBL, final BOMLineHolder repereRecepteurBL, final BOMLineHolder composantBL,
                   final BOMLineHolder repereComposantBL, final int counter) {
        super(session, recepteurBL, repereRecepteurBL, composantBL, repereComposantBL, counter);
    }

    @Override
    public List<Map<R10Keys, String>> transform(final DocumentExtractor extractor) {
        try {
            return extractor.extract(this);
        } catch (KurtException e) {
            log.error("Unable to obtain the list of documents for this Quadline: " + getLineId(), e);
            // Make the line still appear by placing an entry in the list. However all fields will be empty.
            Map<R10Keys, String> mapWithEmptyStrings = new EnumMap<>(R10Keys.class);
            for (R10Keys key : R10Keys.values()) {
                mapWithEmptyStrings.put(key, StringHelper.EMPTY);
            }
            List<Map<R10Keys, String>> listWithOneMapWithEmptyString = new ArrayList<>();
            listWithOneMapWithEmptyString.add(mapWithEmptyStrings);
            return listWithOneMapWithEmptyString;
        }

    }

    @Override
    public String getClassement() {
        if (getComposant().isGeneric() || getComposant().isProduct()) {
            return "-";
        } else {
            return super.getClassement();
        }
    }

    @Override
    public Double getMass() {
        if (getComposant().isGeneric()) {
            return 0d;
        } else {
            return super.getMass();
        }
    }

    @Override
    public String getMassUnit() {
        if (getComposant().isGeneric() || SnecmaObjectTypes.PART_MAT_REVISION.equals(getComposant().getType())) {
            return "Kg";
        } else {
            return super.getMassUnit();
        }
    }

}
