// Copyright 2024 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/manual_fill/ui/address_view_controller.h"

#import "base/apple/foundation_util.h"
#import "ios/chrome/browser/autofill/manual_fill/ui/manual_fill_action_cell.h"
#import "ios/chrome/browser/autofill/manual_fill/ui/manual_fill_address_cell.h"
#import "ios/chrome/browser/shared/ui/list_model/list_item+Controller.h"
#import "ios/chrome/browser/shared/ui/table_view/legacy_chrome_table_view_controller_test.h"
#import "ui/base/device_form_factor.h"

namespace {

enum ItemType : NSInteger {
  kItemTypeSampleOne = kItemTypeEnumZero,
  kItemTypeSampleTwo
};

}  // namespace

class AddressViewControllerTest : public LegacyChromeTableViewControllerTest {
 protected:
  void SetUp() override {
    LegacyChromeTableViewControllerTest::SetUp();
    CreateController();
    CheckController();
  }

  LegacyChromeTableViewController* InstantiateController() override {
    AddressViewController* view_controller =
        [[AddressViewController alloc] init];
    [view_controller loadModel];
    return view_controller;
  }

  // Returns the header item at `section`.
  id GetHeaderItem(int section) {
    return [controller().tableViewModel headerForSectionIndex:section];
  }

  // Returns the type of the table view item at `item` in `section`.
  NSInteger GetTableViewItemType(int section, int item) {
    return base::apple::ObjCCastStrict<TableViewItem>(
               GetTableViewItem(section, item))
        .type;
  }
};

// Tests the following:
// 1. "No address items present" message is shown alongside the action items if
// there are no data items.
// 2. "No address items present" message is removed once there are address items
// to be shown in the view.
TEST_F(AddressViewControllerTest, CheckNoDataItemsMessageRemoved) {
  AddressViewController* address_view_controller =
      base::apple::ObjCCastStrict<AddressViewController>(controller());

  ManualFillActionItem* action_item =
      [[ManualFillActionItem alloc] initWithTitle:nil action:nil];
  action_item.type = kItemTypeSampleOne;

  // First, send no data items so that the "no address items to show" message is
  // displayed.
  [address_view_controller presentAddresses:@[]];
  [address_view_controller presentActions:@[ action_item ]];

  // Make sure that the table view content is as expected.
  EXPECT_EQ(NumberOfSections(), 2);
  EXPECT_TRUE(GetHeaderItem(/*section=*/0));
  EXPECT_EQ(NumberOfItemsInSection(0), 0);
  EXPECT_EQ(NumberOfItemsInSection(1), 1);
  EXPECT_EQ(GetTableViewItemType(/*section=*/1, /*item=*/0),
            kItemTypeSampleOne);

  // Add an address data item.
  ManualFillAddressItem* addressItem =
      [[ManualFillAddressItem alloc] initWithAddress:nil
                                     contentInjector:nil
                                         menuActions:@[]
                                           cellIndex:0
                         cellIndexAccessibilityLabel:nil
                              showAutofillFormButton:NO];
  [address_view_controller presentAddresses:@[ addressItem ]];
  // Override the type for the test.
  addressItem.type = kItemTypeSampleTwo;

  // Check that the "no address present" message is removed.
  EXPECT_EQ(NumberOfSections(), 2);
  EXPECT_FALSE(GetHeaderItem(/*section=*/0));
  EXPECT_FALSE(GetHeaderItem(/*section=*/1));
  EXPECT_EQ(NumberOfItemsInSection(0), 1);
  EXPECT_EQ(NumberOfItemsInSection(1), 1);
  EXPECT_EQ(GetTableViewItemType(/*section=*/1, /*item=*/0),
            kItemTypeSampleOne);
  EXPECT_EQ(GetTableViewItemType(/*section=*/0, /*item=*/0),
            kItemTypeSampleTwo);
}
