package com.atlassian.stash.internal.merge;

import com.atlassian.bitbucket.scm.PluginMergeStrategy;
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 SimpleMergeConfig implements MergeConfig {

    private final Set<PluginMergeStrategy> availableStrategies;
    private final String defaultStrategyId;
    private final Set<String> enabledStrategyIds;
    private final MergeConfigType type;

    public SimpleMergeConfig(@Nonnull Set<PluginMergeStrategy> availableStrategies, @Nonnull String defaultStrategyId,
                             @Nonnull Set<String> enabledStrategyIds, @Nonnull MergeConfigType type) {
        this.availableStrategies = ImmutableSet.copyOf(requireNonNull(availableStrategies, "availableStrategies"));
        this.defaultStrategyId = requireNonNull(defaultStrategyId, "defaultStrategyId");
        this.enabledStrategyIds = ImmutableSet.copyOf(requireNonNull(enabledStrategyIds, "enabledStrategyIds"));
        this.type = requireNonNull(type, "type");
    }

    @Nonnull
    @Override
    public Set<PluginMergeStrategy> getAvailableStrategies() {
        return availableStrategies;
    }

    @Nonnull
    @Override
    public String getDefaultStrategyId() {
        return defaultStrategyId;
    }

    @Nonnull
    @Override
    public Set<String> getEnabledStrategyIds() {
        return enabledStrategyIds;
    }

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

    @Override
    public boolean isAvailable(@Nonnull String strategyId) {
        requireNonNull(strategyId, "strategyId");

        return availableStrategies.stream()
                .map(PluginMergeStrategy::getId)
                .anyMatch(strategyId::equals);
    }

    @Override
    public boolean isEnabled(@Nonnull String strategyId) {
        return enabledStrategyIds.contains(requireNonNull(strategyId, "strategyId"));
    }
}
