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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.lstree.GitLsTreeBuilder;

import javax.annotation.Nonnull;

public class DefaultGitLsTreeBuilder extends AbstractGitCommandBuilder<GitLsTreeBuilder> implements GitLsTreeBuilder {

    private boolean nullTerminated;
    private boolean recursive;
    private boolean sizes;
    private String tree;

    public DefaultGitLsTreeBuilder(GitScmCommandBuilder builder) {
        super(builder.command("ls-tree"));
    }

    @Nonnull
    @Override
    public GitLsTreeBuilder nullTerminated(boolean value) {
        nullTerminated = value;

        return self();
    }

    @Nonnull
    @Override
    public GitLsTreeBuilder recursive(boolean value) {
        recursive = value;

        return self();
    }

    @Nonnull
    @Override
    public GitLsTreeBuilder sizes(boolean value) {
        sizes = value;

        return self();
    }

    @Nonnull
    @Override
    public DefaultGitLsTreeBuilder tree(@Nonnull String value) {
        tree = value;

        return self();
    }

    @Nonnull
    @Override
    public DefaultGitLsTreeBuilder tree(@Nonnull String rev, @Nonnull String file) {
        return tree(rev + ":" + file);
    }

    @Override
    protected void applyArguments() {
        if (nullTerminated) {
            builder.argument("-z");
        }
        if (recursive) {
            builder.argument("-r");
        }
        if (sizes) {
            builder.argument("-l");
        }
        builder.argument(tree);
    }

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