// 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/omnibox/model/suggestions/omnibox_pedal_annotator.h"

#import "base/strings/sys_string_conversions.h"
#import "components/omnibox/browser/actions/omnibox_action.h"
#import "components/omnibox/browser/actions/omnibox_pedal.h"
#import "components/omnibox/browser/actions/omnibox_pedal_concepts.h"
#import "components/omnibox/browser/autocomplete_match.h"
#import "components/password_manager/core/browser/ui/password_check_referrer.h"
#import "ios/chrome/browser/default_browser/model/promo_source.h"
#import "ios/chrome/browser/omnibox/model/suggestions/omnibox_pedal_swift.h"
#import "ios/chrome/browser/shared/model/url/chrome_url_constants.h"
#import "ios/chrome/browser/shared/public/commands/browser_coordinator_commands.h"
#import "ios/chrome/browser/shared/public/commands/omnibox_commands.h"
#import "ios/chrome/browser/shared/public/commands/open_new_tab_command.h"
#import "ios/chrome/browser/shared/public/commands/quick_delete_commands.h"
#import "ios/chrome/browser/shared/public/commands/scene_commands.h"
#import "ios/chrome/browser/shared/public/commands/settings_commands.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "ios/chrome/browser/shared/ui/symbols/colorful_background_symbol_view.h"
#import "ios/chrome/browser/shared/ui/symbols/symbols.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/image_util.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ios/components/webui/web_ui_url_constants.h"
#import "ui/base/l10n/l10n_util.h"

namespace {
const CGFloat kSymbolSize = 18;
}  // namespace

@implementation OmniboxPedalAnnotator

