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

import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.BOMLineHolderHelper;
import com.capgemini.reports.definition.r07report.data.filter.comparators.AbstractBomLineComparator;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.exceptions.NotFoundException;
import com.capgemini.reports.exceptions.NotSupportedException;
import com.capgemini.reports.snecmaobjects.admin.RFAdminRevision;
import lombok.extern.slf4j.Slf4j;

/**
 * Compares two RFs' {@link BOMLineHolder}s together.
 */
@Slf4j
public abstract class RfComparator extends AbstractBomLineComparator {
    private static final long serialVersionUID = -1907326362935233365L;

    /**
     * Compares two RFs of any subtype.
     */
    public static int compareRf(final BOMLineHolder thisRf, final BOMLineHolder otherRf) throws NotSupportedException {
        String thisType = thisRf.getSnecmaObject().getType();
        String otherType = otherRf.getSnecmaObject().getType();
        int cmp = thisType.compareTo(otherType);
        if (cmp == 0) {
            if (SnecmaObjectTypes.RF_GENERIC_REVISION.equals(thisType)) {
                cmp = new RfGenericComparator().compare(thisRf, otherRf);
            } else if (SnecmaObjectTypes.RF_PART_REVISION.equals(thisType)) {
                cmp = new RfPartComparator().compare(thisRf, otherRf);
            } else if (SnecmaObjectTypes.RF_PART_ASSEMB_REVISION.equals(thisType)) {
                cmp = new RfPartAssembComparator().compare(thisRf, otherRf);
            } else {
                throw new NotSupportedException("Unsupported RF type: " + thisRf.getSnecmaObject().getItemId() + " of type " + thisType);
            }
        }
        return cmp;
    }

    RFAdminRevision findRfAdmin(final BOMLineHolder aRF) {
        try {
            return BOMLineHolderHelper.findRfAdmin(aRF);
        } catch (NotFoundException | InvalidInputException nfe) {
            log.warn("No RF Admin AP found below: " + aRF.getSnecmaObject().getName(), nfe);
            return null;
        }
    }
}
