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

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 javax.annotation.Nullable;
import java.nio.file.Path;

import static java.util.Objects.requireNonNull;

public class DefaultGitInitBuilder extends AbstractGitProcessBuilder<GitInitBuilder> implements GitInitBuilder {

    private boolean bare;
    private String directory;
    private boolean quiet;
    private String shared;
    private String template;

    public DefaultGitInitBuilder(GitProcessBuilder builder) {
        super(builder.command("init"));
    }

    @Nonnull
    @Override
    public GitInitBuilder bare(boolean value) {
        bare = value;

        return self();
    }

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

    @Nonnull
    @Override
    public GitInitBuilder directory(@Nonnull Path value) {
        directory = requireNonNull(value, "directory").toAbsolutePath().toString();

        return self();
    }

    @Nonnull
    @Override
    public GitInitBuilder quiet(boolean value) {
        quiet = value;

        return self();
    }

    @Nonnull
    @Override
    public GitInitBuilder shared(String value) {
        shared = value;

        return self();
    }

    @Nonnull
    @Override
    public GitInitBuilder template(@Nullable Path value) {
        template = value == null ? null : value.toAbsolutePath().toString();

        return self();
    }

    @Override
    protected void applyArguments() {
        if (bare) {
            builder.argument("--bare");
        }
        if (quiet) {
            builder.argument("--quiet");
        }
        if (StringUtils.isNotBlank(shared)) {
            builder.argument("--shared=" + shared);
        }
        if (StringUtils.isNotBlank(template)) {
            builder.argument("--template=" + template);
        }
        builder.argument(directory);
    }

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