package com.capgemini.reports.snecmaobjects.generic;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.snecmaobjects.AbstractSnecmaObject;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import lombok.EqualsAndHashCode;

import java.util.regex.Pattern;

import static com.capgemini.framework.teamcenter.object.SnecmaObjectProperties.PV_DESIGNATION_EN;
import static com.capgemini.framework.teamcenter.object.SnecmaObjectProperties.PV_RESPONSABLE_CONCEPTION;


/**
 * Abstract Class representing a Generic.
 */
@EqualsAndHashCode(callSuper = true)
public abstract class AbstractGenericRevision extends AbstractSnecmaObject implements Comparable<AbstractGenericRevision> {
    private static final long serialVersionUID = -190379543546189856L;

    /**
     * This pattern identify a {@link GenericRevision} id.
     **/
    private static final Pattern REGEX_PATTERN_FOR_GENERIC_REVISION = Pattern.compile("^[A-Z]");

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

    /**
     * Return true if the id correspond to a {@link GenericRevision} id.<br/>
     */
    public static boolean isGenericRevision(final String genId) {
        return REGEX_PATTERN_FOR_GENERIC_REVISION.matcher(genId).find();
    }

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

    public String getResponsableConception() {
        return getMasterFormRev().getPropertyObject(PV_RESPONSABLE_CONCEPTION).getStringValue();
    }

    @Override
    public int compareTo(final AbstractGenericRevision other) {
        int compareID = this.getItemId().compareTo(other.getItemId());
        if (compareID != 0) {
            return compareID; // Different ID
        } else {
            return this.getRevisionId().compareTo(other.getRevisionId());
        }
    }

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