// Copyright 2016 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/omnibox/public/omnibox_util.h"

#import "base/notreached.h"
#import "base/strings/utf_string_conversions.h"
#import "ios/chrome/browser/omnibox/public/omnibox_ui_features.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "ios/chrome/browser/shared/ui/symbols/symbols.h"
#import "ios/chrome/grit/ios_theme_resources.h"

namespace {

// The size of symbol images.
const CGFloat kSymbolLocationBarPointSize = 10;

}  // namespace

#pragma mark - Suggestion icons.

OmniboxSuggestionIconType GetOmniboxSuggestionIconTypeForAutocompleteMatchType(
    AutocompleteMatchType::Type type) {
  switch (type) {
    case AutocompleteMatchType::BOOKMARK_TITLE:
    case AutocompleteMatchType::CLIPBOARD_URL:
    case AutocompleteMatchType::DOCUMENT_SUGGESTION:
    case AutocompleteMatchType::HISTORY_BODY:
    case AutocompleteMatchType::HISTORY_CLUSTER:
    case AutocompleteMatchType::HISTORY_KEYWORD:
    case AutocompleteMatchType::HISTORY_TITLE:
    case AutocompleteMatchType::HISTORY_URL:
    case AutocompleteMatchType::NAVSUGGEST:
    case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
    case AutocompleteMatchType::OPEN_TAB:
    case AutocompleteMatchType::PEDAL:
    case AutocompleteMatchType::PHYSICAL_WEB_DEPRECATED:
    case AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW_DEPRECATED:
    case AutocompleteMatchType::STARTER_PACK:
    case AutocompleteMatchType::TAB_SEARCH_DEPRECATED:
    case AutocompleteMatchType::TILE_NAVSUGGEST:
    case AutocompleteMatchType::TILE_MOST_VISITED_SITE:
    case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
      return OmniboxSuggestionIconType::kDefaultFavicon;
    case AutocompleteMatchType::CLIPBOARD_IMAGE:
    case AutocompleteMatchType::CLIPBOARD_TEXT:
    case AutocompleteMatchType::CONTACT_DEPRECATED:
    case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
    case AutocompleteMatchType::SEARCH_SUGGEST:
    case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
    case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
    case AutocompleteMatchType::SEARCH_SUGGEST_TAIL:
    case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
    case AutocompleteMatchType::VOICE_SUGGEST:
      return OmniboxSuggestionIconType::kSearch;
    case AutocompleteMatchType::SEARCH_HISTORY:
    case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
      return OmniboxSuggestionIconType::kSearchHistory;
    case AutocompleteMatchType::CALCULATOR:
      return OmniboxSuggestionIconType::kCalculator;
    case AutocompleteMatchType::EXTENSION_APP_DEPRECATED:
    case AutocompleteMatchType::NULL_RESULT_MESSAGE:
    case AutocompleteMatchType::NUM_TYPES:
    case AutocompleteMatchType::TILE_SUGGESTION:
    case AutocompleteMatchType::TILE_REPEATABLE_QUERY:
    case AutocompleteMatchType::HISTORY_EMBEDDINGS:
    case AutocompleteMatchType::FEATURED_ENTERPRISE_SEARCH:
    case AutocompleteMatchType::HISTORY_EMBEDDINGS_ANSWER:
    default:
      DUMP_WILL_BE_NOTREACHED()
          << "Unsupported AutocompleteMatchType: " << type;
      return OmniboxSuggestionIconType::kDefaultFavicon;
  }
}

UIImage* GetOmniboxSuggestionIconForAutocompleteMatchType(
    AutocompleteMatchType::Type type) {
  OmniboxSuggestionIconType iconType =
      GetOmniboxSuggestionIconTypeForAutocompleteMatchType(type);
  return GetOmniboxSuggestionIcon(iconType);
}

