// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/infobars/model/infobar_metrics_recorder.h"

#import "base/metrics/histogram_functions.h"
#import "base/metrics/histogram_macros.h"
#import "base/notreached.h"

namespace {

// Histogram names for InfobarTypeConfirm.
// Banner.
const char kInfobarConfirmBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeConfirm";
const char kInfobarConfirmBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeConfirm";
// Modal.
const char kInfobarConfirmModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeConfirm";
// Badge.
const char kInfobarConfirmBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeConfirm";

// Histogram names for InfobarTypePasswordSave.
// Banner.
const char kInfobarPasswordSaveBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypePasswordSave";
const char kInfobarPasswordSaveBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypePasswordSave";
// Modal.
const char kInfobarPasswordSaveModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypePasswordSave";
// Badge.
const char kInfobarPasswordSaveBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypePasswordSave";

// Histogram names for InfobarTypePasswordUpdate.
// Banner.
const char kInfobarPasswordUpdateBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypePasswordUpdate";
const char kInfobarPasswordUpdateBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypePasswordUpdate";
// Modal.
const char kInfobarPasswordUpdateModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypePasswordUpdate";
// Badge.
const char kInfobarPasswordUpdateBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypePasswordUpdate";

// Histogram names for InfobarTypeSaveCard.
// Banner.
const char kInfobarSaveCardBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeSaveCard";
const char kInfobarSaveCardBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeSaveCard";
// Modal.
const char kInfobarSaveCardModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeSaveCard";
// Badge.
const char kInfobarSaveCardBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeSaveCard";

// Histogram names for InfobarTypeTranslate.
// Banner.
const char kInfobarTranslateBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeTranslate";
const char kInfobarTranslateBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeTranslate";
// Modal.
const char kInfobarTranslateModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeTranslate";
// Badge.
const char kInfobarTranslateBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeTranslate";

// Histogram names for InfobarTypeSaveAutofillAddressProfile.
// Banner.
const char kInfobarAutofillAddressBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeAutofillAddressProfile";
const char kInfobarAutofillAddressBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeAutofillAddressProfile";
// Modal.
const char kInfobarAutofillAddressModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeAutofillAddressProfile";
// Badge.
const char kInfobarAutofillAddressBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeAutofillAddressProfile";

// Histogram names for InfobarTypeAutofillAiSaveEntity.
// Banner.
const char kInfobarAutofillAiSaveEntityBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeAutofillAiSaveEntity";
const char kInfobarAutofillAiSaveEntityBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeAutofillAiSaveEntity";
// Modal.
const char kInfobarAutofillAiSaveEntityModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeAutofillAiSaveEntity";

// Histogram names for InfobarTypePermissions.
// Banner.
const char kInfobarPermissionsBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypePermissions";
const char kInfobarPermissionsBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypePermissions";
// Modal.
const char kInfobarPermissionsModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypePermissions";
// Badge.
const char kInfobarPermissionsBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypePermissions";

// Histogram names for InfobarTypeTailoredSecurityService.
const char kInfobarTailoredSecurityServiceBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeTailoredSecurityService";
const char kInfobarTailoredSecurityServiceBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeTailoredSecurityService";
// Modal.
const char kInfobarTailoredSecurityServiceModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeTailoredSecurityService";

// Histogram names for InfobarTypeSyncError.
// Banner.
const char kInfobarSyncErrorBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeSyncError";
const char kInfobarSyncErrorBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeSyncError";
// Modal.
const char kInfobarSyncErrorModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeSyncError";
// Badge.
const char kInfobarSyncErrorBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeSyncError";

// Histogram names for the Enhanced Safe Browsing banner.
const char kInfobarEnhancedSafeBrowsingBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeEnhancedSafeBrowsing";
const char kInfobarEnhancedSafeBrowsingBannerDismissTypeHistgram[] =
    "Mobile.messages.Banner.Dismiss.InfobarTypeEnhancedSafeBrowsing";
const char kInfobarEnhancedSafeBrowsingModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeEnhancedSafeBrowsing";
const char kInfobarEnhancedSafeBrowsingBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeEnhancedSafeBrowsing";

// Histogram names for the non modal sign-in promo banner.
const char kInfobarTypeSigninBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeSignin";
const char kInfobarTypeSigninBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeSignin";
const char kInfobarTypeSigninModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeSignin";
const char kInfobarTypeSigninBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeSignin";

// Histogram names for collaboration group banner.
const char kInfobarCollaborationGroupBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.kInfobarCollaborationGroup";
const char kInfobarCollaborationGroupBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.kInfobarCollaborationGroup";
const char kInfobarCollaborationGroupModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.kInfobarCollaborationGroup";
const char kInfobarCollaborationGroupBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.kInfobarCollaborationGroup";

// Histogram names for collaboration out-of-date message banner.
const char kInfobarCollaborationOutOfDateBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.kInfobarCollaborationOutOfDate";
const char kInfobarCollaborationOutOfDateBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.kInfobarCollaborationOutOfDate";
const char kInfobarCollaborationOutOfDateModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.kInfobarCollaborationOutOfDate";
const char kInfobarCollaborationOutOfDateBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.kInfobarCollaborationOutOfDate";

// Histogram names for InfobarTypeSaveCvc Banner.
const char kInfobarSaveCvcBannerEventHistogram[] =
    "Mobile.Messages.Banner.Event.InfobarTypeSaveCvc";
const char kInfobarSaveCvcBannerDismissTypeHistogram[] =
    "Mobile.Messages.Banner.Dismiss.InfobarTypeSaveCvc";
// Modal.
const char kInfobarSaveCvcModalEventHistogram[] =
    "Mobile.Messages.Modal.Event.InfobarTypeSaveCvc";
// Badge.
const char kInfobarSaveCvcBadgeTappedHistogram[] =
    "Mobile.Messages.Badge.Tapped.InfobarTypeSaveCvc";
}  // namespace

