// 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.

#include "base/functional/callback_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/sync/account_extension_tracker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/extensions/extension_dialog_utils.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/base/features.h"
#include "extensions/browser/ui_util.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"

namespace extensions {

void ShowUploadExtensionToAccountDialog(Profile* profile,
                                        gfx::NativeWindow parent,
                                        const Extension& extension,
                                        base::OnceClosure accept_callback,
                                        base::OnceClosure cancel_callback) {
#if BUILDFLAG(IS_CHROMEOS)
  CHECK(
      base::FeatureList::IsEnabled(syncer::kReplaceSyncPromosWithSignInPromos));
#endif
  CHECK(AccountExtensionTracker::Get(profile)->CanUploadAsAccountExtension(
      extension));

  // Show the avatar and email of the signed-in account as a custom view
  // between the dialog's subtitle and buttons.
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile->GetOriginalProfile());
  AccountInfo account_info = identity_manager->FindExtendedAccountInfo(
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin));
  CHECK(!account_info.IsEmpty());

  auto split_cancel_callback =
      base::SplitOnceCallback(std::move(cancel_callback));

  auto dialog_model =
      ui::DialogModel::Builder()
          .SetInternalName("UploadExtensionToAccountDialog")
          .SetTitle(l10n_util::GetStringUTF16(
              IDS_UPLOAD_MOVE_TO_ACCOUNT_DIALOG_TITLE))
          .OverrideShowCloseButton(false)
          .AddParagraph(ui::DialogModelLabel(l10n_util::GetStringFUTF16(
              IDS_EXTENSIONS_MOVE_TO_ACCOUNT_DIALOG_SUBTITLE,
              ui_util::GetFixupExtensionNameForUIDisplay(extension.name()))))
          .AddMenuItem(ui::ImageModel::FromImage(profiles::GetSizedAvatarIcon(
                           account_info.GetAvatarImage().value_or(gfx::Image()),
                           16, 16, profiles::SHAPE_CIRCLE)),
                       base::UTF8ToUTF16(account_info.GetEmail()),
                       base::DoNothing(),
                       ui::DialogModelMenuItem::Params().SetIsEnabled(false))
          .AddOkButton(
              std::move(accept_callback),
              ui::DialogModel::Button::Params().SetLabel(
                  l10n_util::GetStringUTF16(
                      IDS_EXTENSIONS_MOVE_TO_ACCOUNT_DIALOG_OK_BUTTON_LABEL)))
          .AddCancelButton(std::move(split_cancel_callback.first))
          .SetCloseActionCallback(std::move(split_cancel_callback.second))
          .Build();

  // Show a browser modal instead of one attached to the extensions icon to be
  // consistent with upload dialogs for other syncable types (e.g. bookmarks).
  ShowModalDialog(parent, std::move(dialog_model));
}

}  // namespace extensions
