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

import com.capgemini.framework.common.StringHelper;
import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.flattener.line.AbstractTopLine;
import com.capgemini.reports.definition.r10report.DocumentExtractor;
import com.capgemini.reports.definition.r10report.R10Keys;
import com.capgemini.reports.snecmaobjects.generic.AbstractGenericRevision;
import lombok.extern.slf4j.Slf4j;

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

/**
 * Represents a TopLine of nemenclature (i.e. Product or Generic) for R07.
 */
@Slf4j
public class R10TopLine extends AbstractTopLine<List<Map<R10Keys, String>>, DocumentExtractor> {

    /**
     * Simple constructor.
     *
     * @param topBomLine the original topLine {@link BOMLineHolder}
     * @param lineNum    the line number (normally zero for a TopLine!)
     */
    public R10TopLine(final BOMLineHolder topBomLine, final int lineNum) {
        super(topBomLine, lineNum);
    }

    @Override
    public List<Map<R10Keys, String>> transform(final DocumentExtractor transformer) {
        try {
            return transformer.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;
        }
    }

    @Override
    public String getResponsableConception() {
        if (getBomLineObject().isGeneric()) {
            return ((AbstractGenericRevision) getBomLineObject()).getResponsableConception();
        } else {
            return StringHelper.EMPTY;
        }
    }

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

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

    @Override
    public String getMassUnit() {
        if (getBomLineObject().isGeneric()) {
            return "Kg";
        } else {
            return super.getMassUnit();
        }
    }
}
