// Copyright 2025 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/credential_exchange/ui/credential_import_view_controller.h"

#import "base/check_op.h"
#import "base/notreached.h"
#import "base/strings/utf_string_conversions.h"
#import "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/credential_exchange/public/credential_import_stage.h"
#import "ios/chrome/browser/data_import/public/import_data_item.h"
#import "ios/chrome/browser/data_import/public/import_data_item_consumer.h"
#import "ios/chrome/browser/data_import/ui/import_data_item_table_view.h"
#import "ios/chrome/common/ui/button_stack/button_stack_configuration.h"
#import "ios/chrome/grit/ios_branded_strings.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ui/base/l10n/l10n_util.h"

namespace {

// Number of expected items in the table.
constexpr int kExpectedItemCount = 2;

// Exporter display names of some of the password manager apps.
NSString* const k1PasswordDisplayName = @"1Password";
NSString* const kApplePasswordsDisplayName = @"Apple Passwords";
NSString* const kBitwardenDisplayName = @"Bitwarden";
NSString* const kDashlaneDisplayName = @"Dashlane";
NSString* const kKeeperDisplayName = @"Keeper";

// Returns the name of the banner based on `exporterDisplayName`. Returns a
// generic banner if the name does not match any available banners.
NSString* GetBannerName(NSString* exporterDisplayName) {
  static NSDictionary* banners = @{
    k1PasswordDisplayName : @"credential_import_1password",
    kApplePasswordsDisplayName : @"credential_import_apple",
    kBitwardenDisplayName : @"credential_import_bitwarden",
    kDashlaneDisplayName : @"credential_import_dashlane",
    kKeeperDisplayName : @"credential_import_keeper",
  };

  NSString* banner = banners[exporterDisplayName];
  return banner ?: @"credential_import_generic";
}

}  // namespace

@interface CredentialImportViewController () <UITableViewDelegate>
@end

@implementation CredentialImportViewController {
  // Displays the status of importing specific credential types.
  ImportDataItemTableView* _tableView;
}

// Overrides property of super class (PromoStyleViewControllerDelegate).
@dynamic delegate;

- (void)viewDidLoad {
  self.titleText =
      l10n_util::GetNSString(IDS_IOS_CREDENTIAL_EXCHANGE_IMPORT_TITLE);
  self.configuration.primaryActionString = l10n_util::GetNSString(IDS_CONTINUE);
  self.navigationItem.leftBarButtonItem =
      [[UIBarButtonItem alloc] initWithTitle:l10n_util::GetNSString(IDS_CANCEL)
                                       style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(cancelButtonTapped)];
  [super viewDidLoad];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView*)tableView
    accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath {
  CHECK_EQ(tableView, _tableView);
  [self.delegate
      didTapInfoButtonForType:[_tableView itemTypeForIndexPath:indexPath]];
}

#pragma mark - CredentialImportConsumer

- (void)setImportDataItem:(ImportDataItem*)importDataItem {
  if (!_tableView) {
    [self createTableView];
  }

  [_tableView populateItem:importDataItem];
}

- (void)setUserEmail:(const std::string&)userEmail {
  self.disclaimerText =
      l10n_util::GetNSStringF(IDS_IOS_CREDENTIAL_EXCHANGE_IMPORT_DISCLAIMER,
                              base::UTF8ToUTF16(userEmail));
}

- (void)setExporterDisplayName:(NSString*)exporterDisplayName {
  self.bannerName = GetBannerName(exporterDisplayName);
}

- (void)transitionToImportStage:(CredentialImportStage)importStage {
  switch (importStage) {
    case CredentialImportStage::kNotStarted:
      NOTREACHED();
    case CredentialImportStage::kImporting:
      self.navigationItem.leftBarButtonItem.enabled = NO;
      self.configuration.primaryActionEnabled = NO;
      [self reloadConfiguration];
      [_tableView notifyImportStart];
      break;
    case CredentialImportStage::kImported:
      self.navigationItem.leftBarButtonItem = nil;
      self.configuration.primaryActionString = l10n_util::GetNSString(IDS_DONE);
      self.configuration.primaryActionEnabled = YES;
      [self reloadConfiguration];
      break;
  }
}

#pragma mark - Actions

// Invoked when the user taps the "Cancel" button.
- (void)cancelButtonTapped {
  [self.delegate didTapDismissButton];
}

#pragma mark - Private

// Creates the table view for this view controller.
- (void)createTableView {
  _tableView =
      [[ImportDataItemTableView alloc] initWithItemCount:kExpectedItemCount];
  _tableView.delegate = self;
  UIView* specificContentView = self.specificContentView;
  [specificContentView addSubview:_tableView];
  [NSLayoutConstraint activateConstraints:@[
    [_tableView.topAnchor
        constraintEqualToAnchor:specificContentView.topAnchor],
    [_tableView.bottomAnchor
        constraintLessThanOrEqualToAnchor:specificContentView.bottomAnchor],
    [_tableView.leadingAnchor
        constraintEqualToAnchor:specificContentView.leadingAnchor],
    [_tableView.trailingAnchor
        constraintEqualToAnchor:specificContentView.trailingAnchor],
  ]];
}

@end
