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.AbstractReceptorLine;
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 Receptor whose Repere does ot have a Component.
 */
@Slf4j
public class R10ReceptorLine extends AbstractReceptorLine<List<Map<R10Keys, String>>, DocumentExtractor> {

    /**
     * Builds a Receptor whose Repere does not have a Component.
     */
    public R10ReceptorLine(final TcSession session, final BOMLineHolder recepteurBL, final BOMLineHolder repereRecepteurBL, final int counter) {
        super(session, recepteurBL, repereRecepteurBL, 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 TopLine: " + 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;
        }
    }
}
