// Copyright 2022 The MediaPipe Authors.
//
// 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;

/** 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(classificationResultList, 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) {
    List<ClassificationResult> classificationResultList = new ArrayList<>();
    classificationResultList.add(ClassificationResult.createFromProto(proto));
    return new AutoValue_AudioClassifierResult(classificationResultList, timestampMs);
  }

  /**
   * A list of of timestamped {@link ClassificationResult} objects, each contains one set of results
   * per classifier head.
   *
   * <p>In the "audio stream" mode, the list only contains one element, representing the
   * classification result of the audio block that starts at {@link
   * ClassificationResult.timestampMs} in the audio stream. Otherwise, in the "audio clips" mode,
   * the list may include multiple {@link ClassificationResult} objects, each classifying an
   * interval of the entire audio clip that starts at {@link ClassificationResult.timestampMs}.
   */
  public abstract List<ClassificationResult> classificationResults();

  @Override
  public abstract long timestampMs();
}
