// Copyright 2026 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/autofill/autofill_ai/public/autofill_ai_ui_util.h"

#import "base/feature_list.h"
#import "base/strings/sys_string_conversions.h"
#import "components/autofill/core/browser/data_model/autofill_ai/entity_type.h"
#import "components/autofill/core/common/autofill_payments_features.h"
#import "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/shared/ui/buildflags.h"
#import "ios/chrome/browser/shared/ui/symbols/symbols.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/elements/branded_navigation_item_title_view.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ui/base/l10n/l10n_util.h"

namespace {
constexpr CGFloat kWalletLogoHeight = 26.0;
constexpr CGFloat kWalletLogoSpacing = 6.0;
}  // namespace

namespace autofill {

UIImage* DefaultIconForAutofillAiEntityType(EntityTypeName entity_type_name,
                                            bool is_personal_context,
                                            CGFloat symbol_point_size,
                                            UIColor* tint_color) {
  Symbol symbol;
  UIColor* color = tint_color ?: [UIColor colorNamed:kTextPrimaryColor];

  switch (entity_type_name) {
    case EntityTypeName::kPassport:
      symbol = is_personal_context ? SymbolPassportSpark : SymbolPassport;
      break;
    case EntityTypeName::kDriversLicense:
    case EntityTypeName::kNationalIdCard:
      symbol = is_personal_context ? SymbolPersonTextRectangleSpark
                                   : SymbolPersonTextRectangle;
      break;
    case EntityTypeName::kVehicle:
      symbol = is_personal_context ? SymbolCarSpark : SymbolCar;
      break;
    case EntityTypeName::kKnownTravelerNumber:
    case EntityTypeName::kRedressNumber:
      symbol = is_personal_context ? SymbolPersonTextRectangle2Spark
                                   : SymbolPersonTextRectangle2;
      break;
    case EntityTypeName::kFlightReservation:
      symbol = is_personal_context ? SymbolAirplaneUpSpark : SymbolAirplaneUp;
      break;
    case EntityTypeName::kShipment:
      symbol = is_personal_context ? SymbolTruckBoxSpark : SymbolTruckBox;
      break;
    case EntityTypeName::kOrder:
      symbol = is_personal_context ? SymbolBagSpark : SymbolBag;
      break;
    default:
      return nil;
  }

  return SymbolWithPalette(SymbolWithPointSize(symbol, symbol_point_size),
                           @[ color ]);
}

NSString* DisplayNameForAutofillAiAttributeType(AttributeType attribute_type) {
  if (attribute_type.name() == AttributeTypeName::kVehicleVin) {
    return l10n_util::GetNSString(IDS_IOS_AUTOFILL_AI_VEHICLE_VIN_NAME);
  }
  return base::SysUTF16ToNSString(attribute_type.GetNameForI18n());
}

NSString* GetDialogTitleForSaveEntity(EntityTypeName entity_type_name) {
  switch (entity_type_name) {
    case EntityTypeName::kPassport:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_PASSPORT_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kDriversLicense:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_DRIVERS_LICENSE_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kNationalIdCard:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_NATIONAL_ID_CARD_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kVehicle:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_VEHICLE_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kKnownTravelerNumber:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_KNOWN_TRAVELER_NUMBER_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kRedressNumber:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_SAVE_REDRESS_NUMBER_ENTITY_DIALOG_TITLE);
    default:
      return base::SysUTF16ToNSString(
          EntityType(entity_type_name).GetNameForI18n());
  }
}

NSString* GetDialogTitleForUpdateEntity(EntityTypeName entity_type_name) {
  switch (entity_type_name) {
    case EntityTypeName::kPassport:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_PASSPORT_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kDriversLicense:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_DRIVERS_LICENSE_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kNationalIdCard:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_NATIONAL_ID_CARD_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kVehicle:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_VEHICLE_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kKnownTravelerNumber:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_KNOWN_TRAVELER_NUMBER_ENTITY_DIALOG_TITLE);
    case EntityTypeName::kRedressNumber:
      return l10n_util::GetNSString(
          IDS_IOS_AUTOFILL_AI_UPDATE_REDRESS_NUMBER_ENTITY_DIALOG_TITLE);
    default:
      return base::SysUTF16ToNSString(
          EntityType(entity_type_name).GetNameForI18n());
  }
}

NSString* GetDialogTitleForAddEntity(EntityTypeName entity_type_name) {
  switch (entity_type_name) {
    case EntityTypeName::kPassport:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_ADD_PASSPORT_ENTITY);
    case EntityTypeName::kDriversLicense:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_ADD_DRIVERS_LICENSE_ENTITY);
    case EntityTypeName::kNationalIdCard:
      return l10n_util::GetNSString(
          IDS_AUTOFILL_AI_ADD_NATIONAL_ID_CARD_ENTITY);
    case EntityTypeName::kVehicle:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_ADD_VEHICLE_ENTITY);
    case EntityTypeName::kKnownTravelerNumber:
      return l10n_util::GetNSString(
          IDS_AUTOFILL_AI_ADD_KNOWN_TRAVELER_NUMBER_ENTITY);
    case EntityTypeName::kRedressNumber:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_ADD_REDRESS_NUMBER_ENTITY);
    default:
      return base::SysUTF16ToNSString(
          EntityType(entity_type_name).GetNameForI18n());
  }
}

