/*
 * Copyright 2021 Google LLC
 *
 * 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
 *
 *      https://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.android.apps.exposurenotification.privateanalytics.metrics;

import androidx.annotation.VisibleForTesting;
import com.google.android.apps.exposurenotification.storage.DiagnosisEntity.TestResult;
import com.google.android.apps.exposurenotification.storage.ExposureNotificationSharedPreferences;
import com.google.android.libraries.privateanalytics.PrivateAnalyticsMetric;
import com.google.common.primitives.Ints;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import javax.inject.Inject;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;

/**
 * Class for generating an output vector that captures, when an exposure notification was received
 * in the past 14 days, and the user then verified a code.
 * <p><ul>
 * Bins signification:
 * <li>At most one bin has a value of 1 (others are 0)
 * <li>The bin with the 1 is at index: (reportType-1)*4+(classificationIndex-1), with reportType:
 * <li>- 1: CONFIRMED_TEST
 * <li>- 2: CONFIRMED_CLINICAL_DIAGNOSIS
 * <li>- 3: SELF_REPORT
 * <li>- 4: RECURSIVE (reserved for future use)
 * <li>- 5: REVOKED
 * </ul>
 */
public class KeysUploadedAfterNotificationMetric implements PrivateAnalyticsMetric {

  private static final String VERSION = "v2";
  public static final String METRIC_NAME = "SecondaryAttack14d-" + VERSION;

  private static final Duration NUM_DAYS = Duration.ofDays(14);

  @VisibleForTesting
  static final int NUM_CLASSIFICATION_BINS = 4;
  @VisibleForTesting
  static final int BIN_LENGTH = 5 * NUM_CLASSIFICATION_BINS;
  @VisibleForTesting
  static final int UNKNOWN_BIN = 0;
  @VisibleForTesting
  static final int CONFIRMED_TEST_BIN = 1;
  @VisibleForTesting
  static final int CONFIRMED_CLINICAL_DIAGNOSIS_BIN = 2;
  @VisibleForTesting
  static final int SELF_REPORTED_BIN = 3;
  private static final int RECURSIVE_BIN = 4; // Unused
  @VisibleForTesting
  static final int REVOKED_BIN = 5;

  private final ExposureNotificationSharedPreferences exposureNotificationSharedPreferences;

  @Inject
  KeysUploadedAfterNotificationMetric(
      ExposureNotificationSharedPreferences exposureNotificationSharedPreferences) {
    this.exposureNotificationSharedPreferences = exposureNotificationSharedPreferences;
  }

  @Override
  public ListenableFuture<List<Integer>> getDataVector() {
    int[] data = new int[BIN_LENGTH];

    Instant exposureNotificationTime = exposureNotificationSharedPreferences
        .getExposureNotificationLastShownTime();
    Instant lastSubmittedKeysTime = exposureNotificationSharedPreferences
        .getPrivateAnalyticsLastSubmittedKeysTime();
    Instant workerLastTime = exposureNotificationSharedPreferences
        .getPrivateAnalyticsWorkerLastTimeForBiweekly();
    if (lastSubmittedKeysTime.isAfter(exposureNotificationTime)
        && exposureNotificationTime.isAfter(workerLastTime)
        && exposureNotificationTime.isAfter(lastSubmittedKeysTime.minus(NUM_DAYS))) {

      int classification = exposureNotificationSharedPreferences
          .getExposureNotificationLastShownClassification();
      TestResult testResult = exposureNotificationSharedPreferences
          .getPrivateAnalyticsLastReportType();
      int reportType = getReportTypeIndex(testResult);

      if (reportType != UNKNOWN_BIN && classification > 0) {
        data[4 * (reportType - 1) + (classification - 1)] = 1;
      }
    }

    return Futures.immediateFuture(Ints.asList(data));
  }

  private int getReportTypeIndex(TestResult testResult) {
    if (testResult == null) {
      return UNKNOWN_BIN;
    }
    switch (testResult) {
      case CONFIRMED:
        return CONFIRMED_TEST_BIN;
      case LIKELY:
        return CONFIRMED_CLINICAL_DIAGNOSIS_BIN;
      case USER_REPORT:
        return SELF_REPORTED_BIN;
      case NEGATIVE:
        return REVOKED_BIN;
      default:
        return UNKNOWN_BIN;
    }
  }

  @Override
  public String getMetricName() {
    return METRIC_NAME;
  }

  @Override
  public int getMetricHammingWeight() {
    return 0;
  }
}
