// 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.audioclassifier;

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

/** Represents the classification results generated by {@link AudioClassifier}. */
@AutoValue
public abstract class AudioClassifierResult implements TaskResult {

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

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

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

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

  @Override
  public abstract long timestampMs();
}
