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.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.flattener.line.AbstractProductGeneric;
import com.capgemini.reports.definition.r10report.DocumentExtractor;
import com.capgemini.reports.definition.r10report.R10Keys;
import lombok.extern.slf4j.Slf4j;

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

/**
 * Represents a Product - Generic line for R07 report.
 */
@Slf4j
public class R10ProductGeneric extends AbstractProductGeneric<List<Map<R10Keys, String>>, DocumentExtractor> {

    /**
     * Builds a Product - Generic line for the R07 report.
     *
     * @param productReceptor    the {@link BOMLineHolder} product (receptor)
     * @param genericComponent   the {@link BOMLineHolder} generic (component)
     * @param rfGenericComponent the {@link BOMLineHolder} RfGeneric
     * @param counter            the line num
     */
    public R10ProductGeneric(final TcSession session, final BOMLineHolder productReceptor, final BOMLineHolder genericComponent, final BOMLineHolder rfGenericComponent,
                             final int counter) {
        super(session, productReceptor, genericComponent, rfGenericComponent, 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 " + 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 Double getMass() {
        return 0d;
    }

    @Override
    public String getMassUnit() {
        return "Kg";
    }
}
