// Copyright 2018 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/sharing/ui_bundled/activity_services/activities/copy_activity.h"

#import "base/functional/bind.h"
#import "base/functional/callback_helpers.h"
#import "ios/chrome/browser/shared/ui/symbols/symbols.h"
#import "ios/chrome/browser/shared/ui/util/pasteboard_util.h"
#import "ios/chrome/browser/sharing/ui_bundled/activity_services/data/share_to_data.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ui/base/l10n/l10n_util_mac.h"
#import "url/gurl.h"

namespace {

NSString* const kCopyActivityType = @"com.google.chrome.copyActivity";

}  // namespace

@interface CopyActivity ()

@property(nonatomic, strong) NSArray<ShareToData*>* dataItems;

@end

@implementation CopyActivity

#pragma mark - Public

- (instancetype)initWithDataItems:(NSArray<ShareToData*>*)dataItems {
  DCHECK(dataItems);
  DCHECK(dataItems.count);
  self = [super init];
  if (self) {
    _dataItems = dataItems;
  }
  return self;
}

#pragma mark - UIActivity

- (NSString*)activityType {
  return kCopyActivityType;
}

- (NSString*)activityTitle {
  return l10n_util::GetNSString(IDS_IOS_SHARE_MENU_COPY);
}

- (UIImage*)activityImage {
  return SymbolWithPointSize(SymbolCopyAction, kSymbolActionPointSize);
}

- (BOOL)canPerformWithActivityItems:(NSArray*)activityItems {
  return !!self.dataItems && self.dataItems.count;
}

- (void)prepareWithActivityItems:(NSArray*)activityItems {
}

+ (UIActivityCategory)activityCategory {
  return UIActivityCategoryAction;
}

- (void)performActivity {
  __weak __typeof(self) weakSelf = self;
  if (self.dataItems.count == 1 && self.dataItems.firstObject.additionalText) {
    StoreInPasteboard(self.dataItems.firstObject.additionalText,
                      self.dataItems.firstObject.shareURL, base::BindOnce(^{
                        [weakSelf activityDidFinish:YES];
                      }));
  } else {
    std::vector<GURL> urls;
    for (ShareToData* shareToData in self.dataItems) {
      urls.push_back(shareToData.shareURL);
    }
    StoreURLsInPasteboard(urls, base::BindOnce(^{
                            [weakSelf activityDidFinish:YES];
                          }));
  }
}

@end
