chromium/ios/chrome/browser/tips_notifications/coordinator/lens_promo_coordinator.mm

// Copyright 2024 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/tips_notifications/coordinator/lens_promo_coordinator.h"

#import "ios/chrome/browser/shared/model/browser/browser.h"
#import "ios/chrome/browser/shared/public/commands/browser_coordinator_commands.h"
#import "ios/chrome/browser/shared/public/commands/command_dispatcher.h"
#import "ios/chrome/browser/tips_notifications/ui/lens_promo_instructions_view_controller.h"
#import "ios/chrome/browser/tips_notifications/ui/lens_promo_view_controller.h"
#import "ios/chrome/common/ui/confirmation_alert/confirmation_alert_action_handler.h"

@interface LensPromoCoordinator () <ConfirmationAlertActionHandler,
                                    PromoStyleViewControllerDelegate,
                                    UIAdaptivePresentationControllerDelegate>
@end

@implementation LensPromoCoordinator {
  LensPromoViewController* _viewController;
  LensPromoInstructionsViewController* _instructionsViewController;
}

#pragma mark - ChromeCoordinator

- (void)start {
  _viewController = [[LensPromoViewController alloc] init];
  _viewController.delegate = self;

  UINavigationController* navigationController = [[UINavigationController alloc]
      initWithRootViewController:_viewController];
  navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
  [self.baseViewController presentViewController:navigationController
                                        animated:YES
                                      completion:nil];
  navigationController.presentationController.delegate = self;
}

- (void)stop {
  _instructionsViewController.actionHandler = nil;
  _instructionsViewController = nil;
  [_viewController.presentingViewController dismissViewControllerAnimated:YES
                                                               completion:nil];
  _viewController = nil;
}

#pragma mark - PromoStyleViewControllerDelegate

- (void)didTapPrimaryActionButton {
  [self goToLens];
  [self dismissScreen];
}

- (void)didTapSecondaryActionButton {
  _instructionsViewController =
      [[LensPromoInstructionsViewController alloc] init];
  _instructionsViewController.actionHandler = self;
  _instructionsViewController.presentationController.delegate = self;
  [_viewController presentViewController:_instructionsViewController
                                animated:YES
                              completion:nil];
}

- (void)didDismissViewController {
  [self dismissScreen];
}

#pragma mark - ConfirmationAlertPrimaryAction

- (void)confirmationAlertPrimaryAction {
  [self goToLens];
  [self dismissScreen];
}

- (void)confirmationAlertDismissAction {
  [_instructionsViewController.presentingViewController
      dismissViewControllerAnimated:YES
                         completion:nil];
  _instructionsViewController = nil;
}

#pragma mark - UIAdaptivePresentationControllerDelegate

- (void)presentationControllerDidDismiss:
    (UIPresentationController*)presentationController {
  if (presentationController.presentedViewController ==
      _instructionsViewController) {
    _instructionsViewController = nil;
  } else {
    // The UINavigationController was dismissed.
    [self dismissScreen];
  }
}
#pragma mark - Private methods

// Sends a command that will stop this coordinator and dismiss the screen.
- (void)dismissScreen {
  id<BrowserCoordinatorCommands> handler = HandlerForProtocol(
      self.browser->GetCommandDispatcher(), BrowserCoordinatorCommands);
  [handler dismissLensPromo];
}

// Opens the NTP and displays an IPH bubble to call attention to the Lens icon.
- (void)goToLens {
  // TODO(crbug.com/362981235): Go to the NTP and display IPH bubble for Lens.
}

@end