package com.capgemini.framework.teamcenter.object.pom.status;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.teamcenter.soa.client.model.strong.Effectivity;
import com.teamcenter.soa.client.model.strong.Item;

import java.io.Serializable;

/**
 * Represents a Unit effectivity.
 */
public final class TcEffectivity implements Serializable {
    private static final long serialVersionUID = -181276898335854241L;
    private final Effectivity effectivity;
    private final TcSession session;

    public TcEffectivity(TcSession session, final Effectivity effectivity) {
        this.session = session;
        this.effectivity = effectivity;
    }

    public String endItemId() {
        session.loadProperties(effectivity, "end_item");
        Item endItem = effectivity.get_end_item();
        if (endItem != null) {
            session.loadProperties(endItem, "item_id");
            return endItem.get_item_id();
        } else {
            return "";
        }
    }

    public Item endItem() {
        session.loadProperties(effectivity, "end_item");
        return effectivity.get_end_item();
    }

    public int[] unitNumbers() {
        session.loadProperties(effectivity, "effectivity_units");
        return effectivity.get_effectivity_units();
    }

    public String unitRange() {
        session.loadProperties(effectivity, "unit_range_text");
        return effectivity.get_unit_range_text();
    }

    public String dateRange() {
        session.loadProperties(effectivity, "date_range_text");
        return effectivity.get_date_range_text();
    }

    public int unitOpenEndedStatus() {
        session.loadProperties(effectivity, "unit_open_ended_status");
        return effectivity.get_unit_open_ended_status();
    }

    @Override
    public String toString() {
        return "TcEffectivity " + endItemId() + "[" + ((unitRange() == null || unitRange().isEmpty()) ? dateRange() : unitRange()) + "]";
    }
}
