package com.capgemini.reports.snecmaobjects.product;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.changeobjects.UnitRevision;
import com.capgemini.reports.snecmaobjects.AbstractSnecmaObject;
import com.capgemini.reports.snecmaobjects.visitor.IComponentVisitor;
import com.capgemini.reports.snecmaobjects.visitor.IReceptorVisitor;
import com.teamcenter.soa.client.model.ModelObject;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

import static com.capgemini.framework.teamcenter.object.SnecmaObjectProperties.*;
import static com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes.PRODUCT_REVISION;
import static com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes.UNIT_REVISION;

/**
 * Class representing a ProductRevision.
 */
@EqualsAndHashCode(callSuper = true)
public class ProductRevision extends AbstractSnecmaObject {
    private static final long serialVersionUID = 9116577158989990064L;

    public ProductRevision(final TcSession session, final ItemRevision itemRevision) {
        super(session, itemRevision);
    }

    public String getDesignationEN() {
        return getMasterFormRev().getPropertyObject(PV_DESIGNATION_EN).getStringValue();
    }

    public String getTypeProduit() {
        session.loadProperties(getMasterFormRev(),PV_TYPE_PRODUIT);
        return getMasterFormRev().getPropertyObject(PV_TYPE_PRODUIT).getStringValue();
    }

    public String getProductLine() {
        return getMasterFormRev().getPropertyObject(PV_LIGNE_PRODUIT).getStringValue();
    }

    public String getDomain() {
        return getMasterFormRev().getPropertyObject(PV_DOMAINE).getStringValue();
    }

    public List<UnitRevision> getUnits() {
        List<UnitRevision> units = new ArrayList<>();
        session.loadProperties(itemRevision, PV_UNIT);
        ModelObject[] unitsArray = itemRevision.getPropertyObject(PV_UNIT).getModelObjectArrayValue();
        session.loadProperties(unitsArray);
        for (ModelObject unit : unitsArray) {
            ItemRevision currentItemRev = (ItemRevision) unit;
            if (UNIT_REVISION.equals(currentItemRev.getTypeObject().getName())) {
                units.add(UnitRevision.build(session, currentItemRev));
            }
        }
        return units;
    }

    @Override
    public boolean isProduct() {
        return true;
    }

    @Override
    public <T> T accept(final IReceptorVisitor<T> aReceptorVisitor) {
        return aReceptorVisitor.visitReceptor(this);
    }

    @Override
    public <T> T accept(final IComponentVisitor<T> aComponentVisitor) {
        return aComponentVisitor.visitComponent(this);
    }

    @Override
    public boolean isSuperModel() {
        return false;
    }

    @Override
    public String toString() {
        return PRODUCT_REVISION + " " + this.getItemId() + "/" + this.getRevisionId();
    }
}
