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

import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

public class DefaultGitRebaseBuilder extends AbstractGitProcessBuilder<GitRebaseBuilder> implements GitRebaseBuilder {

    private String branch;
    private boolean force;
    private boolean keepEmpty;
    private String onto;
    private boolean quiet;
    private GitRebaseStat stat;
    private String upstream;
    private boolean verbose;
    private GitRebaseVerify verify;

    public DefaultGitRebaseBuilder(GitProcessBuilder builder) {
        super(builder.command("rebase"));

        stat = GitRebaseStat.DEFAULT;
        verify = GitRebaseVerify.DEFAULT;
    }

    @Nonnull
    @Override
    public GitRebaseBuilder branch(@Nullable String value) {
        branch = value;

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder force(boolean value) {
        force = value;

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder keepEmpty(boolean value) {
        keepEmpty = value;

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder onto(@Nullable String value) {
        onto = value;

        return self();
    }

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

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder stat(@Nonnull GitRebaseStat value) {
        stat = requireNonNull(value, "stat");

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder upstream(@Nonnull String value) {
        upstream = requireNonBlank(value, "upstream");

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder verbose(boolean value) {
        verbose = value;

        return self();
    }

    @Nonnull
    @Override
    public GitRebaseBuilder verify(@Nonnull GitRebaseVerify value) {
        verify = requireNonNull(value, "verify");

        return self();
    }

    @Override
    protected void applyArguments() {
        if (force) {
            builder.argument("--force-rebase");
        }
        if (keepEmpty) {
            builder.argument("--keep-empty");
        }
        if (quiet) {
            builder.argument("-q");
        }
        stat.getFlag().ifPresent(builder::argument);
        if (verbose) {
            builder.argument("-v");
        }
        verify.getFlag().ifPresent(builder::argument);

        if (StringUtils.isNotBlank(onto)) {
            builder.argument("--onto");
            builder.commitish(onto);
        }
        builder.commitish(upstream);
        if (StringUtils.isNotBlank(branch)) {
            builder.commitish(branch);
        }
    }

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