OmniboxSuggestionIconType
GetOmniboxSuggestionIconTypeForSuggestTemplateInfoIconType(
    omnibox::SuggestTemplateInfo::IconType type) {
  // Update this assertion and the switch below whenever values are added.
  static_assert(omnibox::SuggestTemplateInfo::IconType_MAX ==
                omnibox::SuggestTemplateInfo::BOLT);
  switch (type) {
    case omnibox::SuggestTemplateInfo_IconType_HISTORY:
      return OmniboxSuggestionIconType::kSearchHistory;
    case omnibox::SuggestTemplateInfo_IconType_SEARCH_LOOP:
      return OmniboxSuggestionIconType::kSearch;
    case omnibox::SuggestTemplateInfo_IconType_SEARCH_LOOP_WITH_SPARKLE:
      return OmniboxSuggestionIconType::kSearchWithSparkle;
    case omnibox::SuggestTemplateInfo_IconType_TRENDING:
      return OmniboxSuggestionIconType::kSearchTrend;
    case omnibox::SuggestTemplateInfo_IconType_SUB_ARROW_RIGHT:
      // TODO(crbug.com/437177158): Replace with the correct symbol when it's
      // available.
      return OmniboxSuggestionIconType::kSearch;
    case omnibox::SuggestTemplateInfo_IconType_GLOBE_WITH_SEARCH_LOOP:
    case omnibox::SuggestTemplateInfo_IconType_BANANA:
      return OmniboxSuggestionIconType::kSearch;
    case omnibox::SuggestTemplateInfo_IconType_NOTES_SPARK:
      return OmniboxSuggestionIconType::kNotesSpark;
    case omnibox::SuggestTemplateInfo_IconType_DRAFT_SPARK:
    case omnibox::SuggestTemplateInfo_IconType_LIGHTBULB:
    case omnibox::SuggestTemplateInfo_IconType_ATTACH_FILE:
    case omnibox::SuggestTemplateInfo_IconType_SCHOOL:
    case omnibox::SuggestTemplateInfo_IconType_INK_PEN:
    case omnibox::SuggestTemplateInfo_IconType_TAB:
    case omnibox::SuggestTemplateInfo_IconType_PHOTO_SPARK:
    case omnibox::SuggestTemplateInfo_IconType_BOLT:
      // TODO(crbug.com/486698515): Replace with the correct symbol when it's
      // available.
      return OmniboxSuggestionIconType::kSearch;
    case omnibox::SuggestTemplateInfo_IconType_FAVICON:
      return OmniboxSuggestionIconType::kDefaultFavicon;
    case omnibox::SuggestTemplateInfo_IconType_ICON_TYPE_UNSPECIFIED:
      return OmniboxSuggestionIconType::kSearch;
    default:
      return OmniboxSuggestionIconType::kSearch;
  }
}

UIImage* GetOmniboxSuggestionIconForSuggestTemplateInfoIconType(
    omnibox::SuggestTemplateInfo::IconType type) {
  OmniboxSuggestionIconType iconType =
      GetOmniboxSuggestionIconTypeForSuggestTemplateInfoIconType(type);
  return GetOmniboxSuggestionIcon(iconType);
}

#pragma mark - Security icons.

// Returns the asset with "always template" rendering mode.
UIImage* GetLocationBarSecurityIcon(LocationBarSecurityIconType iconType) {
  Symbol symbol = GetLocationBarSecuritySymbol(iconType);
  if (symbol == SymbolNone) {
    return nil;
  }
  return SymbolTemplateWithPointSize(symbol, kSymbolLocationBarPointSize);
}

// Converts the `security_level` to an appropriate security icon type.
LocationBarSecurityIconType GetLocationBarSecurityIconTypeForSecurityState(
    security_state::SecurityLevel security_level) {
  switch (security_level) {
    case security_state::NONE:
      return LocationBarSecurityIconType::INFO;
    case security_state::DANGEROUS:
      return LocationBarSecurityIconType::DANGEROUS;
    case security_state::WARNING:
      return LocationBarSecurityIconType::NOT_SECURE_WARNING;
    case security_state::SECURE:
      return LocationBarSecurityIconType::NONE;
    case security_state::SECURITY_LEVEL_COUNT:
      NOTREACHED();
  }
}

// Converts the `security_level` to an appropriate icon in "always template"
// rendering mode.
UIImage* GetLocationBarSecurityIconForSecurityState(
    security_state::SecurityLevel security_level) {
  LocationBarSecurityIconType iconType =
      GetLocationBarSecurityIconTypeForSecurityState(security_level);
  return GetLocationBarSecurityIcon(iconType);
}

UIImage* GetLocationBarOfflineIcon() {
  return SymbolTemplateWithPointSize(SymbolDownloadPromptFill,
                                     kSymbolLocationBarPointSize);
}
