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

#import "CoinsRankMiddleTableCell.h"

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

- (void)setupUI
{
    //header
    UIView *rankHeaderView = [UIView new];
    [self.contentView addSubview:rankHeaderView];
    [rankHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5 * WIDTHRADIU);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(38 * WIDTHRADIU);
    }];
    
    //收益排行榜
    UILabel *lb_rankTitle = [UILabel new];
    lb_rankTitle.text = @"金币累计排行榜";
    lb_rankTitle.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
    lb_rankTitle.textColor = [UIColor colorWithHex:0x4c4c4c];
    [rankHeaderView addSubview:lb_rankTitle];
    [lb_rankTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10 * WIDTHRADIU);
        make.centerY.equalTo(rankHeaderView);
    }];
    
    //每日零点更新
    UILabel *lb_dayRefresh = [UILabel new];
    lb_dayRefresh.text = @"";
    lb_dayRefresh.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    lb_dayRefresh.textColor = [UIColor colorWithHex:0x808080];
    [rankHeaderView addSubview:lb_dayRefresh];
    [lb_dayRefresh mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_rankTitle.mas_right).offset(15 * WIDTHRADIU);
        make.centerY.equalTo(rankHeaderView);
    }];
    
    
    //排名超过多少人
    UIView *cardView = [[UIView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, 43 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 85 * WIDTHRADIU)];
    cardView.backgroundColor = [UIColor whiteColor];
    cardView.layer.cornerRadius = 15 * WIDTHRADIU;
    cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    cardView.layer.shadowOffset = CGSizeMake(0, 0);
    cardView.layer.shadowOpacity = 0.3;
    cardView.layer.shadowRadius = 3;
    [self.contentView addSubview:cardView];
    
    
    //头像
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    iconImageView.layer.cornerRadius = 25 * WIDTHRADIU;
    iconImageView.layer.masksToBounds = YES;
    iconImageView.backgroundColor = [UIColor clearColor];
    [cardView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5 * 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";
    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];
    }
}


- (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
