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 Generic/RF Generic/Part Number structure.
 *
 * @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 AbstractPartNumber<T, U> implements SnecmaLine<T, U> {

    private final BOMLineHolder generic;
    private final BOMLineHolder rfGeneric;
    private final BOMLineHolder partAero;
    private final int lineNumber;

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

    /**
     * Compares this {@link AbstractPartNumber} to an other one.
     *
     * @param other the other {@link AbstractPartNumber}
     * @return a difference number. 0 if equal.
     */
    public int compareTo(final AbstractPartNumber<T, U> other) {
        int cmp = generic.getSnecmaObject().getItemId().compareTo(other.generic.getSnecmaObject().getItemId());
        if (cmp == 0) {
            cmp = rfGeneric.getSnecmaObject().getName().compareTo(other.rfGeneric.getSnecmaObject().getName());
            if (cmp == 0) {
                if (partAero == null) {
                    return other.partAero == null ? 0 : 1;
                } else {
                    return partAero.getSnecmaObject().getItemId().compareTo(other.partAero.getSnecmaObject().getItemId());
                }
            }
        }
        return cmp;
    }

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

    @Override
    public String getLineId() {
        return generic.getSnecmaObject().getItemId() + "/" + rfGeneric.getSnecmaObject().getName() + "/" + partAero.getSnecmaObject().getItemId();
    }

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

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

}
