package com.atlassian.bitbucket.mirroring.mirror;

import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import java.util.Optional;

/**
 * Represents the type of the upstream.
 *
 * @since 4.6
 */
public enum UpstreamServerType {
    /**
     * The upstream is Bitbucket server.
     *
     * The {@code BootstrapOperation#VALUE_SERVER_MIRROR} depends on this value.
     */
    BITBUCKET_SERVER("server"),

    /**
     * The upstream is Bitbucket cloud.
     *
     * The {@code BootstrapOperation#VALUE_CLOUD_MIRROR} depends on this value.
     */
    BITBUCKET_CLOUD("cloud");

    private final String id;

    UpstreamServerType(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    @Nonnull
    public static Optional<UpstreamServerType> fromId(String string) {
        string = StringUtils.stripToNull(string);
        if (string == null) {
            return Optional.empty();
        }

        for (UpstreamServerType type : values()) {
            if (type.getId().equalsIgnoreCase(string)) {
                return Optional.of(type);
            }
        }
        return Optional.empty();
    }
}
