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

import com.capgemini.reports.bomlineholder.flattener.line.SnecmaLine;
import com.capgemini.reports.definition.r07report.data.TransformerR07;
import com.capgemini.reports.definition.r07report.data.filter.identifiers.ReceptorIdExtractor;
import com.capgemini.reports.definition.r07report.data.filter.identifiers.RepereIdExtractor;

import java.util.Comparator;

/**
 * Provides capabilities for comparing structure (receptor ID/repere NAME) between two {@link SnecmaLine}s.
 */
public class StructuralComparator implements Comparator<SnecmaLine<String, TransformerR07>> {

    private static final TransformerR07 RECEPTOR_ID_EXTRACTOR = new ReceptorIdExtractor();
    private static final TransformerR07 REPERE_ID_EXTRACTOR = new RepereIdExtractor();

    @Override
    public int compare(final SnecmaLine<String, TransformerR07> line1, final SnecmaLine<String, TransformerR07> line2) {
        String receptorId1 = line1.transform(RECEPTOR_ID_EXTRACTOR);
        String receptorId2 = line2.transform(RECEPTOR_ID_EXTRACTOR);
        int diff = receptorId1.compareTo(receptorId2);
        if (diff != 0) {
            return diff; // Structural difference: Receptor mismatch
        }
        String repereId1 = line1.transform(REPERE_ID_EXTRACTOR);
        String repereId2 = line2.transform(REPERE_ID_EXTRACTOR);
        diff = repereId1.compareTo(repereId2);
        return diff; // last possible structural difference: Repere mismatch
    }
}
