//
//  MyRankTopTableCell.m
//  Open
//
//  Created by 雷俊博 on 2021/7/22.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "MyRankTopTableCell.h"

@implementation MyRankTopTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //topbg
    UIImageView *topbgImageView = [UIImageView new];
    topbgImageView.image = [UIImage imageNamed:@"coin_tu"];
    topbgImageView.frame = CGRectMake(0, 0, ScreenWidth, 145 * WIDTHRADIU);
    [self.contentView addSubview:topbgImageView];
    
    UIView *contentView = [[UIView alloc]init];
    //contentView.backgroundColor = [UIColor redColor];
    [topbgImageView addSubview:contentView];
    
    //收益排行榜标题
    UILabel *lb_rankTitle = [UILabel new];
    lb_rankTitle.text = @"收益排行榜";
    lb_rankTitle.textColor = [UIColor whiteColor];
    lb_rankTitle.font = [UIFont boldSystemFontOfSize:34 * WIDTHRADIU];
    //lb_rankTitle.backgroundColor = [UIColor blueColor];
    [contentView addSubview:lb_rankTitle];
    
    //更新日期
    UILabel *lb_date = [UILabel new];
    lb_date.text = [NSString stringWithFormat:@"更新于:  %@",[self getCurrentDate]];
    lb_date.textColor = [UIColor whiteColor];
    lb_date.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    //lb_date.backgroundColor = [UIColor blueColor];
    [contentView addSubview:lb_date];
    
    [lb_rankTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.offset(0);
    }];
    
    [lb_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_rankTitle);
        make.bottom.offset(0);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topbgImageView);
        make.left.offset(0);
        make.width.offset(ScreenWidth);
        make.height.offset(64 * WIDTHRADIU);
    }];
    
    
    
    //cardView
    UIView *cardView = [[UIView alloc]initWithFrame:CGRectMake(0, 115 * WIDTHRADIU, ScreenWidth, 85 * WIDTHRADIU)];
    cardView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:cardView];
    
    UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:cardView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(15 * WIDTHRADIU, 15 * WIDTHRADIU)];
    
    CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
    
    cornerRadiusLayer.frame = cardView.bounds;
    
    cornerRadiusLayer.path = cornerRadiusPath.CGPath;
    
    cardView.layer.mask = cornerRadiusLayer;
    
    
    //头像
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.layer.cornerRadius = 25 * WIDTHRADIU;
    iconImageView.layer.masksToBounds = YES;
    iconImageView.backgroundColor = [UIColor clearColor];
    [cardView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15 * WIDTHRADIU);
        make.centerY.equalTo(cardView);
        make.width.height.offset(50 * WIDTHRADIU);
    }];
    
    self.userIcon = iconImageView;
    
    //排名
    UILabel *lb_rank = [UILabel new];
    lb_rank.text = @"累计收益排名--+";
    lb_rank.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_rank.font = [UIFont boldSystemFontOfSize:16 * WIDTHRADIU];
    [cardView addSubview:lb_rank];
    [lb_rank mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(10 * WIDTHRADIU);
        make.top.equalTo(iconImageView);
    }];
    
    self.lb_rank = lb_rank;
    
    //百分比
    UILabel *lb_percent = [UILabel new];
    lb_percent.textColor = [UIColor colorWithHex:0x808080];
    lb_percent.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_percent.attributedText = [self setupAttributeString:@"超越了--%的人" rangeText:@"--" color:[UIColor colorWithHex:0xf98505] font:[UIFont boldSystemFontOfSize:14 * WIDTHRADIU]];
    [cardView addSubview:lb_percent];
    
    self.lb_percent = lb_percent;
    
    [lb_percent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_rank);
        make.top.equalTo(lb_rank.mas_bottom).offset(15 * WIDTHRADIU);
    }];
    
    //累计收益
    UILabel *lb_profitSum = [UILabel new];
    //lb_profitSum.backgroundColor = [UIColor redColor];
    lb_profitSum.text = @"0.00元";
    lb_profitSum.textColor = [UIColor colorWithHex:0xf98505];
    lb_profitSum.font = [UIFont boldSystemFontOfSize:15 * WIDTHRADIU];
    [cardView addSubview:lb_profitSum];
    self.lb_sumProfit = lb_profitSum;
    
    [lb_profitSum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15 * WIDTHRADIU);
        make.centerY.equalTo(cardView);
    }];
    
    
}

- (NSMutableAttributedString *)setupAttributeString:(NSString *)text rangeText:(NSString *)rangeText color:(UIColor *)color font:(UIFont *)font  {
    NSRange hightlightTextRange = [text rangeOfString:rangeText];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (hightlightTextRange.length > 0) {
        [attributeStr addAttribute:NSForegroundColorAttributeName
                                     value:color
                                     range:hightlightTextRange];
        [attributeStr addAttribute:NSFontAttributeName value:font range:hightlightTextRange];
        return attributeStr;
    }else {
        return [rangeText copy];
    }
}

- (NSString *)getCurrentDate
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    //获取当前时间日期展示字符串 如：2019-05-23-13:58:59
    NSString *str = [formatter stringFromDate:date];
    return str;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
