// Copyright 2023 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.text.languagedetector;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.tasks.components.containers.Category;
import com.google.mediapipe.tasks.components.containers.ClassificationResult;
import com.google.mediapipe.tasks.components.containers.Classifications;
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 prediction results generated by {@link LanguageDetector}. */
@AutoValue
public abstract class LanguageDetectorResult implements TaskResult {

  /**
   * Creates an {@link LanguageDetectorResult} instance.
   *
   * @param classificationResult the {@link ClassificationResult} object containing one set of
   *     results per classifier head.
   * @param timestampMs a timestamp for this result.
   */
  static LanguageDetectorResult create(
      ClassificationResult classificationResult, long timestampMs) {
    if (classificationResult.classifications().size() != 1) {
      throw new IllegalArgumentException(
          "Expected 1 classification head, got " + classificationResult.classifications().size());
    }
    Classifications classifications = classificationResult.classifications().get(0);
    List<LanguagePrediction> languagePredictions = new ArrayList<>();
    for (Category category : classifications.categories()) {
      languagePredictions.add(LanguagePrediction.create(category.categoryName(), category.score()));
    }

    return new AutoValue_LanguageDetectorResult(languagePredictions, timestampMs);
  }

  /**
   * Creates an {@link LanguageDetectorResult} 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 LanguageDetectorResult createFromProto(
      ClassificationsProto.ClassificationResult proto, long timestampMs) {
    return create(ClassificationResult.createFromProto(proto), timestampMs);
  }

  /** A list of predictions from the LanguageDetector. */
  // For backwards-compatibility reasons in OSS we want to avoid dependencies on libraries like
  // Guava, so we don't use ImmutableList.
  @SuppressWarnings("AutoValueImmutableFields")
  public abstract List<LanguagePrediction> languagesAndScores();

  @Override
  public abstract long timestampMs();
}
