package com.atlassian.bitbucket.webhook;

import javax.annotation.Nonnull;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public enum WebHookEventId {
    ALL("*"),

    PROJECT("project:*"),
    PROJECT_CREATED("project:created"),
    PROJECT_DELETED("project:deleted"),
    PROJECT_MODIFIED("project:modified"),

    REPOSITORY("repo:*"),
    REPOSITORY_CREATED("repo:created"),
    REPOSITORY_DELETED("repo:deleted"),
    REPOSITORY_MODIFIED("repo:modified"),
    REPOSITORY_REFS_CHANGED("repo:refs_changed");

    private final String id;

    WebHookEventId(@Nonnull String id) {
        this.id = requireNonNull(id, "id");
    }

    public boolean isSingular() {
        return !id.contains("*");
    }

    @Nonnull
    public static Optional<WebHookEventId> forId(String id) {
        for (WebHookEventId webHookEventId : values()) {
            if (webHookEventId.getId().equals(id)) {
                return Optional.of(webHookEventId);
            }
        }
        return Optional.empty();
    }

    @Nonnull
    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return id;
    }
}
