// Copyright 2023 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/ui/keyboard_assist/omnibox_assistive_keyboard_mediator.h"

#import "base/apple/foundation_util.h"
#import "base/metrics/histogram_functions.h"
#import "base/metrics/user_metrics.h"
#import "base/metrics/user_metrics_action.h"
#import "ios/chrome/browser/lens/ui_bundled/lens_entrypoint.h"
#import "ios/chrome/browser/location_bar/ui_bundled/location_bar_constants.h"
#import "ios/chrome/browser/omnibox/ui/keyboard_assist/omnibox_assistive_keyboard_utils.h"
#import "ios/chrome/browser/omnibox/ui/omnibox_text_input.h"
#import "ios/chrome/browser/shared/public/commands/browser_coordinator_commands.h"
#import "ios/chrome/browser/shared/public/commands/lens_commands.h"
#import "ios/chrome/browser/shared/public/commands/open_lens_input_selection_command.h"
#import "ios/chrome/browser/shared/public/commands/qr_scanner_commands.h"
#import "ios/chrome/browser/shared/public/commands/scene_commands.h"
#import "ios/chrome/browser/shared/public/features/system_flags.h"
#import "ios/chrome/browser/shared/ui/util/layout_guide_names.h"
#import "ios/chrome/browser/shared/ui/util/util_swift.h"
#import "ios/public/provider/chrome/browser/voice_search/voice_search_api.h"

@implementation OmniboxAssistiveKeyboardMediator

@synthesize layoutGuideCenter = _layoutGuideCenter;
@synthesize lensButton = _lensButton;

#pragma mark - Public

- (void)keyboardAccessoryVoiceSearchTapped:(id)sender {
  if (ios::provider::IsVoiceSearchEnabled()) {
    [self.browserCoordinatorCommandsHandler preloadVoiceSearch];
    base::RecordAction(base::UserMetricsAction("MobileCustomRowVoiceSearch"));
    // Voice Search will query kVoiceSearchButtonGuide to know from where to
    // start its animation, so reference the sender under that name. The sender
    // can be a regular view or a bar button item. Handle both cases.
    UIView* view;
    if ([sender isKindOfClass:[UIView class]]) {
      view = base::apple::ObjCCastStrict<UIView>(sender);
    } else if ([sender isKindOfClass:[UIBarButtonItem class]]) {
      view = [sender valueForKey:@"view"];
    }
    DCHECK(view);
    [self.layoutGuideCenter referenceView:view
                                underName:kVoiceSearchButtonGuide];
    [self.browserCoordinatorCommandsHandler startVoiceSearch];
  }
}

- (void)keyboardAccessoryCameraSearchTapped {
  base::RecordAction(base::UserMetricsAction("MobileCustomRowCameraSearch"));
  [self.qrScannerCommandsHandler showQRScanner];
}

- (void)keyboardAccessoryLensTapped {
  base::RecordAction(base::UserMetricsAction("MobileCustomRowLensSearch"));
  OpenLensInputSelectionCommand* command = [[OpenLensInputSelectionCommand
      alloc]
          initWithEntryPoint:LensEntrypoint::Keyboard
           presentationStyle:LensInputSelectionPresentationStyle::SlideFromRight
      presentationCompletion:nil];
  [self.lensCommandsHandler openLensInputSelection:command];
}

- (void)keyboardAccessoryDebuggerTapped {
  CHECK(experimental_flags::IsOmniboxDebuggingEnabled());
  [self.delegate omniboxAssistiveKeyboardDidTapDebuggerButton];
}

- (void)keyPressed:(NSString*)title {
  // Event can happen when the text input is not editing (crbug.com/349002705).
  if (!self.omniboxTextInput.isEditing) {
    return;
  }

  base::UmaHistogramEnumeration("IOS.Omnibox.AssistiveKeyboard",
                                AssistiveKeyStringToEnumValue(title));

  NSString* text = [self updateTextForDotCom:title];
  [self.omniboxTextInput insertTextWhileEditing:text];
}

- (void)presentLensKeyboardInProductHelper {
  [self.delegate presentLensKeyboardInProductHelper];
}

#pragma mark - Private

/// Returns 'com' without the period if cursor is directly after a period.
- (NSString*)updateTextForDotCom:(NSString*)text {
  if ([text isEqualToString:kDotComTLD]) {
    UITextRange* textRange = [self.omniboxTextInput selectedTextRange];
    NSInteger pos = [self.omniboxTextInput
        offsetFromPosition:[self.omniboxTextInput beginningOfDocument]
                toPosition:textRange.start];
    if (pos > 0 &&
        [[self.omniboxTextInput text] characterAtIndex:pos - 1] == '.') {
      return [kDotComTLD substringFromIndex:1];
    }
  }
  return text;
}

@end