NSString* GetDialogTitleForViewEntity(EntityTypeName entity_type_name) {
  return base::SysUTF16ToNSString(
      EntityType(entity_type_name).GetNameForI18n());
}

NSString* GetDialogTitleForEditEntity(EntityTypeName entity_type_name) {
  switch (entity_type_name) {
    case EntityTypeName::kPassport:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_EDIT_PASSPORT_ENTITY);
    case EntityTypeName::kDriversLicense:
      return l10n_util::GetNSString(
          IDS_AUTOFILL_AI_EDIT_DRIVERS_LICENSE_ENTITY);
    case EntityTypeName::kNationalIdCard:
      return l10n_util::GetNSString(
          IDS_AUTOFILL_AI_EDIT_NATIONAL_ID_CARD_ENTITY);
    case EntityTypeName::kVehicle:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_EDIT_VEHICLE_ENTITY);
    case EntityTypeName::kKnownTravelerNumber:
      return l10n_util::GetNSString(
          IDS_AUTOFILL_AI_EDIT_KNOWN_TRAVELER_NUMBER_ENTITY);
    case EntityTypeName::kRedressNumber:
      return l10n_util::GetNSString(IDS_AUTOFILL_AI_EDIT_REDRESS_NUMBER_ENTITY);
    default:
      return base::SysUTF16ToNSString(
          EntityType(entity_type_name).GetNameForI18n());
  }
}

NSString* GetSaveEntityToWalletFooterText(NSString* user_email) {
  NSString* googleWallet =
      l10n_util::GetNSString(IDS_AUTOFILL_GOOGLE_WALLET_TITLE);
  NSString* linkText =
      l10n_util::GetNSString(IDS_AUTOFILL_MANAGE_YOUR_INFO_LINK);
  NSString* formattedLink =
      [NSString stringWithFormat:@"BEGIN_LINK%@END_LINK", linkText];
  return l10n_util::GetNSStringF(GetSaveToWalletSubtitleStringId(),
                                 base::SysNSStringToUTF16(googleWallet),
                                 base::SysNSStringToUTF16(formattedLink),
                                 base::SysNSStringToUTF16(googleWallet),
                                 base::SysNSStringToUTF16(user_email));
}

NSString* GetUpdateEntitySavedInWalletFooterText(NSString* user_email) {
  NSString* googleWallet =
      l10n_util::GetNSString(IDS_AUTOFILL_GOOGLE_WALLET_TITLE);
  NSString* formattedLink =
      [NSString stringWithFormat:@"BEGIN_LINK%@END_LINK", googleWallet];
  return l10n_util::GetNSStringF(
      IDS_AUTOFILL_AI_UPDATE_ENTITY_TO_WALLET_DIALOG_SUBTITLE,
      base::SysNSStringToUTF16(formattedLink),
      base::SysNSStringToUTF16(user_email));
}

GURL GetManageYourInfoURL() {
  return GURL("https://support.google.com/wallet?p=private_use_across_google");
}

GURL GetGoogleWalletPassesURL() {
  return GURL("https://wallet.google.com/wallet/passes");
}

UIImage* GetWalletLogo(CGFloat point_size, UIColor* tint_color) {
#if BUILDFLAG(IOS_USE_BRANDED_ASSETS)
  Symbol symbol =
      base::FeatureList::IsEnabled(features::kAutofillEnableGradientGoogleLogos)
          ? SymbolGoogleWalletIconV2
          : SymbolGoogleWalletIcon;
  return MakeSymbolMulticolor(SymbolWithPointSize(symbol, point_size));
#else
  return SymbolWithPalette(
      SymbolWithPointSize(SymbolSparkles, point_size),
      @[ tint_color ?: [UIColor colorNamed:kBlue600Color] ]);
#endif
}

UIView* CreateBrandedTitleForWalletSave(NSString* title) {
  BrandedNavigationItemTitleView* titleView =
      [[BrandedNavigationItemTitleView alloc] init];
  titleView.imageLogo = GetWalletLogo(kWalletLogoHeight, nil);
  titleView.title = title;
  titleView.titleLogoSpacing = kWalletLogoSpacing;
  titleView.accessibilityLabel = title;
  return titleView;
}

int GetSaveToWalletSubtitleStringId() {
  return IDS_AUTOFILL_AI_SAVE_ENTITY_TO_WALLET_DIALOG_SUBTITLE_NEW;
}

int GetSaveEntityAcceptButtonStringId() {
  return IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_OK_BUTTON_LABEL;
}

}  // namespace autofill
