#import "NativeBaseViewController.h"

@implementation NativeBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (id)init {
    self = [super initWithNibName:@"NativeBaseViewController" bundle:nil];
    return self;
}

- (void)updateCallToActionButtonTitle:(NSString *)title {
    NSString *displayTitle = [NativeBaseViewController displayCallToAction:title];
    UIButtonConfiguration *config = self.callToActionButton.configuration;
    if (config == nil) {
        [self.callToActionButton setTitle:displayTitle forState:UIControlStateNormal];
        return;
    }
    config.title = displayTitle;
    self.callToActionButton.configuration = config;
}

- (void)applyFeedNativeLayout {
    [NativeBaseViewController applyFeedLayoutToContainer:self.view
                                                iconView:self.iconView
                                              titleLabel:self.titleLable
                                               bodyLabel:self.bodyLabel
                                          sponsoredLabel:self.sponsoredLabel
                                           mainImageView:self.mainImageView
                                        callToActionButton:self.callToActionButton
                                         useSafeAreaInsets:YES];
}

+ (NSString *)displayCallToAction:(NSString *)callToAction {
    NSString *trimmed = [[(callToAction ?: @"") stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] copy];
    if (trimmed.length == 0) {
        return @"下载";
    }

    NSString *normalized = trimmed.lowercaseString;
    if ([normalized isEqualToString:@"install"] ||
        [normalized isEqualToString:@"download"] ||
        [normalized isEqualToString:@"get"] ||
        [normalized isEqualToString:@"get it"] ||
        [normalized isEqualToString:@"get the app"]) {
        return @"下载";
    }

    return trimmed;
}

