package zeenea.connector.commons.git;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
import org.jetbrains.annotations.Nullable;

/**
 * Immutable implementation of {@link GitConfig}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableGitConfig.builder()}.
 */
@Generated(from = "GitConfig", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableGitConfig implements GitConfig {
  private final URI repositoryUrl;
  private final String branch;
  private final @Nullable String token;
  private final @Nullable String username;
  private final @Nullable String password;
  private final Path workDir;
  private final boolean clean;

  private ImmutableGitConfig(
      URI repositoryUrl,
      String branch,
      @Nullable String token,
      @Nullable String username,
      @Nullable String password,
      Path workDir,
      boolean clean) {
    this.repositoryUrl = repositoryUrl;
    this.branch = branch;
    this.token = token;
    this.username = username;
    this.password = password;
    this.workDir = workDir;
    this.clean = clean;
  }

  /**
   * @return The value of the {@code repositoryUrl} attribute
   */
  @Override
  public URI repositoryUrl() {
    return repositoryUrl;
  }

  /**
   * @return The value of the {@code branch} attribute
   */
  @Override
  public String branch() {
    return branch;
  }

  /**
   * @return The value of the {@code token} attribute
   */
  @Override
  public @Nullable String token() {
    return token;
  }

  /**
   * @return The value of the {@code username} attribute
   */
  @Override
  public @Nullable String username() {
    return username;
  }

  /**
   * @return The value of the {@code password} attribute
   */
  @Override
  public @Nullable String password() {
    return password;
  }

  /**
   * @return The value of the {@code workDir} attribute
   */
  @Override
  public Path workDir() {
    return workDir;
  }

  /**
   * @return The value of the {@code clean} attribute
   */
  @Override
  public boolean clean() {
    return clean;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#repositoryUrl() repositoryUrl} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for repositoryUrl
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withRepositoryUrl(URI value) {
    if (this.repositoryUrl == value) return this;
    URI newValue = Objects.requireNonNull(value, "repositoryUrl");
    return new ImmutableGitConfig(newValue, this.branch, this.token, this.username, this.password, this.workDir, this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#branch() branch} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for branch
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withBranch(String value) {
    String newValue = Objects.requireNonNull(value, "branch");
    if (this.branch.equals(newValue)) return this;
    return new ImmutableGitConfig(
        this.repositoryUrl,
        newValue,
        this.token,
        this.username,
        this.password,
        this.workDir,
        this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#token() token} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for token (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withToken(@Nullable String value) {
    if (Objects.equals(this.token, value)) return this;
    return new ImmutableGitConfig(this.repositoryUrl, this.branch, value, this.username, this.password, this.workDir, this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#username() username} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for username (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withUsername(@Nullable String value) {
    if (Objects.equals(this.username, value)) return this;
    return new ImmutableGitConfig(this.repositoryUrl, this.branch, this.token, value, this.password, this.workDir, this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#password() password} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for password (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withPassword(@Nullable String value) {
    if (Objects.equals(this.password, value)) return this;
    return new ImmutableGitConfig(this.repositoryUrl, this.branch, this.token, this.username, value, this.workDir, this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#workDir() workDir} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for workDir
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withWorkDir(Path value) {
    if (this.workDir == value) return this;
    Path newValue = Objects.requireNonNull(value, "workDir");
    return new ImmutableGitConfig(this.repositoryUrl, this.branch, this.token, this.username, this.password, newValue, this.clean);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link GitConfig#clean() clean} attribute.
   * A value equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for clean
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableGitConfig withClean(boolean value) {
    if (this.clean == value) return this;
    return new ImmutableGitConfig(this.repositoryUrl, this.branch, this.token, this.username, this.password, this.workDir, value);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableGitConfig} that have equal attribute values.
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(@javax.annotation.Nullable Object another) {
    if (this == another) return true;
    return another instanceof ImmutableGitConfig
        && equalTo(0, (ImmutableGitConfig) another);
  }

  private boolean equalTo(int synthetic, ImmutableGitConfig another) {
    return repositoryUrl.equals(another.repositoryUrl)
        && branch.equals(another.branch)
        && Objects.equals(token, another.token)
        && Objects.equals(username, another.username)
        && Objects.equals(password, another.password)
        && workDir.equals(another.workDir)
        && clean == another.clean;
  }

  /**
   * Computes a hash code from attributes: {@code repositoryUrl}, {@code branch}, {@code token}, {@code username}, {@code password}, {@code workDir}, {@code clean}.
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 5381;
    h += (h << 5) + repositoryUrl.hashCode();
    h += (h << 5) + branch.hashCode();
    h += (h << 5) + Objects.hashCode(token);
    h += (h << 5) + Objects.hashCode(username);
    h += (h << 5) + Objects.hashCode(password);
    h += (h << 5) + workDir.hashCode();
    h += (h << 5) + Boolean.hashCode(clean);
    return h;
  }

  /**
   * Prints the immutable value {@code GitConfig} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "GitConfig{"
        + "repositoryUrl=" + repositoryUrl
        + ", branch=" + branch
        + ", token=" + token
        + ", username=" + username
        + ", password=" + password
        + ", workDir=" + workDir
        + ", clean=" + clean
        + "}";
  }

  /**
   * Creates an immutable copy of a {@link GitConfig} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   * @param instance The instance to copy
   * @return A copied immutable GitConfig instance
   */
  public static ImmutableGitConfig copyOf(GitConfig instance) {
    if (instance instanceof ImmutableGitConfig) {
      return (ImmutableGitConfig) instance;
    }
    return ImmutableGitConfig.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableGitConfig ImmutableGitConfig}.
   * <pre>
   * ImmutableGitConfig.builder()
   *    .repositoryUrl(java.net.URI) // required {@link GitConfig#repositoryUrl() repositoryUrl}
   *    .branch(String) // required {@link GitConfig#branch() branch}
   *    .token(String | null) // nullable {@link GitConfig#token() token}
   *    .username(String | null) // nullable {@link GitConfig#username() username}
   *    .password(String | null) // nullable {@link GitConfig#password() password}
   *    .workDir(java.nio.file.Path) // required {@link GitConfig#workDir() workDir}
   *    .clean(boolean) // required {@link GitConfig#clean() clean}
   *    .build();
   * </pre>
   * @return A new ImmutableGitConfig builder
   */
  public static ImmutableGitConfig.Builder builder() {
    return new ImmutableGitConfig.Builder();
  }

  /**
   * Builds instances of type {@link ImmutableGitConfig ImmutableGitConfig}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  @Generated(from = "GitConfig", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder {
    private static final long INIT_BIT_REPOSITORY_URL = 0x1L;
    private static final long INIT_BIT_BRANCH = 0x2L;
    private static final long INIT_BIT_WORK_DIR = 0x4L;
    private static final long INIT_BIT_CLEAN = 0x8L;
    private long initBits = 0xfL;

    private @javax.annotation.Nullable URI repositoryUrl;
    private @javax.annotation.Nullable String branch;
    private @javax.annotation.Nullable String token;
    private @javax.annotation.Nullable String username;
    private @javax.annotation.Nullable String password;
    private @javax.annotation.Nullable Path workDir;
    private boolean clean;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code GitConfig} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(GitConfig instance) {
      Objects.requireNonNull(instance, "instance");
      this.repositoryUrl(instance.repositoryUrl());
      this.branch(instance.branch());
      @Nullable String tokenValue = instance.token();
      if (tokenValue != null) {
        token(tokenValue);
      }
      @Nullable String usernameValue = instance.username();
      if (usernameValue != null) {
        username(usernameValue);
      }
      @Nullable String passwordValue = instance.password();
      if (passwordValue != null) {
        password(passwordValue);
      }
      this.workDir(instance.workDir());
      this.clean(instance.clean());
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#repositoryUrl() repositoryUrl} attribute.
     * @param repositoryUrl The value for repositoryUrl 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder repositoryUrl(URI repositoryUrl) {
      this.repositoryUrl = Objects.requireNonNull(repositoryUrl, "repositoryUrl");
      initBits &= ~INIT_BIT_REPOSITORY_URL;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#branch() branch} attribute.
     * @param branch The value for branch 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder branch(String branch) {
      this.branch = Objects.requireNonNull(branch, "branch");
      initBits &= ~INIT_BIT_BRANCH;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#token() token} attribute.
     * @param token The value for token (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder token(@Nullable String token) {
      this.token = token;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#username() username} attribute.
     * @param username The value for username (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder username(@Nullable String username) {
      this.username = username;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#password() password} attribute.
     * @param password The value for password (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder password(@Nullable String password) {
      this.password = password;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#workDir() workDir} attribute.
     * @param workDir The value for workDir 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder workDir(Path workDir) {
      this.workDir = Objects.requireNonNull(workDir, "workDir");
      initBits &= ~INIT_BIT_WORK_DIR;
      return this;
    }

    /**
     * Initializes the value for the {@link GitConfig#clean() clean} attribute.
     * @param clean The value for clean 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder clean(boolean clean) {
      this.clean = clean;
      initBits &= ~INIT_BIT_CLEAN;
      return this;
    }

    /**
     * Builds a new {@link ImmutableGitConfig ImmutableGitConfig}.
     * @return An immutable instance of GitConfig
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableGitConfig build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableGitConfig(repositoryUrl, branch, token, username, password, workDir, clean);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = new ArrayList<>();
      if ((initBits & INIT_BIT_REPOSITORY_URL) != 0) attributes.add("repositoryUrl");
      if ((initBits & INIT_BIT_BRANCH) != 0) attributes.add("branch");
      if ((initBits & INIT_BIT_WORK_DIR) != 0) attributes.add("workDir");
      if ((initBits & INIT_BIT_CLEAN) != 0) attributes.add("clean");
      return "Cannot build GitConfig, some of required attributes are not set " + attributes;
    }
  }
}
