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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class DefaultGitRevertBuilder extends AbstractGitProcessBuilder<GitRevertBuilder> implements GitRevertBuilder {

    private String commit;
    private Integer parentNumber;

    public DefaultGitRevertBuilder(GitProcessBuilder builder) {
        super(builder.command("revert"));
    }

    @Nonnull
    @Override
    public GitProcess<Void> build() {
        return buildWithLoggingHandler();
    }

    @Nonnull
    @Override
    public GitRevertBuilder commit(@Nonnull String value) {
        commit = requireNonNull(StringUtils.trimToNull(value), "commit");
        return self();
    }

    @Nonnull
    @Override
    public GitRevertBuilder parentNumber(int value) {
        parentNumber = value;
        return self();
    }

    @Override
    protected void applyArguments() {
        builder.argument("--no-edit");
        if (parentNumber != null && parentNumber > 0) {
            builder.argument("-m").argument(String.valueOf(parentNumber));
        }
        builder.commitish(commit);
    }

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