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.AbstractItemComparator;
import com.capgemini.reports.exceptions.NotSupportedException;
import com.capgemini.reports.snecmaobjects.ISnecmaObject;
import com.capgemini.reports.snecmaobjects.part.AbstractPart;

/**
 * Compares two {@link AbstractPart}'s {@link BOMLineHolder}s.
 */
public class PartComparator extends AbstractItemComparator {
    private static final long serialVersionUID = 8767701166124425908L;

    /**
     * Compares two Part's contents.
     *
     * @param part  this object
     * @param other an other object
     * @return the diff
     * @throws NotSupportedException when one of the input is not a Part
     */
    public static int compareParts(final ISnecmaObject part, final ISnecmaObject other) throws NotSupportedException {
        String thisType = part.getType();
        String otherType = other.getType();
        int cmp = thisType.compareTo(otherType);
        if (cmp == 0) {
            if (SnecmaObjectTypes.ALTERNATE_REVISION.equals(thisType)) {
                cmp = new AlternateComparator().compare(part, other);
            } else if (SnecmaObjectTypes.DET_REVISION.equals(thisType)) {
                cmp = new DetComparator().compare(part, other);
            } else if (SnecmaObjectTypes.PART_AERO_REVISION.equals(thisType)) {
                cmp = new PartAeroComparator().compare(part, other);
            } else if (SnecmaObjectTypes.PART_NO_AERO_REVISION.equals(thisType)) {
                cmp = new PartNoAeroComparator().compare(part, other);
            } else if (SnecmaObjectTypes.PART_ASSEMB_REVISION.equals(thisType)) {
                cmp = new PartAssembComparator().compare(part, other);
            } else if (SnecmaObjectTypes.PART_MAT_REVISION.equals(thisType)) {
                cmp = new PartMatComparator().compare(part, other);
            } else if (SnecmaObjectTypes.STANDARD_REVISION.equals(thisType)) {
                cmp = new StandardComparator().compare(part, other);
            } else {
                throw new NotSupportedException(StringHelper.concat("Unsupported Part type: ", part.getItemId(), " of type ", thisType).toString());
            }
        }
        return cmp;
    }

    @Override
    public int compare(final ISnecmaObject thisObj, final ISnecmaObject otherObj) {
        AbstractPart thisPart = (AbstractPart) thisObj;
        AbstractPart otherPart = (AbstractPart) otherObj;
        // Compare part data
        int cmp = thisPart.getItemId().compareTo(otherPart.getItemId());
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getName(), otherPart.getName());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getDesignationEN(), otherPart.getDesignationEN());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getRefCompleteConcepteur(), otherPart.getRefCompleteConcepteur());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getRefTransposeeSnecma(), otherPart.getRefTransposeeSnecma());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getResponsableConception(), otherPart.getResponsableConception());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getClassement(), otherPart.getClassement());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getSuiviUtilisation(), otherPart.getSuiviUtilisation());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getSuiviDureeVie(), otherPart.getSuiviDureeVie());
        }
        if (cmp == 0) {
            cmp = Double.compare(thisPart.getMass(), otherPart.getMass());
        }
        if (cmp == 0) {
            cmp = safeCmp(thisPart.getMassUnit(), otherPart.getMassUnit());
        }
        return cmp;
    }
}
