package com.atlassian.stash.internal.pull;

import com.atlassian.bitbucket.pull.PullRequestMergeConfig;
import com.atlassian.bitbucket.pull.PullRequestMergeConfigType;
import com.atlassian.bitbucket.pull.PullRequestMergeStrategy;
import com.atlassian.stash.internal.merge.MergeConfigType;
import com.google.common.collect.ImmutableSet;

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

import static java.util.Objects.requireNonNull;

/**
 * @since 4.9
 */
public class SimplePullRequestMergeConfig implements PullRequestMergeConfig {

    private final PullRequestMergeStrategy defaultStrategy;
    private final Set<PullRequestMergeStrategy> strategies;
    private final PullRequestMergeConfigType type;

    private SimplePullRequestMergeConfig(Builder builder) {
        defaultStrategy = builder.defaultStrategy;
        strategies = builder.strategies.build();
        type = builder.type;
    }

    @Nonnull
    @Override
    public PullRequestMergeStrategy getDefaultStrategy() {
        return defaultStrategy;
    }

    @Nonnull
    @Override
    public Set<PullRequestMergeStrategy> getStrategies() {
        return strategies;
    }

    @Nonnull
    @Override
    public PullRequestMergeConfigType getType() {
        return type;
    }

    public static class Builder {

        private final ImmutableSet.Builder<PullRequestMergeStrategy> strategies;

        private PullRequestMergeStrategy defaultStrategy;
        private PullRequestMergeConfigType type;

        public Builder() {
            strategies = ImmutableSet.builder();
        }

        @Nonnull
        public SimplePullRequestMergeConfig build() {
            return new SimplePullRequestMergeConfig(this);
        }

        @Nonnull
        public Builder defaultStrategy(@Nonnull PullRequestMergeStrategy value) {
            defaultStrategy = requireNonNull(value, "defaultStrategy");

            return this;
        }

        @Nonnull
        public Builder strategies(@Nonnull Iterable<PullRequestMergeStrategy> values) {
            strategies.addAll(requireNonNull(values, "strategies"));

            return this;
        }

        @Nonnull
        public Builder type(@Nonnull MergeConfigType value) {
            return type(PullRequestMergeConfigType.valueOf(requireNonNull(value, "type").name()));
        }

        @Nonnull
        public Builder type(@Nonnull PullRequestMergeConfigType value) {
            type = requireNonNull(value, "type");

            return this;
        }
    }
}
