// Copyright 2022 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/signin/model/system_identity_manager.h"

#import "base/functional/bind.h"
#import "base/functional/callback.h"
#import "google_apis/gaia/gaia_id.h"

namespace {

using DismissViewCallback = SystemIdentityManager::DismissViewCallback;

}  // anonymous namespace

SystemIdentityManager::PresentDialogConfiguration::
    PresentDialogConfiguration() {}

SystemIdentityManager::PresentDialogConfiguration::
    ~PresentDialogConfiguration() {}

SystemIdentityManager::PresentDialogConfiguration::PresentDialogConfiguration(
    SystemIdentityManager::PresentDialogConfiguration&& configuration) {
  identity = configuration.identity;
  view_controller = configuration.view_controller;
  animated = configuration.animated;
  dismissal_completion = std::move(configuration.dismissal_completion);
}

SystemIdentityManager::SystemIdentityManager() = default;

SystemIdentityManager::~SystemIdentityManager() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void SystemIdentityManager::AddObserver(
    SystemIdentityManagerObserver* observer) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  observers_.AddObserver(observer);
}

void SystemIdentityManager::RemoveObserver(
    SystemIdentityManagerObserver* observer) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  observers_.RemoveObserver(observer);
}

DismissViewCallback SystemIdentityManager::PresentAccountDetailsController(
    id<SystemIdentity> identity,
    UIViewController* view_controller,
    bool animated,
    base::OnceClosure dismissal_completion) {
  SystemIdentityManager::PresentDialogConfiguration configuration;
  configuration.identity = identity;
  configuration.view_controller = view_controller;
  configuration.animated = animated;
  configuration.dismissal_completion = std::move(dismissal_completion);
  return PresentAccountDetailsController(std::move(configuration));
}

DismissViewCallback
SystemIdentityManager::PresentWebAndAppSettingDetailsController(
    id<SystemIdentity> identity,
    UIViewController* view_controller,
    bool animated,
    base::OnceClosure dismissal_completion) {
  SystemIdentityManager::PresentDialogConfiguration configuration;
  configuration.identity = identity;
  configuration.view_controller = view_controller;
  configuration.animated = animated;
  configuration.dismissal_completion = std::move(dismissal_completion);
  return PresentWebAndAppSettingDetailsController(std::move(configuration));
}

DismissViewCallback
SystemIdentityManager::PresentLinkedServicesSettingsDetailsController(
    id<SystemIdentity> identity,
    UIViewController* view_controller,
    bool animated,
    base::OnceClosure dismissal_completion) {
  SystemIdentityManager::PresentDialogConfiguration configuration;
  configuration.identity = identity;
  configuration.view_controller = view_controller;
  configuration.animated = animated;
  configuration.dismissal_completion = std::move(dismissal_completion);
  return PresentLinkedServicesSettingsDetailsController(
      std::move(configuration));
}

void SystemIdentityManager::FireIdentityListChanged() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  for (auto& observer : observers_) {
    observer.OnIdentityListChanged();
  }
}

void SystemIdentityManager::FireIdentityUpdated(id<SystemIdentity> identity) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  for (auto& observer : observers_) {
    observer.OnIdentityUpdated(identity);
  }
}

void SystemIdentityManager::FireIdentityRefreshTokenUpdated(
    id<SystemIdentity> identity) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  for (auto& observer : observers_) {
    observer.OnIdentityRefreshTokenUpdated(identity);
  }
}

void SystemIdentityManager::FireIdentityAccessTokenRefreshFailed(
    id<SystemIdentity> identity,
    id<RefreshAccessTokenError> error,
    const std::set<std::string>& scopes) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  for (auto& observer : observers_) {
    observer.OnIdentityAccessTokenRefreshFailed(identity, error, scopes);
  }
}

bool SystemIdentityManager::IsScopeLimitedError(
    id<RefreshAccessTokenError> error) {
  return false;
}

