package com.capgemini.reports.definition.r07report.data.filter.comparators.generics;

import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.definition.r07report.data.filter.comparators.AbstractBomLineComparator;
import com.capgemini.reports.exceptions.NotSupportedException;

/**
 * Compares two Generics / Generics Assembs' {@link BOMLineHolder}s to each other.
 */
public class GenericBLComparator extends AbstractBomLineComparator {
    private static final long serialVersionUID = -1959398094059231129L;

    /**
     * Compares a two Generic/Generic Assemb contents.
     *
     * @param thisGen  this object
     * @param otherGen an other object
     * @return the difference
     * @throws NotSupportedException if either bomline does not hold a Generic / Generic Assemb
     */
    public static int compareGenerics(final BOMLineHolder thisGen, final BOMLineHolder otherGen) throws NotSupportedException {
        String thisType = thisGen.getSnecmaObject().getType();
        String otherType = otherGen.getSnecmaObject().getType();
        int cmp = thisType.compareTo(otherType);
        if (cmp != 0) {
            return cmp;
        }
        if (SnecmaObjectTypes.GENERIC_REVISION.equals(thisType) || SnecmaObjectTypes.GENERIC_ASSEMB_REVISION.equals(thisType)) {
            return new GenericBLComparator().compare(thisGen, otherGen);
        } else {
            throw new NotSupportedException("Unsupported Generic type: " + thisGen.getSnecmaObject().getItemId() + " of type " + thisType);
        }
    }

    @Override
    public int compare(final BOMLineHolder thisGenericBL, final BOMLineHolder otherGenericBL) {
        // Compare BOM data
        int cmp = safeCmp(thisGenericBL.getBomQty(), otherGenericBL.getBomQty());
        if (cmp == 0) {
            cmp = safeCmp(thisGenericBL.getBomAlt(), otherGenericBL.getBomAlt());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisGenericBL.getBomSb(), otherGenericBL.getBomSb());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisGenericBL.getBomQtyCumul(), otherGenericBL.getBomQtyCumul());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisGenericBL.getBomPanache(), otherGenericBL.getBomPanache());
        }
        if (cmp == 0) {
            // Compare Generic data
            cmp = new GenericComparator().compare(thisGenericBL.getSnecmaObject(), otherGenericBL.getSnecmaObject());
        }
        return cmp;
    }
}