+ (void)applyFeedLayoutToContainer:(UIView *)container
                          iconView:(UIImageView *)iconView
                        titleLabel:(UILabel *)titleLabel
                         bodyLabel:(UILabel *)bodyLabel
                    sponsoredLabel:(UILabel *)sponsoredLabel
                     mainImageView:(UIView *)mainImageView
                  callToActionButton:(UIButton *)callToActionButton
                   useSafeAreaInsets:(BOOL)useSafeAreaInsets {
    UIStackView *containerStack = nil;
    for (UIView *subview in container.subviews) {
        if ([subview isKindOfClass:[UIStackView class]]) {
            containerStack = (UIStackView *)subview;
            break;
        }
    }

    if (containerStack != nil) {
        [iconView removeFromSuperview];
        [titleLabel removeFromSuperview];
        [bodyLabel removeFromSuperview];
        [mainImageView removeFromSuperview];
        [callToActionButton removeFromSuperview];
        [sponsoredLabel removeFromSuperview];
        [containerStack removeFromSuperview];
    } else {
        for (UIView *subview in [container.subviews copy]) {
            if (subview != iconView &&
                subview != titleLabel &&
                subview != bodyLabel &&
                subview != mainImageView &&
                subview != callToActionButton &&
                subview != sponsoredLabel) {
                [subview removeFromSuperview];
            }
        }
    }

    UIView *cardView = [[UIView alloc] init];
    cardView.translatesAutoresizingMaskIntoConstraints = NO;
    cardView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    cardView.layer.cornerRadius = 4;
    [container addSubview:cardView];

    iconView.translatesAutoresizingMaskIntoConstraints = NO;
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    iconView.clipsToBounds = YES;
    iconView.layer.cornerRadius = 8;

    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.numberOfLines = 2;

    bodyLabel.translatesAutoresizingMaskIntoConstraints = NO;
    bodyLabel.font = [UIFont systemFontOfSize:13];
    bodyLabel.textColor = UIColor.secondaryLabelColor;
    bodyLabel.numberOfLines = 2;

    sponsoredLabel.translatesAutoresizingMaskIntoConstraints = NO;
    sponsoredLabel.font = [UIFont systemFontOfSize:12];
    sponsoredLabel.textColor = UIColor.secondaryLabelColor;
    sponsoredLabel.numberOfLines = 1;

    mainImageView.translatesAutoresizingMaskIntoConstraints = NO;
    if ([mainImageView isKindOfClass:[UIImageView class]]) {
        UIImageView *imageView = (UIImageView *)mainImageView;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
    }

    callToActionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [callToActionButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [callToActionButton setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    UIButtonConfiguration *buttonConfig = [UIButtonConfiguration filledButtonConfiguration];
    buttonConfig.baseBackgroundColor = UIColor.systemBlueColor;
    buttonConfig.baseForegroundColor = UIColor.whiteColor;
    buttonConfig.cornerStyle = UIButtonConfigurationCornerStyleMedium;
    buttonConfig.contentInsets = NSDirectionalEdgeInsetsMake(12, 16, 12, 16);
    callToActionButton.configuration = buttonConfig;

    [iconView removeConstraints:iconView.constraints];
    [titleLabel removeConstraints:titleLabel.constraints];
    [bodyLabel removeConstraints:bodyLabel.constraints];
    [sponsoredLabel removeConstraints:sponsoredLabel.constraints];
    [mainImageView removeConstraints:mainImageView.constraints];
    [callToActionButton removeConstraints:callToActionButton.constraints];

    [cardView addSubview:iconView];
    [cardView addSubview:titleLabel];
    [cardView addSubview:bodyLabel];
    [cardView addSubview:sponsoredLabel];
    [cardView addSubview:mainImageView];
    [cardView addSubview:callToActionButton];

    NSLayoutAnchor *topAnchor = useSafeAreaInsets ? container.safeAreaLayoutGuide.topAnchor : container.topAnchor;
    NSLayoutAnchor *leadingAnchor = useSafeAreaInsets ? container.safeAreaLayoutGuide.leadingAnchor : container.leadingAnchor;
    NSLayoutAnchor *trailingAnchor = useSafeAreaInsets ? container.safeAreaLayoutGuide.trailingAnchor : container.trailingAnchor;
    CGFloat edgeInset = useSafeAreaInsets ? 15 : 0;

    NSMutableArray<NSLayoutConstraint *> *constraints = [NSMutableArray arrayWithArray:@[
        [cardView.topAnchor constraintEqualToAnchor:topAnchor constant:edgeInset],
        [cardView.leadingAnchor constraintEqualToAnchor:leadingAnchor constant:edgeInset],
        [cardView.trailingAnchor constraintEqualToAnchor:trailingAnchor constant:-edgeInset],

        [iconView.topAnchor constraintEqualToAnchor:cardView.topAnchor constant:12],
        [iconView.leadingAnchor constraintEqualToAnchor:cardView.leadingAnchor constant:12],
        [iconView.widthAnchor constraintEqualToConstant:48],
        [iconView.heightAnchor constraintEqualToConstant:48],

        [titleLabel.topAnchor constraintEqualToAnchor:iconView.topAnchor],
        [titleLabel.leadingAnchor constraintEqualToAnchor:iconView.trailingAnchor constant:12],
        [titleLabel.trailingAnchor constraintEqualToAnchor:cardView.trailingAnchor constant:-12],

        [bodyLabel.topAnchor constraintEqualToAnchor:titleLabel.bottomAnchor constant:4],
        [bodyLabel.leadingAnchor constraintEqualToAnchor:titleLabel.leadingAnchor],
        [bodyLabel.trailingAnchor constraintEqualToAnchor:titleLabel.trailingAnchor],

        [sponsoredLabel.topAnchor constraintEqualToAnchor:bodyLabel.bottomAnchor constant:4],
        [sponsoredLabel.leadingAnchor constraintEqualToAnchor:titleLabel.leadingAnchor],
        [sponsoredLabel.trailingAnchor constraintEqualToAnchor:titleLabel.trailingAnchor],
        [sponsoredLabel.bottomAnchor constraintGreaterThanOrEqualToAnchor:iconView.bottomAnchor],

        [mainImageView.topAnchor constraintEqualToAnchor:sponsoredLabel.bottomAnchor constant:12],
        [mainImageView.leadingAnchor constraintEqualToAnchor:cardView.leadingAnchor],
        [mainImageView.trailingAnchor constraintEqualToAnchor:cardView.trailingAnchor],
        [mainImageView.heightAnchor constraintEqualToConstant:180],

        [callToActionButton.topAnchor constraintEqualToAnchor:mainImageView.bottomAnchor constant:12],
        [callToActionButton.leadingAnchor constraintEqualToAnchor:cardView.leadingAnchor constant:12],
        [callToActionButton.trailingAnchor constraintEqualToAnchor:cardView.trailingAnchor constant:-12],
        [callToActionButton.heightAnchor constraintEqualToConstant:48],
        [callToActionButton.bottomAnchor constraintEqualToAnchor:cardView.bottomAnchor constant:-12]
    ]];

    if (!useSafeAreaInsets) {
        [constraints addObject:[container.bottomAnchor constraintEqualToAnchor:cardView.bottomAnchor constant:edgeInset]];
    }

    [NSLayoutConstraint activateConstraints:constraints];
}

@end
