package com.capgemini.reports.bomlineholder.flattener.line;

import com.capgemini.framework.common.StringHelper;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.snecmaobjects.part.AbstractPart;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * Represents a direct Part/Part link. No RFs in-between.
 *
 * @param <U> a Transformer object producing results of type T
 * @param <T> the result of a transformation by object of type U
 */
@AllArgsConstructor
@Getter
public abstract class AbstractPartPart<T, U> implements SnecmaLine<T, U> {

    private final BOMLineHolder partReceptor;
    private final BOMLineHolder partComponent;
    private final int lineNumber;

    @Override
    public int compareTo(final SnecmaLine<T, U> other) {
        if (this.getClass().isAssignableFrom(other.getClass())) {
            return compareTo((AbstractPartPart<T, U>) other);
        } else {
            return getLineId().compareTo(other.getLineId());
        }
    }

    /**
     * Compares this {@link AbstractPartPart} to an other one.
     *
     * @param other the other {@link AbstractPartPart}
     * @return a difference number. 0 if equal.
     */
    public int compareTo(final AbstractPartPart<T, U> other) {
        int cmp = partReceptor.getSnecmaObject().getItemId().compareTo(other.partReceptor.getSnecmaObject().getItemId());
        if (cmp != 0) {
            return cmp;
        } else {
            return partComponent.getSnecmaObject().getItemId().compareTo(other.partComponent.getSnecmaObject().getItemId());
        }
    }

    @Override
    public String toString() {
        return "PartPart(" + getLineId() + ")";
    }

    @Override
    public String getLineId() {
        return partReceptor.getSnecmaObject().getItemId() + "/" + partComponent.getSnecmaObject().getItemId();
    }

    @Override
    public Double getMass() {
        if (partComponent.getSnecmaObject().isPart()) {
            return ((AbstractPart) partComponent.getSnecmaObject()).getMass();
        }
        return null;
    }

    @Override
    public String getMassUnit() {
        if (partComponent.getSnecmaObject().isPart()) {
            return ((AbstractPart) partComponent.getSnecmaObject()).getMassUnit();
        }
        return StringHelper.EMPTY;
    }

}
