package be.iminds.ilabt.jfed.highlevel.model;

/**
 * The current internal status of a slice or sliver in jFed.
 * This status only has meaning to jFed itself, and tells us in which phase of it's lifecycle the slice is.
 * <p>
 * It contains additional states compared to the AM API v3 "Allocation States" that signify that jFed is trying
 * to transition from one Allocation State to another.
 */
public enum InternalState {


    /**
     * Initial state.
     * <p>
     * We don't know anything about the object, besides that it must exist somehow.
     */
    UNKNOWN,

    /**
     * See <a href="http://groups.geni.net/geni/wiki/GAPI_AM_API_V3/CommonConcepts#SliverAllocationStates">geni_unallocated</a>.
     * <p>
     * This state is reached when a resource expires.
     * <p>
     * However we don't return to this state when the element has been deleted by the user in jFed.
     * It then transitions to {@link #DELETED} instead.
     */
    UNALLOCATED,

    RESERVING,

    /**
     * A reserved resource is a resource that has been allocated by means of a reservation.
     * This typically means that the reservation is in the future, but this is however not necessary
     * (e.g. the reservation starts directly).
     * <p>
     * The distinction between {@link #RESERVED} and {@link #ALLOCATED} is made for clarity sake. When a resource is
     * reserved, whe should always check if the reservation has already started!
     */
    RESERVED,

    /**
     * jFed is trying to transition this element to the the {@link #ALLOCATED} status
     */
    ALLOCATING,

    /**
     * See <a href="http://groups.geni.net/geni/wiki/GAPI_AM_API_V3/CommonConcepts#SliverAllocationStates">geni_allocated</a>
     */
    ALLOCATED,

    /**
     * jFed is trying to transition this element to the {@link #PROVISIONED} status
     */
    PROVISIONING,

    /**
     * See <a href="http://groups.geni.net/geni/wiki/GAPI_AM_API_V3/CommonConcepts#SliverAllocationStates">geni_provisioned</a>
     */
    PROVISIONED,

    WAIT_FOR_READY,

    READY,

    /**
     * jFed is trying to delete this resource.
     */
    DELETING,

    /**
     * Final state
     * <p>
     * The resource has been deleted
     */
    DELETED,

    /**
     * An action that jFed executed failed permanently.
     */
    FAILED,

    /**
     * An external change has happened, and jFed should refresh the information periodically to see how the state evolves
     */
    //TODO Implement logic that will automatically start GetStatusUntilReadyOrFail to see how information changes
    CHANGING;


    public static boolean isActiveState(InternalState state) {
        return state != UNALLOCATED && state != DELETED && state != FAILED;
    }
}