// Copyright 2025 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/composebox/shared/ui/composebox_snackbar_presenter.h"

#import "ios/chrome/browser/composebox/public/features.h"
#import "ios/chrome/browser/shared/model/browser/browser.h"
#import "ios/chrome/browser/shared/public/commands/command_dispatcher.h"
#import "ios/chrome/browser/shared/public/commands/snackbar_commands.h"
#import "ios/chrome/browser/shared/public/snackbar/snackbar_message.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ui/base/l10n/l10n_util_mac.h"

@implementation ComposeboxSnackbarPresenter {
  raw_ptr<Browser> _browser;
}

- (instancetype)initWithBrowser:(Browser*)browser {
  if ((self = [super init])) {
    CHECK(browser, base::NotFatalUntil::M155);
    _browser = browser;
  }
  return self;
}

- (void)stop {
  _browser = nil;
}

- (void)showSnackbarForAttachmentLimit:(NSUInteger)attachmentLimit {
  [self showSnackbarForAttachmentLimit:attachmentLimit bottomOffset:0];
}

- (void)showSnackbarForAttachmentLimit:(NSUInteger)attachmentLimit
                          bottomOffset:(CGFloat)bottomOffset {
  NSString* title = l10n_util::GetPluralNSStringF(
      IDS_IOS_COMPOSEBOX_MAXIMUM_ATTACHMENTS_REACHED, attachmentLimit);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:bottomOffset];
}

- (void)showUnableToAddAttachmentSnackbarWithBottomOffset:
    (CGFloat)bottomOffset {
  NSString* title =
      l10n_util::GetNSString(IDS_IOS_COMPOSEBOX_UNABLE_TO_ADD_ATTACHMENT);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:bottomOffset];
}

- (void)showAttachmentLimitForImageGenerationSnackbar:
            (NSUInteger)attachmentLimit
                                     withBottomOffset:(CGFloat)bottomOffset {
  NSString* title = l10n_util::GetPluralNSStringF(
      IDS_IOS_COMPOSEBOX_IMAGE_GEN_MAXIMUM_ATTACHMENTS_REACHED,
      attachmentLimit);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:bottomOffset];
}

- (void)dismissAllSnackbars {
  [self.snackbarHandler dismissAllSnackbars];
}

#pragma mark - TabPickerSnackbarPresenter

- (void)showSnackbarForTabAttachmentLimit:(NSUInteger)attachmentLimit {
  NSString* title = l10n_util::GetPluralNSStringF(
      IDS_IOS_COMPOSEBOX_MAXIMUM_TABS_REACHED, attachmentLimit);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:0];
}

- (void)showCannotReloadTabError {
  NSString* title =
      l10n_util::GetNSString(IDS_IOS_COMPOSEBOX_CANNOT_RELOAD_TAB_ERROR);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:0];
}

- (void)showCannotAttachTabError {
  NSString* title =
      l10n_util::GetNSString(IDS_IOS_COMPOSEBOX_UNABLE_TO_ADD_ATTACHMENT);
  SnackbarMessage* message = [[SnackbarMessage alloc] initWithTitle:title];

  [self.snackbarHandler showSnackbarMessage:message bottomOffset:0];
}

#pragma mark - Private

// Returns the snackbar handler if it exist.
- (id<SnackbarCommands>)snackbarHandler {
  if (!_browser) {
    return nil;
  }
  CommandDispatcher* dispatcher = _browser->GetCommandDispatcher();
  if ([dispatcher dispatchingForProtocol:@protocol(SnackbarCommands)]) {
    id<SnackbarCommands> snackbarHandler =
        HandlerForProtocol(dispatcher, SnackbarCommands);
    return snackbarHandler;
  }
  return nil;
}

@end
