// 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/content_suggestions/ui/cells/content_suggestions_tile_view.h"

#import "ios/chrome/browser/shared/ui/util/uikit_ui_util.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#import "ios/chrome/common/ui/util/dynamic_type_util.h"
#import "ios/public/provider/chrome/browser/raccoon/raccoon_api.h"

namespace {

const NSInteger kLabelNumLines = 2;
const CGFloat kSpaceIconTitle = 10;
const CGFloat kMagicStackIconSize = 52;
// Image container corner radius.
const CGFloat kCornerRadius = 8.0;

}  // namespace

@interface ContentSuggestionsTileView ()
// Hold onto the created interaction for pointer support so it can be removed
// when the view goes away.
@property(nonatomic, strong) UIPointerInteraction* pointerInteraction;
@end

@implementation ContentSuggestionsTileView {
  ContentSuggestionsTileType _type;
  NSLayoutConstraint* _imageBackgroundWidthConstraint;
}

- (instancetype)initWithFrame:(CGRect)frame
                     tileType:(ContentSuggestionsTileType)type {
  self = [super initWithFrame:frame];
  if (self) {
    _type = type;
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor colorNamed:kTextSecondaryColor];
    _titleLabel.font = [self titleLabelFont];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.numberOfLines = kLabelNumLines;
    _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;

    _imageContainerView = [[UIView alloc] init];
    _imageContainerView.translatesAutoresizingMaskIntoConstraints = NO;
    if (ios::provider::IsRaccoonEnabled()) {
      _imageContainerView.hoverStyle = [UIHoverStyle
          styleWithShape:[UIShape rectShapeWithCornerRadius:kCornerRadius]];
    }

    // Use original rounded-square background image for Shorcuts
    if (type == ContentSuggestionsTileType::kAction) {
      [self addSubview:_titleLabel];

      // The squircle background view.
      UIImageView* backgroundView =
          [[UIImageView alloc] initWithFrame:self.bounds];
      backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
      UIImage* backgroundImage = [[UIImage imageNamed:@"ntp_most_visited_tile"]
          imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
      backgroundView.image = backgroundImage;
      backgroundView.tintColor = [UIColor colorNamed:kGrey100Color];
      [self addSubview:backgroundView];
      [self addSubview:_imageContainerView];

      _imageBackgroundWidthConstraint = [backgroundView.widthAnchor
          constraintEqualToConstant:kMagicStackIconSize];
      [NSLayoutConstraint activateConstraints:@[
        _imageBackgroundWidthConstraint,
        [backgroundView.heightAnchor
            constraintEqualToAnchor:backgroundView.widthAnchor],
        [backgroundView.centerXAnchor
            constraintEqualToAnchor:_titleLabel.centerXAnchor]
      ]];

      AddSameCenterConstraints(_imageContainerView, backgroundView);
      UIView* containerView = backgroundView;

      ApplyVisualConstraintsWithMetrics(
          @[ @"V:|[container]-(space)-[title]|", @"H:|[title]|" ],
          @{@"container" : containerView, @"title" : _titleLabel},
          @{@"space" : @(kSpaceIconTitle)});

      _imageBackgroundView = backgroundView;
    }

    _pointerInteraction = [[UIPointerInteraction alloc] initWithDelegate:self];
    [self addInteraction:self.pointerInteraction];

    [self registerForTraitChanges:@[ UITraitPreferredContentSizeCategory.class ]
                       withAction:@selector(updateTitleLabelOnTraitChange)];
  }
  return self;
}

- (void)dealloc {
  [self removeInteraction:self.pointerInteraction];
  self.pointerInteraction = nil;
}

// Returns the font size for the location label.
- (UIFont*)titleLabelFont {
  return PreferredFontForTextStyleWithMaxCategory(
      UIFontTextStyleCaption1,
      self.traitCollection.preferredContentSizeCategory,
      UIContentSizeCategoryAccessibilityLarge);
}

- (void)setImageBackgroundSize:(CGFloat)size {
  if (size == _imageBackgroundWidthConstraint.constant) {
    return;
  }
  _imageBackgroundWidthConstraint.constant = size;
}

#pragma mark - UIPointerInteractionDelegate

- (UIPointerRegion*)pointerInteraction:(UIPointerInteraction*)interaction
                      regionForRequest:(UIPointerRegionRequest*)request
                         defaultRegion:(UIPointerRegion*)defaultRegion {
  return defaultRegion;
}

- (UIPointerStyle*)pointerInteraction:(UIPointerInteraction*)interaction
                       styleForRegion:(UIPointerRegion*)region {
  // The preview APIs require the view to be in a window. Ensure they are before
  // proceeding.
  if (!self.window) {
    return nil;
  }

  UITargetedPreview* preview =
      [[UITargetedPreview alloc] initWithView:_imageContainerView];
  UIPointerHighlightEffect* effect =
      [UIPointerHighlightEffect effectWithPreview:preview];
  UIPointerShape* shape =
      [UIPointerShape shapeWithRoundedRect:_imageContainerView.frame
                              cornerRadius:kCornerRadius];
  return [UIPointerStyle styleWithEffect:effect shape:shape];
}

#pragma mark - Private

// Updates the `titleLabel`'s font and numberOfLines property when a change in
// the device's UITraits is detected.
- (void)updateTitleLabelOnTraitChange {
  self.titleLabel.font = [self titleLabelFont];
}

@end
