package com.capgemini.reports.bomlineholder.flattener.line;

import com.capgemini.framework.common.LanguageHelper;
import com.capgemini.framework.common.StringHelper;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.reports.snecmaobjects.ISnecmaObject;
import com.capgemini.reports.snecmaobjects.part.*;
import lombok.extern.slf4j.Slf4j;

/**
 * Helps to extract the 'type' of a Part.
 */
@Slf4j
public final class PartTypeExtractorHelper {

    private static final String SLASH_SEPARATOR = " / ";

    private PartTypeExtractorHelper() {
        // Nothing!
    }

    /**
     * Helps to extract the 'type' of a Part, sometimes by concatenating several fields.<br>
     * For instance, a Part Aero's type will be TypePartAero1, TypePartAero2, TypePartAero3
     *
     * @param part a Part
     * @return the type
     */
    public static String extractType(final ISnecmaObject part) {
        String thisType = part.getType();
        String returnContent;
        if (SnecmaObjectTypes.ALTERNATE_REVISION.equals(thisType)) {
            returnContent = extract((AlternateRevision) part);
        } else if (SnecmaObjectTypes.DET_REVISION.equals(thisType)) {
            returnContent = extract((DETRevision) part);
        } else if (SnecmaObjectTypes.PART_AERO_REVISION.equals(thisType)) {
            returnContent = extract((PartAeroRevision) part);
        } else if (SnecmaObjectTypes.PART_NO_AERO_REVISION.equals(thisType)) {
            returnContent = LanguageHelper.french(((PartNoAeroRevision) part).getTypeNoAero());
        } else if (SnecmaObjectTypes.PART_ASSEMB_REVISION.equals(thisType)) {
            returnContent = extract((PartAssembRevision) part);
        } else if (SnecmaObjectTypes.PART_MAT_REVISION.equals(thisType)) {
            returnContent = "Matière";
        } else if (SnecmaObjectTypes.STANDARD_REVISION.equals(thisType)) {
            returnContent = LanguageHelper.french(((StandardRevision) part).getTypeStandard());
        } else {
            log.warn(StringHelper.concat("Could not extract Type from unsupported Part type: ", part.getItemId(), " of type ", thisType).toString());
            returnContent = "";
        }
        return returnContent;
    }

    private static String extract(final PartAssembRevision partAssemb) {
        return StringHelper.implode(SLASH_SEPARATOR, LanguageHelper.french(partAssemb.getTypePartMontage1()),
                LanguageHelper.french(partAssemb.getTypePartMontage2()));
    }

    private static String extract(final PartAeroRevision partAero) {
        return StringHelper.implode(SLASH_SEPARATOR, LanguageHelper.french(partAero.getTypeAero1()), LanguageHelper.french(partAero.getTypeAero2()),
                LanguageHelper.french(partAero.getTypeAero3()));
    }

    private static String extract(final DETRevision det) {
        return StringHelper.implode(SLASH_SEPARATOR, LanguageHelper.french(det.getTypeDet1()), LanguageHelper.french(det.getTypeDet2()));
    }

    private static String extract(final AlternateRevision alt) {
        return StringHelper.implode(SLASH_SEPARATOR, LanguageHelper.french(alt.getTypeAlt1()), LanguageHelper.french(alt.getTypeAlt2()));
    }

}
