package com.atlassian.stash.internal.scm.git.command.commit;

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitCommand;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.commit.GitCommitBuilder;

import javax.annotation.Nonnull;
import java.io.File;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @since 1.3
 */
public class DefaultGitCommitBuilder extends AbstractGitCommandBuilder<GitCommitBuilder> implements GitCommitBuilder {

    private File file;
    private String message;

    public DefaultGitCommitBuilder(GitScmCommandBuilder builder) {
        super(builder.command("commit"));
    }

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

    @Nonnull
    @Override
    public GitCommitBuilder message(@Nonnull File value) {
        file = checkNotNull(value, "message");

        return self();
    }

    @Nonnull
    @Override
    public GitCommitBuilder message(@Nonnull String value) {
        message = checkNotBlank(value, "message");

        return self();
    }

    @Override
    protected void applyArguments() {
        if (file == null) {
            builder.argument("-m")
                    .argument(message);
        } else {
            builder.argument("-F")
                    .argument(file.getAbsolutePath());
        }
    }

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