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

import com.capgemini.framework.common.StringHelper;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
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.generic.AbstractGenericRevision;

/**
 * Compares two Generic's contents.
 */
public class GenericComparator extends AbstractItemComparator {
    private static final long serialVersionUID = -1959398094059231129L;

    /**
     * Compares two Generic's contents.
     *
     * @param thisGen  this object
     * @param otherGen an other object
     * @return the diff
     * @throws NotSupportedException if objects are not Generics
     */
    public static int compareGenerics(final ISnecmaObject thisGen, final ISnecmaObject otherGen) throws NotSupportedException {
        String thisType = thisGen.getType();
        String otherType = otherGen.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 GenericComparator().compare(thisGen, otherGen);
        } else {
            throw new NotSupportedException(StringHelper.concat("Unsupported Generic type: ", thisGen.getItemId(), " of type ", thisType).toString());
        }
    }

    @Override
    public int compare(final ISnecmaObject thisItem, final ISnecmaObject otherItem) {
        // Compare Generic data
        AbstractGenericRevision thisGeneric = (AbstractGenericRevision) thisItem;
        AbstractGenericRevision otherGeneric = (AbstractGenericRevision) otherItem;
        int cmp = thisGeneric.getItemId().compareTo(otherGeneric.getItemId());
        if (cmp == 0) {
            cmp = safeCmp(thisGeneric.getName(), otherGeneric.getName());
            if (cmp == 0) {
                cmp = safeCmp(thisGeneric.getDesignationEN(), otherGeneric.getDesignationEN());
                if (cmp == 0) {
                    cmp = safeCmp(thisGeneric.getResponsableConception(), otherGeneric.getResponsableConception());
                }
            }
        }
        return cmp;
    }
}