- (OmniboxPedalData*)pedalForMatch:(const AutocompleteMatch&)match {
  // Currently this logic takes only pedal type actions, but it could
  // be expanded to support other kinds of actions by changing the
  // predicate or iterating through `match.actions`. In that case,
  // the static_casts below should also be removed in favor of generic
  // use of the OmniboxAction base class.
  const OmniboxPedal* omniboxPedal =
      OmniboxPedal::FromAction(match.GetActionWhere([](const auto& action) {
        return action->ActionId() == OmniboxActionId::PEDAL;
      }));
  if (!omniboxPedal) {
    return nil;
  }
  __weak id<SceneCommands> sceneHandler = self.sceneHandler;
  __weak id<SettingsCommands> settingsHandler = self.settingsHandler;
  __weak id<QuickDeleteCommands> quickDeleteHandler = self.quickDeleteHandler;
  __weak id<BrowserCoordinatorCommands> browserCoordinatorHandler =
      self.browserCoordinatorHandler;

  NSString* hint =
      base::SysUTF16ToNSString(omniboxPedal->GetLabelStrings().hint);
  NSString* suggestionContents = base::SysUTF16ToNSString(
      omniboxPedal->GetLabelStrings().suggestion_contents);
  NSInteger pedalType = static_cast<NSInteger>(omniboxPedal->GetMetricsId());
  OmniboxPedalId pedalId = omniboxPedal->PedalId();

  switch (pedalId) {
    case OmniboxPedalId::PLAY_CHROME_DINO_GAME: {
      UIImage* image = SymbolWithPointSize(SymbolDino, kSymbolSize);
      NSString* urlStr = [NSString
          stringWithFormat:@"%s://%s", kChromeUIScheme, kChromeUIDinoHost];
      GURL url(base::SysNSStringToUTF8(urlStr));
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:urlStr
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:UIColor.blackColor
            backgroundColor:UIColor.whiteColor
           imageBorderColor:[UIColor colorNamed:kLightOnlyGrey200Color]
                       type:pedalType
                     action:^{
                       OpenNewTabCommand* command =
                           [OpenNewTabCommand commandWithURLFromChrome:url
                                                           inIncognito:NO];
                       [sceneHandler openURLInNewTab:command];
                     }];
    }
    case OmniboxPedalId::CLEAR_BROWSING_DATA: {
      UIImage* image = SymbolTemplateWithPointSize(SymbolTrash, kSymbolSize);
      auto completion = ^{
        [quickDeleteHandler
            showQuickDeleteAndCanPerformRadialWipeAnimation:YES];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:
                       l10n_util::GetNSString(
                           IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_CLEAR_BROWSING_DATA)
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kBlue500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::SET_CHROME_AS_DEFAULT_BROWSER: {
#if BUILDFLAG(IOS_USE_BRANDED_ASSETS)
      UIImage* image = MakeSymbolMulticolor(
          SymbolWithPointSize(SymbolMulticolorChromeball, kSymbolSize));
#else
      UIImage* image =
          SymbolTemplateWithPointSize(SymbolDefaultBrowser, kSymbolSize);
#endif  // BUILDFLAG(IOS_USE_BRANDED_ASSETS)
      DefaultBrowserSettingsPageSource source =
          DefaultBrowserSettingsPageSource::kOmnibox;
      auto completion = ^{
        [settingsHandler showDefaultBrowserSettingsFromViewController:nil
                                                         sourceForUMA:source];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:l10n_util::GetNSString(
                                IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_DEFAULT_BROWSER)
          accessibilityHint:suggestionContents
                      image:image
#if BUILDFLAG(IOS_USE_BRANDED_ASSETS)
             imageTintColor:nil
            backgroundColor:UIColor.whiteColor
           imageBorderColor:[UIColor colorNamed:kLightOnlyGrey200Color]
#else
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kPurple500Color]
           imageBorderColor:nil
#endif  // BUILDFLAG(IOS_USE_BRANDED_ASSETS)
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::MANAGE_PASSWORDS: {
      UIImage* image = SymbolWithPointSize(SymbolPassword, kSymbolSize);
      auto completion = ^{
        [settingsHandler showSavedPasswordsSettingsFromViewController:nil];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:l10n_util::GetNSString(
                                IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_MANAGE_PASSWORDS)
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kYellow500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::UPDATE_CREDIT_CARD: {
      UIImage* image =
          SymbolTemplateWithPointSize(SymbolCreditCard, kSymbolSize);
      auto completion = ^{
        [settingsHandler showCreditCardSettings];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:
                       l10n_util::GetNSString(
                           IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_UPDATE_CREDIT_CARD)
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kYellow500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::LAUNCH_INCOGNITO: {
      UIImage* image = SymbolWithPointSize(SymbolIncognito, kSymbolSize);
      auto completion = ^{
        [sceneHandler openURLInNewTab:[OpenNewTabCommand incognitoTabCommand]];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:l10n_util::GetNSString(
                                IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_LAUNCH_INCOGNITO)
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kGrey800Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::RUN_CHROME_SAFETY_CHECK: {
      UIImage* image = SymbolWithPointSize(SymbolSafetyCheck, kSymbolSize);
      NSString* subtitle = l10n_util::GetNSString(
          IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_RUN_CHROME_SAFETY_CHECK);
      auto completion = ^{
        [settingsHandler
            showAndStartSafetyCheckForReferrer:
                password_manager::PasswordCheckReferrer::kSafetyCheck];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:subtitle
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kBlue500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::MANAGE_CHROME_SETTINGS: {
      UIImage* image = SymbolTemplateWithPointSize(SymbolSettings, kSymbolSize);
      NSString* subtitle = l10n_util::GetNSString(
          IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_MANAGE_CHROME_SETTINGS);
      auto completion = ^{
        [sceneHandler showSettingsFromViewController:nil];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:subtitle
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kGrey500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
    case OmniboxPedalId::VIEW_CHROME_HISTORY: {
      UIImage* image = SymbolTemplateWithPointSize(SymbolHistory, kSymbolSize);
      auto completion = ^{
        [sceneHandler showHistory];
      };
      auto action = ^{
        [browserCoordinatorHandler hideComposeboxWithCompletion:completion];
      };
      return [[OmniboxPedalData alloc]
              initWithTitle:hint
                   subtitle:
                       l10n_util::GetNSString(
                           IDS_IOS_OMNIBOX_PEDAL_SUBTITLE_VIEW_CHROME_HISTORY)
          accessibilityHint:suggestionContents
                      image:image
             imageTintColor:nil
            backgroundColor:[UIColor colorNamed:kBlue500Color]
           imageBorderColor:nil
                       type:pedalType
                     action:action];
    }
      // If a new case is added here, make sure to update the method returning
      // the icon.
    default:
      return nil;
  }
}

@end
