// Copyright 2021 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/common/ui/favicon/favicon_container_view.h"

#import <UIKit/UIKit.h>

#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/favicon/favicon_view.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"

namespace {
// The width and height of the favicon ImageView.
const CGFloat kFaviconWidth = 16;
// Corner radius of the favicon ImageView.
const CGFloat kFaviconCornerRadius = 7.0;
// The width and height of the favicon container view.
const CGFloat kFaviconContainerWidth = 30;
}  // namespace

@interface FaviconContainerView ()

// Store custom background color.
@property(nonatomic, strong) UIColor* customBackgroundColor;

@end

@implementation FaviconContainerView

- (instancetype)init {
  self = [super initWithFrame:CGRectZero];
  if (self) {
    [self.traitCollection performAsCurrentTraitCollection:^{
      [self resetColor];
    }];
    self.layer.cornerRadius = kFaviconCornerRadius;
    self.layer.masksToBounds = YES;

    _faviconView = [[FaviconView alloc] init];
    _faviconView.contentMode = UIViewContentModeScaleAspectFit;
    _faviconView.clipsToBounds = YES;
    [self addSubview:_faviconView];
    _faviconView.translatesAutoresizingMaskIntoConstraints = NO;

    [NSLayoutConstraint activateConstraints:@[
      // The favicon view is a fixed size, is pinned to the leading edge of the
      // content view, and is centered vertically.
      [_faviconView.heightAnchor constraintEqualToConstant:kFaviconWidth],
      [_faviconView.widthAnchor constraintEqualToConstant:kFaviconWidth],
      [_faviconView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
      [_faviconView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],

      [self.heightAnchor constraintEqualToConstant:kFaviconContainerWidth],
      [self.widthAnchor constraintEqualToAnchor:self.heightAnchor],
    ]];

    NSArray<UITrait>* traits = @[
      UITraitUserInterfaceIdiom.class, UITraitUserInterfaceStyle.class,
      UITraitDisplayGamut.class, UITraitAccessibilityContrast.class,
      UITraitUserInterfaceLevel.class
    ];

    __weak __typeof(self) weakSelf = self;
    UITraitChangeHandler handler = ^(id<UITraitEnvironment> traitEnvironment,
                                     UITraitCollection* previousCollection) {
      [weakSelf updateColorOnTraitChange:previousCollection];
    };
    [self registerForTraitChanges:traits withHandler:handler];
  }
  return self;
}

- (void)setFaviconBackgroundColor:(UIColor*)color {
  self.customBackgroundColor = color;
  if (color) {
    self.backgroundColor = color;
  } else {
    [self resetColor];
  }
}

- (void)resetColor {
  if (self.customBackgroundColor) {
    self.backgroundColor = self.customBackgroundColor;
  } else {
    self.backgroundColor =
        self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark
            ? [UIColor colorNamed:kPrimaryBackgroundColor]
            : [UIColor colorNamed:kSecondaryBackgroundColor];
  }
}

#pragma mark - Private

- (void)updateColorOnTraitChange:(UITraitCollection*)previousTraitCollection {
  if ([self.traitCollection
          hasDifferentColorAppearanceComparedToTraitCollection:
              previousTraitCollection]) {
    [self resetColor];
  }
}

@end
