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

import com.capgemini.framework.common.StringHelper;
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 Parts' {@link BOMLineHolder}s, for any type of Part.
 */
public class PartBLComparator extends AbstractBomLineComparator {
    private static final long serialVersionUID = 1847560066180416682L;

    /**
     * Compare two part bomlines together.
     *
     * @param part  this object
     * @param other an other object
     * @return the diff
     * @throws NotSupportedException if objects are of the wrong type
     */
    public static int compareParts(final BOMLineHolder part, final BOMLineHolder other) throws NotSupportedException {
        String thisType = part.getSnecmaObject().getType();
        String otherType = other.getSnecmaObject().getType();
        int cmp = thisType.compareTo(otherType);
        if (cmp != 0) {
            return cmp;
        }
        // Same types
        if (SnecmaObjectTypes.ALTERNATE_REVISION.equals(thisType)) {
            cmp = new AlternateBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.DET_REVISION.equals(thisType)) {
            cmp = new DetBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.PART_AERO_REVISION.equals(thisType)) {
            cmp = new PartAeroBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.PART_NO_AERO_REVISION.equals(thisType)) {
            cmp = new PartNoAeroBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.PART_ASSEMB_REVISION.equals(thisType)) {
            cmp = new PartAssembBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.PART_MAT_REVISION.equals(thisType)) {
            cmp = new PartMatBLComparator().compare(part, other);
        } else if (SnecmaObjectTypes.STANDARD_REVISION.equals(thisType)) {
            cmp = new StandardBLComparator().compare(part, other);
        } else {
            throw new NotSupportedException(
                    StringHelper.concat("Unsupported Part type: ", part.getSnecmaObject().getItemId(), " of type ", thisType).toString());
        }
        return cmp;
    }

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

}
