// Copyright 2018 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/strings/sys_string_conversions.h"
#import "components/password_manager/core/browser/password_form.h"
#import "components/password_manager/core/browser/password_ui_utils.h"
#import "ios/chrome/browser/autofill/manual_fill/model/manual_fill_credential+PasswordForm.h"
#import "net/base/registry_controlled_domains/registry_controlled_domain.h"
#import "url/gurl.h"

@implementation ManualFillCredential (PasswordForm)

- (instancetype)initWithPasswordForm:
                    (const password_manager::PasswordForm&)passwordForm
                            isBackup:(BOOL)isBackup {
  std::string host = passwordForm.url.GetHost();
  std::string site_name =
      net::registry_controlled_domains::GetDomainAndRegistry(
          host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
  NSString* siteName = base::SysUTF8ToNSString(site_name);
  NSString* username = base::SysUTF16ToNSString(passwordForm.username_value);
  NSString* password = base::SysUTF16ToNSString(passwordForm.password_value);
  NSString* displayName = base::SysUTF16ToNSString(passwordForm.display_name);
  NSString* credentialHost = base::SysUTF8ToNSString(host);
  if ([credentialHost hasPrefix:@"www."] && credentialHost.length > 4) {
    credentialHost = [credentialHost substringFromIndex:4];
  }
  return [self initWithUsername:username
                       password:password
                    displayName:displayName
                       siteName:siteName.length ? siteName : credentialHost
                           host:credentialHost
                            URL:passwordForm.url
             isBackupCredential:isBackup];
}

@end
