package com.atlassian.bitbucket.mesh.git.submodule;

import com.atlassian.bitbucket.mesh.util.BuilderSupport;

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

/**
 * The configuration for a submodule, indicating the URL from which it should be cloned.
 */
public class SubmoduleConfig {

    private final boolean active;
    private final String branch;
    private final String path;
    private final String url;

    private SubmoduleConfig(Builder builder) {
        active = builder.active;
        branch = builder.branch;
        path = builder.path;
        url = builder.url;
    }

    /**
     * @return the branch in the <i>external</i> repository to update from, or {@code null} for the default branch
     */
    @Nullable
    public String getBranch() {
        return branch;
    }

    /**
     * @return the <i>full</i> path to the submodule's tree entry within the <i>containing</i> repository
     */
    @Nonnull
    public String getPath() {
        return path;
    }

    /**
     * @return the clone URL to fetch the <i>external</i> repository
     */
    @Nonnull
    public String getUrl() {
        return url;
    }

    /**
     * @return {@code true} if the submodule is active; otherwise, {@code false} if {@code git} commands should
     *         not consider the submodule by default
     */
    public boolean isActive() {
        return active;
    }

    public static class Builder extends BuilderSupport {

        private final String path;
        private final String url;

        private boolean active;
        private String branch;

        public Builder(@Nonnull String path, @Nonnull String url) {
            this.path = requireNonBlank(path, "path");
            this.url = requireNonBlank(url, "url");

            active = true;
        }

        @Nonnull
        public Builder active(boolean value) {
            active = value;

            return this;
        }

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

            return this;
        }

        @Nonnull
        public SubmoduleConfig build() {
            return new SubmoduleConfig(this);
        }
    }
}