@interface InfobarMetricsRecorder ()
// The Infobar type for the metrics recorder.
@property(nonatomic, assign) InfobarType infobarType;
@end

@implementation InfobarMetricsRecorder

#pragma mark - Public

- (instancetype)initWithType:(InfobarType)infobarType {
  self = [super init];
  if (self) {
    _infobarType = infobarType;
  }
  return self;
}

- (void)recordBannerEvent:(MobileMessagesBannerEvent)event {
  switch (self.infobarType) {
    case InfobarType::kInfobarTypeConfirm:
      UMA_HISTOGRAM_ENUMERATION(kInfobarConfirmBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypePasswordSave:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordSaveBannerEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypePasswordUpdate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordUpdateBannerEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypeSaveCard:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCardBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeTranslate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTranslateBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSaveAutofillAddressProfile:
      UMA_HISTOGRAM_ENUMERATION(kInfobarAutofillAddressBannerEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypePermissions:
      base::UmaHistogramEnumeration(kInfobarPermissionsBannerEventHistogram,
                                    event);
      break;
    case InfobarType::kInfobarTypeTailoredSecurityService:
      base::UmaHistogramEnumeration(
          kInfobarTailoredSecurityServiceBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSyncError:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSyncErrorBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeEnhancedSafeBrowsing:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarEnhancedSafeBrowsingBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSignin:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTypeSigninBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeCollaborationGroup:
      UMA_HISTOGRAM_ENUMERATION(kInfobarCollaborationGroupBannerEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypeCollaborationOutOfDate:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarCollaborationOutOfDateBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSaveCvc:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCvcBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeAutofillAiSaveEntity:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarAutofillAiSaveEntityBannerEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeFormsAiPrivateInference:
      break;
    case InfobarType::kInfobarTypeReaderMode:
      // Reader Mode infobar does not support banners.
      NOTREACHED();
  }
}

- (void)recordBannerDismissType:(MobileMessagesBannerDismissType)dismissType {
  switch (self.infobarType) {
    case InfobarType::kInfobarTypeConfirm:
      UMA_HISTOGRAM_ENUMERATION(kInfobarConfirmBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypePasswordSave:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordSaveBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypePasswordUpdate:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarPasswordUpdateBannerDismissTypeHistogram, dismissType);
      break;
    case InfobarType::kInfobarTypeSaveCard:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCardBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypeTranslate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTranslateBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypeSaveAutofillAddressProfile:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarAutofillAddressBannerDismissTypeHistogram, dismissType);
      break;
    case InfobarType::kInfobarTypePermissions:
      base::UmaHistogramEnumeration(
          kInfobarPermissionsBannerDismissTypeHistogram, dismissType);
      break;
    case InfobarType::kInfobarTypeTailoredSecurityService:
      base::UmaHistogramEnumeration(
          kInfobarTailoredSecurityServiceBannerDismissTypeHistogram,
          dismissType);
      break;
    case InfobarType::kInfobarTypeSyncError:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSyncErrorBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypeEnhancedSafeBrowsing:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarEnhancedSafeBrowsingBannerDismissTypeHistgram, dismissType);
      break;
    case InfobarType::kInfobarTypeSignin:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTypeSigninBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypeCollaborationGroup:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarCollaborationGroupBannerDismissTypeHistogram, dismissType);
      break;
    case InfobarType::kInfobarTypeCollaborationOutOfDate:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarCollaborationOutOfDateBannerDismissTypeHistogram,
          dismissType);
      break;
    case InfobarType::kInfobarTypeSaveCvc:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCvcBannerDismissTypeHistogram,
                                dismissType);
      break;
    case InfobarType::kInfobarTypeAutofillAiSaveEntity:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarAutofillAiSaveEntityBannerDismissTypeHistogram, dismissType);
      break;
    case InfobarType::kInfobarTypeFormsAiPrivateInference:
      break;
    case InfobarType::kInfobarTypeReaderMode:
      // Reader Mode infobar does not support banners.
      NOTREACHED();
  }
}

