package com.atlassian.bitbucket.mesh.git.builder.merge;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

public class DefaultGitMergeBuilder extends AbstractGitMergeBuilder<GitMergeBuilder> implements GitMergeBuilder {

    private int log;
    private String message;

    public DefaultGitMergeBuilder(GitProcessBuilder builder) {
        super(builder);
    }

    @Nonnull
    @Override
    public GitMergeBuilder log(boolean value) {
        return log(value ? Integer.MAX_VALUE : 0);
    }

    @Nonnull
    @Override
    public GitMergeBuilder log(int value) {
        log = value;

        return self();
    }

    @Nonnull
    @Override
    public GitMergeBuilder message(String value) {
        message = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        if (StringUtils.isBlank(message)) {
            builder.argument("--no-edit");
        } else {
            builder.argument("-m")
                    .argument(message);
        }
        if (ff.isFlagged()) {
            builder.argument(ff.getFlag());
        }
        if (log > 0) {
            StringBuilder option = new StringBuilder("--log");
            if (log < Integer.MAX_VALUE) {
                option.append("=").append(log);
            }
            builder.argument(option.toString());
        } else {
            builder.argument("--no-log");
        }
        super.applyArguments();
    }

    @Nonnull
    @Override
    protected GitMergeBuilder self() {
        return this;
    }
}
