// Copyright 2021 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/credential_provider_extension/ui/ui_util.h"

#import <AuthenticationServices/AuthenticationServices.h>

#import "ios/chrome/common/credential_provider/net_util.h"
#import "ios/chrome/credential_provider_extension/generated_localized_strings.h"

namespace {

// System SF symbol names for the hide and show actions.
NSString* const kHideActionSymbol = @"eye.slash";
NSString* const kShowActionSymbol = @"eye";

// Size of the action icons.
const CGFloat kSymbolActionPointSize = 15;

// System SF symbol name for the info button icon.
NSString* const kInfoCircleSymbol = @"info.circle";

// Size of the info button icon.
const CGFloat kInfoSymbolPointSize = 20;

// System SF symbol name for the error icon.
NSString* const kErrorCircleFillSymbol = @"exclamationmark.circle.fill";

// Size of the error icon.
const CGFloat kErrorSymbolPointSize = 20;

UIImage* DefaultSymbolWithPointSize(NSString* symbol_name, CGFloat point_size) {
  UIImageConfiguration* configuration = [UIImageSymbolConfiguration
      configurationWithPointSize:point_size
                          weight:UIImageSymbolWeightMedium
                           scale:UIImageSymbolScaleMedium];
  return [[UIImage systemImageNamed:symbol_name withConfiguration:configuration]
      imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

}  // namespace

NSString* PromptForServiceIdentifiers(
    NSArray<ASCredentialServiceIdentifier*>* serviceIdentifiers) {
  NSString* IDForPrompt = credential_provider::HostForIdentifier(
      serviceIdentifiers.firstObject.identifier);
  if (!IDForPrompt) {
    return nil;
  }
  NSString* baseLocalizedString = CredentialProviderTitlePromptString();
  return [baseLocalizedString stringByReplacingOccurrencesOfString:@"$1"
                                                        withString:IDForPrompt];
}

UIImage* GetPasswordVisibilityIcon(bool is_visible) {
  NSString* symbol_name = is_visible ? kHideActionSymbol : kShowActionSymbol;
  return DefaultSymbolWithPointSize(symbol_name, kSymbolActionPointSize);
}

UIImage* GetCredentialInfoIcon() {
  return DefaultSymbolWithPointSize(kInfoCircleSymbol, kInfoSymbolPointSize);
}

UIImage* GetNoteErrorIcon() {
  return DefaultSymbolWithPointSize(kErrorCircleFillSymbol,
                                    kErrorSymbolPointSize);
}
