package com.atlassian.bitbucket.internal.branch.model;

import com.atlassian.bitbucket.branch.model.BranchType;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;

import javax.annotation.Nonnull;

import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

/**
 * Enumeration of built-in branch types.
 *
 * @since 2.8
 */
public enum BranchTypes {

    // If adding another type please add an entry to the i18n properties file
    BUGFIX,
    FEATURE,
    HOTFIX,
    RELEASE;

    /**
     * Default ordering of branch types, as defined by the enumeration of built-in types.
     * <p>
     * NOTE: this will fail for branch types, whose ID is not one of the known. built-in branch type IDs.
     *
     * @see com.atlassian.bitbucket.internal.branch.model.BranchTypes
     */
    public static final Ordering<BranchType> DEFAULT_ORDER = Ordering.<BranchTypes>natural().onResultOf(
            new Function<BranchType, BranchTypes>() {

                @Override
                public BranchTypes apply(BranchType type) {
                    return BranchTypes.forId(type.getId());
                }
            });

    public static BranchTypes forId(@Nonnull String id) {
        return BranchTypes.valueOf(requireNonNull(id, "id").toUpperCase());
    }

    /**
     * @return unique branch type ID
     */
    public String getId() {
        return name();
    }

    public Predicate<BranchType> matcher() {
        return branchType -> branchType != null && getId().equals(branchType.getId().toUpperCase());
    }
}
