// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "base/functional/bind.h"
#import "components/autofill/core/common/autofill_prefs.h"
#import "components/language/core/browser/pref_names.h"
#import "components/password_manager/core/common/password_manager_pref_names.h"
#import "components/prefs/pref_service.h"
#import "components/safe_browsing/core/common/safe_browsing_prefs.h"
#import "components/translate/core/browser/translate_pref_names.h"
#import "components/translate/core/browser/translate_prefs.h"
#import "ios/web_view/internal/autofill/cwv_autofill_prefs.h"
#import "ios/web_view/internal/autofill/cwv_password_affiliation.h"
#import "ios/web_view/internal/cwv_preferences_internal.h"
#import "ios/web_view/internal/passwords/web_view_password_manager_client.h"

@implementation CWVPreferences {
  PrefService* _prefService;
  // In-memory only.
  BOOL _triggerNonFatalCheck;
}

- (instancetype)initWithPrefService:(PrefService*)prefService {
  self = [super init];
  if (self) {
    _prefService = prefService;
  }
  return self;
}

#pragma mark - Public Methods

- (void)setTranslationEnabled:(BOOL)enabled {
  _prefService->SetBoolean(translate::prefs::kOfferTranslateEnabled, enabled);
}

- (BOOL)isTranslationEnabled {
  return _prefService->GetBoolean(translate::prefs::kOfferTranslateEnabled);
}

- (void)resetTranslationSettings {
  translate::TranslatePrefs translatePrefs(_prefService);
  translatePrefs.ResetToDefaults();
}

- (void)setProfileAutofillEnabled:(BOOL)enabled {
  autofill::prefs::SetAutofillProfileEnabled(_prefService, enabled);
}

- (BOOL)isProfileAutofillEnabled {
  return autofill::prefs::IsAutofillProfileEnabled(_prefService);
}

- (void)setCreditCardAutofillEnabled:(BOOL)enabled {
  autofill::prefs::SetAutofillPaymentMethodsEnabled(_prefService, enabled);
}

- (BOOL)isCreditCardAutofillEnabled {
  return autofill::prefs::IsAutofillPaymentMethodsEnabled(_prefService);
}

- (void)setAutofillAddressSyncEnabled:(BOOL)enabled {
  ios_web_view::SetAutofillAddressSyncEnabled(_prefService, enabled);
}

- (BOOL)isAutofillAddressSyncEnabled {
  return ios_web_view::IsAutofillAddressSyncEnabled(_prefService);
}

- (void)setPasswordAffiliationEnabled:(BOOL)enabled {
  ios_web_view::SetPasswordAffiliationEnabled(_prefService, enabled);
}

- (BOOL)isPasswordAffiliationEnabled {
  return ios_web_view::IsPasswordAffiliationEnabled(_prefService);
}

- (void)setPasswordAutofillEnabled:(BOOL)enabled {
  _prefService->SetBoolean(password_manager::prefs::kCredentialsEnableService,
                           enabled);
}

- (BOOL)isPasswordAutofillEnabled {
  return _prefService->GetBoolean(
      password_manager::prefs::kCredentialsEnableService);
}

- (void)setPasswordLeakCheckEnabled:(BOOL)enabled {
  _prefService->SetBoolean(
      password_manager::prefs::kPasswordLeakDetectionEnabled, enabled);
}

- (BOOL)isPasswordLeakCheckEnabled {
  return _prefService->GetBoolean(
      password_manager::prefs::kPasswordLeakDetectionEnabled);
}

- (void)setPasswordManagerSafeLifecycleEnabled:(BOOL)enabled {
  _prefService->SetBoolean(ios_web_view::kPasswordManagerSafeLifecycleEnabled,
                           enabled);
}

- (BOOL)isPasswordManagerSafeLifecycleEnabled {
  return _prefService->GetBoolean(
      ios_web_view::kPasswordManagerSafeLifecycleEnabled);
}

- (void)setSafeBrowsingEnabled:(BOOL)enabled {
  safe_browsing::SetSafeBrowsingState(
      _prefService,
      enabled ? safe_browsing::SafeBrowsingState::STANDARD_PROTECTION
              : safe_browsing::SafeBrowsingState::NO_SAFE_BROWSING,
      /*is_esb_enabled_by_account_integration=*/false);
}

- (BOOL)isSafeBrowsingEnabled {
  return safe_browsing::IsSafeBrowsingEnabled(*_prefService);
}

- (void)setAutofillVCNUsageEnabled:(BOOL)enabled {
  ios_web_view::SetAutofillVCNUsageEnabled(_prefService, enabled);
}

- (BOOL)isAutofillVCNUsageEnabled {
  return ios_web_view::IsAutofillVCNUsageEnabled(_prefService);
}

- (void)setAutofillSafeLifecycleEnabled:(BOOL)enabled {
  ios_web_view::SetAutofillSafeLifecycleEnabled(_prefService, enabled);
}

- (BOOL)isAutofillSafeLifecycleEnabled {
  return ios_web_view::IsAutofillSafeLifecycleEnabled(_prefService);
}

- (void)setTriggerNonFatalCheck:(BOOL)enabled {
  // TODO(crbug.com/503005390): Remove after release integration testing in
  // stable.
  _triggerNonFatalCheck = enabled;
}

- (BOOL)isTriggerNonFatalCheckEnabled {
  // TODO(crbug.com/503005390): Remove after release integration testing in
  // stable.
  return _triggerNonFatalCheck;
}

- (void)commitPendingWrite:(void (^)(void))completionHandler {
  _prefService->CommitPendingWrite(base::BindOnce(^{
    if (completionHandler) {
      completionHandler();
    }
  }));
}

- (void)shutDown {
  _prefService = nullptr;
}

@end