- (void)recordBannerOnScreenDuration:(base::TimeDelta)duration {
  DEPRECATED_UMA_HISTOGRAM_MEDIUM_TIMES("Mobile.Messages.Banner.OnScreenTime",
                                        duration);
}

- (void)recordModalEvent:(MobileMessagesModalEvent)event {
  switch (self.infobarType) {
    case InfobarType::kInfobarTypeConfirm:
      UMA_HISTOGRAM_ENUMERATION(kInfobarConfirmModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypePasswordSave:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordSaveModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypePasswordUpdate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordUpdateModalEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypeSaveCard:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCardModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeTranslate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTranslateModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSaveAutofillAddressProfile:
      UMA_HISTOGRAM_ENUMERATION(kInfobarAutofillAddressModalEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypePermissions:
      base::UmaHistogramEnumeration(kInfobarPermissionsModalEventHistogram,
                                    event);
      break;
    case InfobarType::kInfobarTypeTailoredSecurityService:
      base::UmaHistogramEnumeration(
          kInfobarTailoredSecurityServiceModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSyncError:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSyncErrorModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeEnhancedSafeBrowsing:
      UMA_HISTOGRAM_ENUMERATION(kInfobarEnhancedSafeBrowsingModalEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypeSignin:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTypeSigninModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeCollaborationGroup:
      UMA_HISTOGRAM_ENUMERATION(kInfobarCollaborationGroupModalEventHistogram,
                                event);
      break;
    case InfobarType::kInfobarTypeCollaborationOutOfDate:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarCollaborationOutOfDateModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeSaveCvc:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCvcModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeAutofillAiSaveEntity:
      base::UmaHistogramEnumeration(
          kInfobarAutofillAiSaveEntityModalEventHistogram, event);
      break;
    case InfobarType::kInfobarTypeFormsAiPrivateInference:
      // FormsAiPrivateInference notice does not support modals.
      NOTREACHED();
    case InfobarType::kInfobarTypeReaderMode:
      // Reader Mode infobar does not support modals.
      NOTREACHED();
  }
}

- (void)recordBadgeTappedInState:(MobileMessagesBadgeState)state {
  switch (self.infobarType) {
    case InfobarType::kInfobarTypeConfirm:
      UMA_HISTOGRAM_ENUMERATION(kInfobarConfirmBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypePasswordSave:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordSaveBadgeTappedHistogram,
                                state);
      break;
    case InfobarType::kInfobarTypePasswordUpdate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarPasswordUpdateBadgeTappedHistogram,
                                state);
      break;
    case InfobarType::kInfobarTypeSaveCard:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCardBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeTranslate:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTranslateBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeSaveAutofillAddressProfile:
      UMA_HISTOGRAM_ENUMERATION(kInfobarAutofillAddressBadgeTappedHistogram,
                                state);
      break;
    case InfobarType::kInfobarTypePermissions:
      base::UmaHistogramEnumeration(kInfobarPermissionsBadgeTappedHistogram,
                                    state);
      break;
    case InfobarType::kInfobarTypeTailoredSecurityService:
      // TailoredSecurityService infobar doesn't have a badge.
      NOTREACHED();
    case InfobarType::kInfobarTypeSyncError:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSyncErrorBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeEnhancedSafeBrowsing:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarEnhancedSafeBrowsingBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeSignin:
      UMA_HISTOGRAM_ENUMERATION(kInfobarTypeSigninBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeCollaborationGroup:
      UMA_HISTOGRAM_ENUMERATION(kInfobarCollaborationGroupBadgeTappedHistogram,
                                state);
      break;
    case InfobarType::kInfobarTypeCollaborationOutOfDate:
      UMA_HISTOGRAM_ENUMERATION(
          kInfobarCollaborationOutOfDateBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeSaveCvc:
      UMA_HISTOGRAM_ENUMERATION(kInfobarSaveCvcBadgeTappedHistogram, state);
      break;
    case InfobarType::kInfobarTypeFormsAiPrivateInference:
      // FormsAiPrivateInference notice does not support badges.
      NOTREACHED();
    case InfobarType::kInfobarTypeAutofillAiSaveEntity:
      // Autofill Ai infobar does not support badges.
      NOTREACHED();
    case InfobarType::kInfobarTypeReaderMode:
      // Reader Mode infobar badge is not interactive.
      NOTREACHED();
  }
}

@end
