// Copyright 2017 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/ntp_tile/ntp_tile.h"

namespace {
NSString* const kTitleKey = @"title";
NSString* const kURLKey = @"URL";
NSString* const kfaviconFileNameKey = @"faviconFileName";
NSString* const kFallbackTextColorKey = @"fallbackTextColor";
NSString* const kFallbackBackgroundColorKey = @"fallbackBackgroundColor";
NSString* const kFallbackIsDefaultColorKey = @"fallbackIsDefaultColor";
NSString* const kFallbackMonogram = @"fallbackMonogram";
NSString* const kFaviconFetched = @"faviconFetched";
NSString* const kPosition = @"position";
}  // namespace

@implementation NTPTile

@synthesize title = _title;
@synthesize URL = _URL;
@synthesize faviconFileName = _faviconFileName;
@synthesize fallbackTextColor = _fallbackTextColor;
@synthesize fallbackBackgroundColor = _fallbackBackgroundColor;
@synthesize fallbackIsDefaultColor = _fallbackIsDefaultColor;
@synthesize fallbackMonogram = _fallbackMonogram;
@synthesize position = _position;

- (instancetype)initWithTitle:(NSString*)title
                          URL:(NSURL*)URL
                     position:(NSUInteger)position {
  self = [super init];
  if (self) {
    _title = title;
    _URL = URL;
    _position = position;
  }
  return self;
}

- (instancetype)initWithTitle:(NSString*)title
                          URL:(NSURL*)URL
              faviconFileName:(NSString*)faviconFileName
            fallbackTextColor:(UIColor*)fallbackTextColor
      fallbackBackgroundColor:(UIColor*)fallbackBackgroundColor
       fallbackIsDefaultColor:(BOOL)fallbackIsDefaultColor
             fallbackMonogram:(NSString*)fallbackMonogram
                     position:(NSUInteger)position {
  self = [super init];
  if (self) {
    _title = title;
    _URL = URL;
    _faviconFileName = faviconFileName;
    _fallbackTextColor = fallbackTextColor;
    _fallbackBackgroundColor = fallbackBackgroundColor;
    _fallbackIsDefaultColor = fallbackIsDefaultColor;
    _fallbackMonogram = fallbackMonogram;
    _position = position;
  }
  return self;
}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
  return YES;
}

- (instancetype)initWithCoder:(NSCoder*)aDecoder {
  return [self initWithTitle:[aDecoder decodeObjectOfClass:[NSString class]
                                                    forKey:kTitleKey]
                          URL:[aDecoder decodeObjectOfClass:[NSURL class]
                                                     forKey:kURLKey]
              faviconFileName:[aDecoder decodeObjectOfClass:[NSString class]
                                                     forKey:kfaviconFileNameKey]
            fallbackTextColor:[aDecoder
                                  decodeObjectOfClass:[UIColor class]
                                               forKey:kFallbackTextColorKey]
      fallbackBackgroundColor:
          [aDecoder decodeObjectOfClass:[UIColor class]
                                 forKey:kFallbackBackgroundColorKey]
       fallbackIsDefaultColor:[aDecoder
                                  decodeBoolForKey:kFallbackIsDefaultColorKey]
             fallbackMonogram:[aDecoder decodeObjectOfClass:[NSString class]
                                                     forKey:kFallbackMonogram]
                     position:[[aDecoder decodeObjectOfClass:[NSNumber class]
                                                      forKey:kPosition]
                                  unsignedIntegerValue]];
}

- (void)encodeWithCoder:(NSCoder*)aCoder {
  [aCoder encodeObject:self.title forKey:kTitleKey];
  [aCoder encodeObject:self.URL forKey:kURLKey];
  [aCoder encodeObject:self.faviconFileName forKey:kfaviconFileNameKey];
  [aCoder encodeObject:self.fallbackTextColor forKey:kFallbackTextColorKey];
  [aCoder encodeObject:self.fallbackBackgroundColor
                forKey:kFallbackBackgroundColorKey];
  [aCoder encodeBool:self.fallbackIsDefaultColor
              forKey:kFallbackIsDefaultColorKey];
  [aCoder encodeObject:self.fallbackMonogram forKey:kFallbackMonogram];
  [aCoder encodeObject:[NSNumber numberWithUnsignedInteger:self.position]
                forKey:kPosition];
}

@end
