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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.catfile.GitCatFileBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

/**
 * @since 1.1
 */
public class DefaultGitCatFileBuilder extends AbstractGitCommandBuilder<GitCatFileBuilder> implements GitCatFileBuilder {

    private final CatFileFormat format;

    private String object;

    public DefaultGitCatFileBuilder(GitScmCommandBuilder builder, CatFileFormat format) {
        super(builder);

        this.format = format;
    }

    @Nonnull
    @Override
    public DefaultGitCatFileBuilder object(@Nonnull String value) {
        object = checkNotBlank(value, "object");

        return this;
    }

    @Nonnull
    @Override
    public DefaultGitCatFileBuilder object(@Nonnull String rev, String file) {
        return object(checkNotBlank(rev, "rev") + ":" + StringUtils.trimToEmpty(file));
    }

    @Override
    protected void applyArguments() {
        builder.argument(format.getFlag());
        builder.argument(object);
    }

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