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

#import "CoinsRankTopTableCell.h"

@implementation CoinsRankTopTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //topbg
    UIImageView *topbgImageView = [UIImageView new];
    topbgImageView.image = [UIImage imageNamed:@"coinRankBg"];
    topbgImageView.frame = CGRectMake(0, 0, ScreenWidth, 155 * WIDTHRADIU);
    [self.contentView addSubview:topbgImageView];
    
    
    //cardView
    UIView *cardView = [[UIView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, 110 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 147 * WIDTHRADIU)];
    cardView.backgroundColor = [UIColor whiteColor];
    cardView.layer.cornerRadius = 10 * WIDTHRADIU;
    [self.contentView addSubview:cardView];
    
    cardView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    cardView.layer.shadowOffset = CGSizeMake(0,0);
    cardView.layer.shadowOpacity = 0.6;
    cardView.layer.shadowRadius = 1.0;
    cardView.clipsToBounds = NO;
    
    
    //可用金币
    UILabel *lb_currentCoins = [UILabel new];
    lb_currentCoins.backgroundColor = [UIColor clearColor];
    lb_currentCoins.numberOfLines = 0;
    lb_currentCoins.textAlignment = NSTextAlignmentCenter;
    lb_currentCoins.textColor = [UIColor colorWithHex:0x808080];
    lb_currentCoins.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_currentCoins.attributedText = [self setupAttributeString:@"0\n可用金币" rangeText:@"0" color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:24 * WIDTHRADIU]];
    [cardView addSubview:lb_currentCoins];
    
    self.lb_currentCoins = lb_currentCoins;
    
    
    //3.提现
    UIButton *btn_coinList = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_coinList setTitle:@"金币明细" forState:UIControlStateNormal];
    btn_coinList.backgroundColor = [UIColor colorWithHex:0xf35511];
    [btn_coinList setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn_coinList.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    btn_coinList.frame = CGRectMake(ScreenWidth - 110 * WIDTHRADIU, 31 * WIDTHRADIU, 90 * WIDTHRADIU, 32 * WIDTHRADIU);
    [btn_coinList addTarget:self action:@selector(coinBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_coinList.tag = 200;
    [cardView addSubview:btn_coinList];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn_coinList.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(16 * WIDTHRADIU,16 * WIDTHRADIU)];

    //圆角大小
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = btn_coinList.bounds;

    maskLayer.path = maskPath.CGPath;

    btn_coinList.layer.mask = maskLayer;
    
    
    //累计金币
    UILabel *lb_allCoins = [UILabel new];
    lb_allCoins.numberOfLines = 0;
    lb_allCoins.textAlignment = NSTextAlignmentCenter;
    lb_allCoins.textColor = [UIColor colorWithHex:0x808080];
    lb_allCoins.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_allCoins.attributedText = [self setupAttributeString:@"0\n累计金币" rangeText:@"0" color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:18 * WIDTHRADIU]];
    [cardView addSubview:lb_allCoins];
    self.lb_allCoins = lb_allCoins;
    
    
    //今日金币
    UILabel *lb_todayCoins = [UILabel new];
    lb_todayCoins.numberOfLines = 0;
    lb_todayCoins.textAlignment = NSTextAlignmentCenter;
    lb_todayCoins.textColor = [UIColor colorWithHex:0x808080];
    lb_todayCoins.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_todayCoins.attributedText = [self setupAttributeString:@"0\n今日金币" rangeText:@"0" color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:18 * WIDTHRADIU]];
    [cardView addSubview:lb_todayCoins];
    self.lb_todayCoins = lb_todayCoins;
    
    
    [lb_currentCoins mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cardView);
        make.top.offset(22 * WIDTHRADIU);
    }];
    
    [lb_allCoins mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cardView.mas_centerX).with.multipliedBy(2/5.0);
        make.bottom.offset(-20 * WIDTHRADIU);
    }];
    
    [lb_todayCoins mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cardView.mas_centerX).with.multipliedBy(8/5.0);
        make.bottom.offset(-20 * WIDTHRADIU);
    }];
  
    
    
}

- (void)reloadDataWithData:(NSDictionary *)dic
{
    if (dic) {
//        NSMutableAttributedString *todayProfitStr = [self setupAttributeString:[NSString stringWithFormat:@"今日收益 %@ 元",dic[@"currentDayProfit"]] rangeText:[NSString stringWithFormat:@"%@",dic[@"currentDayProfit"]] font:[UIFont boldSystemFontOfSize:15 * WIDTHRADIU]];
//
//        NSMutableAttributedString *profitSumStr = [self setupAttributeString:[NSString stringWithFormat:@"累计收益 %@ 元",dic[@"userCurrentAccrue"]] rangeText:[NSString stringWithFormat:@"%@",dic[@"userCurrentAccrue"]] font:[UIFont boldSystemFontOfSize:15 * WIDTHRADIU]];
//
//        self.lb_balance.text = [NSString stringWithFormat:@"%@",dic[@"currenttotalMoney"]];
//        self.lb_todayProfit.attributedText = todayProfitStr;
//        self.lb_profitSum.attributedText = profitSumStr;
        
        NSString *currentTotalCoins = [NSString stringWithFormat:@"%@",dic[@"totalScore"]];
        NSString *expendScore = [NSString stringWithFormat:@"%@",dic[@"expendScore"]];
        NSString *todaySore = [NSString stringWithFormat:@"%@",dic[@"todaySore"]];
        self.lb_currentCoins.attributedText = [self setupAttributeString:[NSString stringWithFormat:@"%@\n可用金币",currentTotalCoins] rangeText:currentTotalCoins color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:24 * WIDTHRADIU]];
        self.lb_allCoins.attributedText = [self setupAttributeString:[NSString stringWithFormat:@"%@\n累计金币",@(currentTotalCoins.integerValue +expendScore.integerValue)] rangeText:[NSString stringWithFormat:@"%@",@(currentTotalCoins.integerValue +expendScore.integerValue)] color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:18 * WIDTHRADIU]];
        self.lb_todayCoins.attributedText = [self setupAttributeString:[NSString stringWithFormat:@"%@\n今日金币",todaySore] rangeText:todaySore color:[UIColor colorWithHex:0x4d4d4d] font:[UIFont boldSystemFontOfSize:18 * WIDTHRADIU]];
        
    }
    
}


#pragma mark - event response
- (void)coinBtnClick:(UIButton *)sender
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(CoinsRankTopTableCellBtnClickWithIndex:)]) {
        [self.delegate CoinsRankTopTableCellBtnClickWithIndex:sender.tag];
    }
    
}





- (NSMutableAttributedString *)setupAttributeString:(NSString *)text rangeText:(NSString *)rangeText color:(UIColor *)color font:(UIFont *)font  {
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
       //调整行间距
    paragraphStyle.lineSpacing = 7 * WIDTHRADIU;
    
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle};
    
    NSRange hightlightTextRange = [text rangeOfString:rangeText];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attriDict];
    if (hightlightTextRange.length > 0) {
        [attributeStr addAttribute:NSForegroundColorAttributeName
                                     value:color
                                     range:hightlightTextRange];
        [attributeStr addAttribute:NSFontAttributeName value:font range:hightlightTextRange];
        return attributeStr;
    }else {
        return [attributeStr 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
