package com.atlassian.bitbucket.scm.git.protocol;

import com.atlassian.bitbucket.request.RequestContext;
import com.atlassian.bitbucket.scm.ScmRequest;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.scm.git.GitConstants.*;

/**
 * Represents the different {@link ScmRequest} operations that are are supported by the git
 * plugin.
*/
public enum GitScmRequestType {
    ARCHIVE(LABEL_ARCHIVE),
    CLONE(LABEL_CLONE),
    FETCH(LABEL_FETCH),
    NEGOTIATION(LABEL_SHALLOW, LABEL_NEGOTIATION),
    PUSH(LABEL_PUSH),
    REFS(LABEL_REFS),
    SHALLOW_CLONE(LABEL_SHALLOW, LABEL_CLONE),
    SHALLOW_FETCH(LABEL_SHALLOW, LABEL_FETCH),
    UNKNOWN();

    private final ImmutableList<String> labels;
    private final boolean clone;

    GitScmRequestType(String... labels) {
        this.labels = ImmutableList.copyOf(labels);
        this.clone = this.labels.contains(LABEL_CLONE);
    }

    public boolean isClone() {
        return clone;
    }

    @Override
    public String toString() {
        return StringUtils.join(labels, " ");
    }

    /**
     * @return the {@link RequestContext#addLabel(String) labels} associated with the
     *         request type.
     */
    @Nonnull
    public ImmutableList<String> getLabels() {
        return labels;
    }
}
