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

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

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

public class DefaultGitIndexPackBuilder
        extends AbstractGitProcessBuilder<GitIndexPackBuilder> implements GitIndexPackBuilder {

    private boolean fixThin;
    private Path indexFile;
    private boolean keep;
    private Path packFile;
    private boolean stdin;
    private boolean strict;

    public DefaultGitIndexPackBuilder(GitProcessBuilder builder) {
        super(builder.command("index-pack"));
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder fixThin(boolean value) {
        fixThin = value;
        return self();
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder indexFile(@Nullable Path value) {
        indexFile = value;
        return self();
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder keep(boolean value) {
        keep = value;
        return self();
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder packFile(@Nullable Path value) {
        packFile = value;
        return self();
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder stdin(boolean value) {
        stdin = value;
        return self();
    }

    @Nonnull
    @Override
    public GitIndexPackBuilder strict(boolean value) {
        strict = value;
        return self();
    }

    @Override
    protected void applyArguments() {
        if (fixThin) {
            builder.argument("--fix-thin");
        }
        if (keep) {
            builder.argument("--keep");
        }
        if (stdin) {
            builder.argument("--stdin");
        }
        if (strict) {
            builder.argument("--strict");
        }
        if (indexFile != null) {
            builder.argument("-o").argument(indexFile.toAbsolutePath().toString());
        }
        if (packFile != null) {
            builder.argument(packFile.toAbsolutePath().toString());
        }
    }

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