package com.atlassian.stash.internal.pull;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.pull.PullRequestMergeStrategy;
import com.atlassian.bitbucket.scm.PluginMergeStrategy;

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

/**
 * @since 4.9
 */
public class ScmPullRequestMergeStrategy implements PullRequestMergeStrategy {

    private final boolean enabled;
    private final I18nService i18nService;
    private final PluginMergeStrategy strategy;

    private ScmPullRequestMergeStrategy(Builder builder) {
        enabled = builder.enabled;
        i18nService = builder.i18nService;
        strategy = builder.strategy;
    }

    @Nonnull
    @Override
    public String getDescription() {
        return i18nService.getMessage(strategy.getDescriptionKey());
    }

    @Nonnull
    @Override
    public Optional<String> getFlag() {
        return strategy.getFlag();
    }

    @Nonnull
    @Override
    public String getId() {
        return strategy.getId();
    }

    @Nonnull
    @Override
    public String getName() {
        return i18nService.getMessage(strategy.getNameKey());
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public static class Builder {

        private final I18nService i18nService;
        private final PluginMergeStrategy strategy;

        private boolean enabled;

        public Builder(I18nService i18nService, PluginMergeStrategy strategy) {
            this.i18nService = i18nService;
            this.strategy = strategy;
        }

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

        @Nonnull
        public Builder enabled(boolean value) {
            enabled = value;

            return this;
        }
    }
}
