// Copyright 2022 The MediaPipe Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.audio.audioembedder;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.tasks.components.containers.EmbeddingResult;
import com.google.mediapipe.tasks.components.containers.proto.EmbeddingsProto;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/** Represents the embedding results generated by {@link AudioEmbedder}. */
@AutoValue
public abstract class AudioEmbedderResult implements TaskResult {

  /**
   * Creates an {@link AudioEmbedderResult} instance from a list of {@link
   * EmbeddingsProto.EmbeddingResult} protobuf messages.
   *
   * @param protoList a list of {@link EmbeddingsProto.EmbeddingResult} protobuf message to convert.
   * @param timestampMs a timestamp for this result.
   */
  static AudioEmbedderResult createFromProtoList(
      List<EmbeddingsProto.EmbeddingResult> protoList, long timestampMs) {
    List<EmbeddingResult> classificationResultList = new ArrayList<>();
    for (EmbeddingsProto.EmbeddingResult proto : protoList) {
      classificationResultList.add(EmbeddingResult.createFromProto(proto));
    }
    return new AutoValue_AudioEmbedderResult(
        Optional.of(classificationResultList), Optional.empty(), timestampMs);
  }

  /**
   * Creates an {@link AudioEmbedderResult} instance from a {@link EmbeddingsProto.EmbeddingResult}
   * protobuf message.
   *
   * @param proto the {@link EmbeddingsProto.EmbeddingResult} protobuf message to convert.
   * @param timestampMs a timestamp for this result.
   */
  static AudioEmbedderResult createFromProto(
      EmbeddingsProto.EmbeddingResult proto, long timestampMs) {
    return new AutoValue_AudioEmbedderResult(
        Optional.empty(), Optional.of(EmbeddingResult.createFromProto(proto)), timestampMs);
  }

  /**
   * A list of of timpstamped {@link EmbeddingResult} objects, each contains one set of results per
   * embedder head. The list represents the audio embedding result of an audio clip, and is only
   * available when running with the audio clips mode.
   */
  public abstract Optional<List<EmbeddingResult>> embeddingResultList();

  /**
   * Contains one set of results per classifier head. A {@link EmbeddingResult} usually represents
   * one audio embedding result in an audio stream, and is only available when running with the
   * audio stream mode.
   */
  public abstract Optional<EmbeddingResult> embeddingResult();

  @Override
  public abstract long timestampMs();
}
