package zeenea.connector.zdf.visualization;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
import zeenea.connector.zdf.model.ZdfProcess;
import zeenea.connector.zdf.model.ZdfVisualization;

/**
 * Immutable implementation of {@link VisualizationRoot}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableVisualizationRoot.builder()}.
 */
@Generated(from = "VisualizationRoot", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableVisualizationRoot implements VisualizationRoot {
  private final List<ZdfVisualization> visualizations;
  private final List<ZdfProcess> lineage;

  private ImmutableVisualizationRoot(ImmutableVisualizationRoot.Builder builder) {
    if (builder.visualizationsIsSet()) {
      initShim.visualizations(List.copyOf(builder.visualizations));
    }
    if (builder.lineageIsSet()) {
      initShim.lineage(List.copyOf(builder.lineage));
    }
    this.visualizations = initShim.visualizations();
    this.lineage = initShim.lineage();
    this.initShim = null;
  }

  private ImmutableVisualizationRoot(
      List<ZdfVisualization> visualizations,
      List<ZdfProcess> lineage) {
    this.visualizations = visualizations;
    this.lineage = lineage;
    this.initShim = null;
  }

  private static final byte STAGE_INITIALIZING = -1;
  private static final byte STAGE_UNINITIALIZED = 0;
  private static final byte STAGE_INITIALIZED = 1;
  private transient volatile InitShim initShim = new InitShim();

  @Generated(from = "VisualizationRoot", generator = "Immutables")
  private final class InitShim {
    private byte visualizationsBuildStage = STAGE_UNINITIALIZED;
    private List<ZdfVisualization> visualizations;

    List<ZdfVisualization> visualizations() {
      if (visualizationsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
      if (visualizationsBuildStage == STAGE_UNINITIALIZED) {
        visualizationsBuildStage = STAGE_INITIALIZING;
        this.visualizations = visualizationsInitialize() instanceof Collection<?>
       ? List.copyOf((Collection<? extends ZdfVisualization>) visualizationsInitialize())
       : StreamSupport.stream(visualizationsInitialize().spliterator(), false)
             .collect(Collectors.toUnmodifiableList());
        visualizationsBuildStage = STAGE_INITIALIZED;
      }
      return this.visualizations;
    }

    void visualizations(List<ZdfVisualization> visualizations) {
      this.visualizations = visualizations;
      visualizationsBuildStage = STAGE_INITIALIZED;
    }

    private byte lineageBuildStage = STAGE_UNINITIALIZED;
    private List<ZdfProcess> lineage;

    List<ZdfProcess> lineage() {
      if (lineageBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
      if (lineageBuildStage == STAGE_UNINITIALIZED) {
        lineageBuildStage = STAGE_INITIALIZING;
        this.lineage = lineageInitialize() instanceof Collection<?>
       ? List.copyOf((Collection<? extends ZdfProcess>) lineageInitialize())
       : StreamSupport.stream(lineageInitialize().spliterator(), false)
             .collect(Collectors.toUnmodifiableList());
        lineageBuildStage = STAGE_INITIALIZED;
      }
      return this.lineage;
    }

    void lineage(List<ZdfProcess> lineage) {
      this.lineage = lineage;
      lineageBuildStage = STAGE_INITIALIZED;
    }

    private String formatInitCycleMessage() {
      List<String> attributes = new ArrayList<>();
      if (visualizationsBuildStage == STAGE_INITIALIZING) attributes.add("visualizations");
      if (lineageBuildStage == STAGE_INITIALIZING) attributes.add("lineage");
      return "Cannot build VisualizationRoot, attribute initializers form cycle " + attributes;
    }
  }

  private List<ZdfVisualization> visualizationsInitialize() {
    return VisualizationRoot.super.visualizations();
  }

  private List<ZdfProcess> lineageInitialize() {
    return VisualizationRoot.super.lineage();
  }

  /**
   * @return The value of the {@code visualizations} attribute
   */
  @JsonProperty("visualizations")
  @Override
  public List<ZdfVisualization> visualizations() {
    InitShim shim = this.initShim;
    return shim != null
        ? shim.visualizations()
        : this.visualizations;
  }

  /**
   * @return The value of the {@code lineage} attribute
   */
  @JsonProperty("lineage")
  @Override
  public List<ZdfProcess> lineage() {
    InitShim shim = this.initShim;
    return shim != null
        ? shim.lineage()
        : this.lineage;
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link VisualizationRoot#visualizations() visualizations}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableVisualizationRoot withVisualizations(ZdfVisualization... elements) {
    List<ZdfVisualization> newValue = List.of(elements);
    return new ImmutableVisualizationRoot(newValue, this.lineage);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link VisualizationRoot#visualizations() visualizations}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param elements An iterable of visualizations elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableVisualizationRoot withVisualizations(Iterable<? extends ZdfVisualization> elements) {
    if (this.visualizations == elements) return this;
    List<ZdfVisualization> newValue = elements instanceof Collection<?>
        ? List.copyOf((Collection<? extends ZdfVisualization>) elements)
        : StreamSupport.stream(elements.spliterator(), false)
            .collect(Collectors.toUnmodifiableList());
    return new ImmutableVisualizationRoot(newValue, this.lineage);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link VisualizationRoot#lineage() lineage}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableVisualizationRoot withLineage(ZdfProcess... elements) {
    List<ZdfProcess> newValue = List.of(elements);
    return new ImmutableVisualizationRoot(this.visualizations, newValue);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link VisualizationRoot#lineage() lineage}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param elements An iterable of lineage elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableVisualizationRoot withLineage(Iterable<? extends ZdfProcess> elements) {
    if (this.lineage == elements) return this;
    List<ZdfProcess> newValue = elements instanceof Collection<?>
        ? List.copyOf((Collection<? extends ZdfProcess>) elements)
        : StreamSupport.stream(elements.spliterator(), false)
            .collect(Collectors.toUnmodifiableList());
    return new ImmutableVisualizationRoot(this.visualizations, newValue);
  }

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

  private boolean equalTo(int synthetic, ImmutableVisualizationRoot another) {
    return visualizations.equals(another.visualizations)
        && lineage.equals(another.lineage);
  }

  /**
   * Computes a hash code from attributes: {@code visualizations}, {@code lineage}.
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 5381;
    h += (h << 5) + visualizations.hashCode();
    h += (h << 5) + lineage.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code VisualizationRoot} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "VisualizationRoot{"
        + "visualizations=" + visualizations
        + ", lineage=" + lineage
        + "}";
  }

  /**
   * Utility type used to correctly read immutable object from JSON representation.
   * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
   */
  @Generated(from = "VisualizationRoot", generator = "Immutables")
  @Deprecated
  @JsonDeserialize
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
  static final class Json implements VisualizationRoot {
    @Nullable List<ZdfVisualization> visualizations = List.of();
    boolean visualizationsIsSet;
    @Nullable List<ZdfProcess> lineage = List.of();
    boolean lineageIsSet;
    @JsonProperty("visualizations")
    public void setVisualizations(List<ZdfVisualization> visualizations) {
      this.visualizations = visualizations;
      this.visualizationsIsSet = null != visualizations;
    }
    @JsonProperty("lineage")
    public void setLineage(List<ZdfProcess> lineage) {
      this.lineage = lineage;
      this.lineageIsSet = null != lineage;
    }
    @Override
    public List<ZdfVisualization> visualizations() { throw new UnsupportedOperationException(); }
    @Override
    public List<ZdfProcess> lineage() { throw new UnsupportedOperationException(); }
  }

  /**
   * @param json A JSON-bindable data structure
   * @return An immutable value type
   * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
   */
  @Deprecated
  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
  static ImmutableVisualizationRoot fromJson(Json json) {
    ImmutableVisualizationRoot.Builder builder = ImmutableVisualizationRoot.builder();
    if (json.visualizationsIsSet) {
      builder.addAllVisualizations(json.visualizations);
    }
    if (json.lineageIsSet) {
      builder.addAllLineage(json.lineage);
    }
    return builder.build();
  }

  /**
   * Creates an immutable copy of a {@link VisualizationRoot} 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 VisualizationRoot instance
   */
  public static ImmutableVisualizationRoot copyOf(VisualizationRoot instance) {
    if (instance instanceof ImmutableVisualizationRoot) {
      return (ImmutableVisualizationRoot) instance;
    }
    return ImmutableVisualizationRoot.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableVisualizationRoot ImmutableVisualizationRoot}.
   * <pre>
   * ImmutableVisualizationRoot.builder()
   *    .addVisualizations|addAllVisualizations(zeenea.connector.zdf.model.ZdfVisualization) // {@link VisualizationRoot#visualizations() visualizations} elements
   *    .addLineage|addAllLineage(zeenea.connector.zdf.model.ZdfProcess) // {@link VisualizationRoot#lineage() lineage} elements
   *    .build();
   * </pre>
   * @return A new ImmutableVisualizationRoot builder
   */
  public static ImmutableVisualizationRoot.Builder builder() {
    return new ImmutableVisualizationRoot.Builder();
  }

  /**
   * Builds instances of type {@link ImmutableVisualizationRoot ImmutableVisualizationRoot}.
   * 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 = "VisualizationRoot", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder {
    private static final long OPT_BIT_VISUALIZATIONS = 0x1L;
    private static final long OPT_BIT_LINEAGE = 0x2L;
    private long optBits;

    private List<ZdfVisualization> visualizations = new ArrayList<ZdfVisualization>();
    private List<ZdfProcess> lineage = new ArrayList<ZdfProcess>();

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code VisualizationRoot} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * Collection elements and entries will be added, not replaced.
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(VisualizationRoot instance) {
      Objects.requireNonNull(instance, "instance");
      addAllVisualizations(instance.visualizations());
      addAllLineage(instance.lineage());
      return this;
    }

    /**
     * Adds one element to {@link VisualizationRoot#visualizations() visualizations} list.
     * @param element A visualizations element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addVisualizations(ZdfVisualization element) {
      this.visualizations.add(Objects.requireNonNull(element, "visualizations element"));
      optBits |= OPT_BIT_VISUALIZATIONS;
      return this;
    }

    /**
     * Adds elements to {@link VisualizationRoot#visualizations() visualizations} list.
     * @param elements An array of visualizations elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addVisualizations(ZdfVisualization... elements) {
      for (ZdfVisualization element : elements) {
        this.visualizations.add(Objects.requireNonNull(element, "visualizations element"));
      }
      optBits |= OPT_BIT_VISUALIZATIONS;
      return this;
    }


    /**
     * Sets or replaces all elements for {@link VisualizationRoot#visualizations() visualizations} list.
     * @param elements An iterable of visualizations elements
     * @return {@code this} builder for use in a chained invocation
     */
    @JsonProperty("visualizations")
    public final Builder visualizations(Iterable<? extends ZdfVisualization> elements) {
      this.visualizations.clear();
      return addAllVisualizations(elements);
    }

    /**
     * Adds elements to {@link VisualizationRoot#visualizations() visualizations} list.
     * @param elements An iterable of visualizations elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllVisualizations(Iterable<? extends ZdfVisualization> elements) {
      for (ZdfVisualization element : elements) {
        this.visualizations.add(Objects.requireNonNull(element, "visualizations element"));
      }
      optBits |= OPT_BIT_VISUALIZATIONS;
      return this;
    }

    /**
     * Adds one element to {@link VisualizationRoot#lineage() lineage} list.
     * @param element A lineage element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addLineage(ZdfProcess element) {
      this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }

    /**
     * Adds elements to {@link VisualizationRoot#lineage() lineage} list.
     * @param elements An array of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addLineage(ZdfProcess... elements) {
      for (ZdfProcess element : elements) {
        this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      }
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }


    /**
     * Sets or replaces all elements for {@link VisualizationRoot#lineage() lineage} list.
     * @param elements An iterable of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    @JsonProperty("lineage")
    public final Builder lineage(Iterable<? extends ZdfProcess> elements) {
      this.lineage.clear();
      return addAllLineage(elements);
    }

    /**
     * Adds elements to {@link VisualizationRoot#lineage() lineage} list.
     * @param elements An iterable of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllLineage(Iterable<? extends ZdfProcess> elements) {
      for (ZdfProcess element : elements) {
        this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      }
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }

    /**
     * Builds a new {@link ImmutableVisualizationRoot ImmutableVisualizationRoot}.
     * @return An immutable instance of VisualizationRoot
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableVisualizationRoot build() {
      return new ImmutableVisualizationRoot(this);
    }

    private boolean visualizationsIsSet() {
      return (optBits & OPT_BIT_VISUALIZATIONS) != 0;
    }

    private boolean lineageIsSet() {
      return (optBits & OPT_BIT_LINEAGE) != 0;
    }
  }
}
