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

import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;

import javax.annotation.Nonnull;
import java.nio.file.Path;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

public class DefaultGitHttpBackendBuilder
        extends AbstractGitProcessBuilder<GitHttpBackendBuilder>
        implements GitHttpBackendBuilder {

    private Path directory;

    public DefaultGitHttpBackendBuilder(GitProcessBuilder builder, Path directory) {
        super(builder.command("http-backend"));

        //Default to the repository's directory, if one was set, so callers don't need to redundantly
        //pass it to directory(Path)
        this.directory = directory;
    }

    @Nonnull
    @Override
    public GitHttpBackendBuilder directory(@Nonnull Path value) {
        directory = requireNonNull(value, "directory");

        return self();
    }

    @Override
    protected void applyArguments() {
        checkState(directory != null, "git http-backend requires a path");

        builder.rawArgument(directory.toString());
    }

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