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

import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.reports.bomlineholder.BOMLineHolder;
import com.capgemini.reports.snecmaobjects.part.*;
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 AbstractAltPart<T, U> implements SnecmaLine<T, U> {

    private static final String NON = "Non";
    private static final String OUI = "Oui";
    private static final String EMPTY = "";

    private final BOMLineHolder altBL;
    private final AbstractPart child;
    private final int lineNumber;

    @Override
    public int compareTo(final SnecmaLine<T, U> other) {
        if (this.getClass().isAssignableFrom(other.getClass())) {
            return compareTo((AbstractAltPart<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 AbstractAltPart<T, U> other) {
        int cmp = altBL.getSnecmaObject().getItemId().compareTo(other.altBL.getSnecmaObject().getItemId());
        if (cmp != 0) {
            return cmp;
        } else {
            return child.getItemId().compareTo(other.child.getItemId());
        }
    }

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

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

    @Override
    public String getIDReceptor() {
        return altBL.getSnecmaObject().getItemId();
    }

    @Override
    public String getNameReceptor() {
        return altBL.getSnecmaObject().getName();
    }

    @Override
    public String getRevisionReceptor() {
        return altBL.getSnecmaObject().getRevisionId();
    }

    @Override
    public String getRepereName() {
        return EMPTY;
    }

    @Override
    public String getRepereRevision() {
        return EMPTY;
    }

    @Override
    public String getResponsableRepere() {
        return EMPTY;
    }

    @Override
    public String getSuiviMontage() {
        return EMPTY;
    }

    @Override
    public String getIDComponent() {
        return this.getChild().getItemId();
    }

    @Override
    public String getIDRevisionComponent() {
        return this.getChild().getRevisionId();
    }

    @Override
    public String getTypeReference() {
        return PartTypeExtractorHelper.extractType(this.getChild());
    }

    @Override
    public String getNameReference() {
        return this.getChild().getName();
    }

    @Override
    public String getReferenceConceptor() {
        if (this.getChild().isPart()) {
            return this.getChild().getRefCompleteConcepteur();
        }
        return EMPTY;
    }

    @Override
    public String getReferenceTransposed() {
        if (this.getChild().isPart()) {
            return this.getChild().getRefTransposeeSnecma();
        }
        return EMPTY;
    }

    @Override
    public String getReferenceAssociated() {
        if (this.getChild().isPart()) {
            return this.getChild().getRefAssociee();
        }
        return EMPTY;
    }

    @Override
    public String getResponsableConception() {
        if (this.getChild().isPart()) {
            return this.getChild().getResponsableConception();
        }
        return EMPTY;
    }

    @Override
    public String getArticleGestion() {
        if (SnecmaObjectTypes.isA(this.getChild().getType(), SnecmaObjectTypes.DET_REVISION, SnecmaObjectTypes.ALTERNATE_REVISION,
                SnecmaObjectTypes.PART_ASSEMB_REVISION)) {
            return OUI;
        }
        return EMPTY;
    }

    @Override
    public String getClassement() {
        if (this.getChild().isPart()) {
            if ("Part Mat Revision".equals(this.getChild().getType())) {
                return ((PartMatRevision) this.getChild()).getClassementMat();
            } else {
                return this.getChild().getClassement();
            }

        }
        return EMPTY;
    }

    @Override
    public String getSuiviUtilisation() {
        if (this.getChild().isPart()) {
            return this.getChild().getSuiviUtilisation();
        }
        return OUI;
    }

    @Override
    public String getSuiviDureeVie() {
        if (this.getChild().isPart()) {
            return this.getChild().getSuiviDureeVie();
        }
        return NON;
    }

    @Override
    public String[] getSuppliersList() {
        String type = this.getChild().getType();
        if (this.getChild().isPart() && !SnecmaObjectTypes.PART_NO_AERO_REVISION.equals(type)) {
            String[] lst;
            if (SnecmaObjectTypes.PART_AERO_REVISION.equals(type)) {
                lst = ((PartAeroRevision) this.getChild()).getLstFournisseurs();
            } else if (SnecmaObjectTypes.PART_MAT_REVISION.equals(type)) {
                lst = ((PartMatRevision) this.getChild()).getLstFournisseurs();
            } else if (SnecmaObjectTypes.STANDARD_REVISION.equals(type)) {
                lst = ((StandardRevision) this.getChild()).getLstFournisseurs();
            } else if (SnecmaObjectTypes.DET_REVISION.equals(type)) {
                lst = ((DETRevision) this.getChild()).getLstFournisseurs();
            } else if (SnecmaObjectTypes.ALTERNATE_REVISION.equals(type)) {
                lst = ((AlternateRevision) this.getChild()).getLstFournisseurs();
            } else {
                lst = new String[0];
            }
            return lst;
        }
        return new String[0];
    }

    @Override
    public String getALT() {
        return EMPTY;
    }

    @Override
    public String getSB() {
        return EMPTY;
    }

    @Override
    public String getQuantiteCumulee() {
        return EMPTY;
    }

    @Override
    public String getPanachage() {
        return EMPTY;
    }

    @Override
    public String getQuantite() {
        return EMPTY;
    }

    @Override
    public String getMaquetteInterneExterne() {
        return EMPTY;
    }

    @Override
    public Double getMass() {
        return child.getMass();
    }

    @Override
    public String getMassUnit() {
        return child.getMassUnit();
    }

    @Override
    public String getDesignationENG() {
        return child.getDesignationEN();
    }

}
