package com.capgemini.reports.definition.r07report.data.extractor.lines;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.bomlineholder.flattener.line.AbstractQuad;
import com.capgemini.reports.definition.r07report.data.TransformerR07;
import com.capgemini.reports.definition.r07report.data.filter.comparators.AbstractBomLineComparator;
import com.capgemini.reports.definition.r07report.data.filter.comparators.rf.RfComparator;
import com.capgemini.reports.exceptions.NotSupportedException;
import lombok.extern.slf4j.Slf4j;

/**
 * Represents a {@link AbstractQuad} line, with receptor and component, both having their own repere. <br>
 * Receptors and components can be Parts or Generics.<br>
 * For the R07, the Quad accepts a TransformerR07 which will generate a custom Id for it, for sorting purposes.
 */
@Slf4j
public class R07Quad extends AbstractQuad<String, TransformerR07> implements ComparableLine {

    /**
     * Builds a 'quad' object, using receptor, its repere, a component, and its repere.
     *
     * @param recepteurBL       a {@link BOMLineHolder}
     * @param repereRecepteurBL a {@link BOMLineHolder}
     * @param composantBL       a {@link BOMLineHolder}
     * @param repereComposantBL a {@link BOMLineHolder}
     * @param counter           the position of this line in the Bom
     */
    public R07Quad(final TcSession session, final BOMLineHolder recepteurBL, final BOMLineHolder repereRecepteurBL, final BOMLineHolder composantBL,
                   final BOMLineHolder repereComposantBL, final int counter) {
        super(session, recepteurBL, repereRecepteurBL, composantBL, repereComposantBL, counter);
    }

    @Override
    public String transform(final TransformerR07 transformer) {
        return transformer.makeIdentifier(this);
    }

    @Override
    public int compareLineContent(final ComparableLine other) {
        return other.compareContent(this);
    }

    @Override
    public int compareContent(final R07Quad other) {
        try {
            int cmp = AbstractBomLineComparator.compareReceptors(this.getRecepteurBL(), other.getRecepteurBL());
            if (cmp == 0) {
                cmp = RfComparator.compareRf(this.getRepereComposantBL(), other.getRepereComposantBL());
                if (cmp == 0) {
                    cmp = AbstractBomLineComparator.compareComponents(this.getComposantBL(), other.getComposantBL());
                }
            }
            return cmp;
        } catch (NotSupportedException e) {
            log.warn("Failed object comparison", e);
            return 1;
        }
    }

    @Override
    public int compareContent(final R07PartRfPart other) {
        return 1;
    }

    @Override
    public int compareContent(final R07ReceptorLine other) {
        return 1;
    }

    @Override
    public int compareContent(final R07ProductGeneric other) {
        return 1;
    }

    @Override
    public int compareContent(final R07AltPart other) {
        return 1;
    }
}
