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

#import "MyWalletTopCardTableCell.h"

@implementation MyWalletTopCardTableCell
- (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
{
    //card
    UIView *cardView = [[UIView alloc]init];
    cardView.backgroundColor = [UIColor colorWithHex:0xf98505];
    cardView.layer.cornerRadius = 15 * WIDTHRADIU;
    [self.contentView addSubview:cardView];
    [cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10 * WIDTHRADIU, 10 * WIDTHRADIU, 10 * WIDTHRADIU, 10 * WIDTHRADIU));
    }];
    
    
    //可用余额数值
    UILabel *lb_balance = [UILabel new];
    //lb_balance.backgroundColor = [UIColor redColor];
    lb_balance.text = @"0.00";
    lb_balance.textColor = [UIColor whiteColor];
    lb_balance.font = [UIFont boldSystemFontOfSize:28 * WIDTHRADIU];
    [cardView addSubview:lb_balance];
    self.lb_balance = lb_balance;
    
    //可用余额单位
    UILabel *lb_balanceMessage = [UILabel new];
    //lb_balanceMessage.backgroundColor = [UIColor redColor];
    lb_balanceMessage.text = @"可用余额(元)";
    lb_balanceMessage.textColor = [UIColor whiteColor];
    lb_balanceMessage.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    [cardView addSubview:lb_balanceMessage];
    
    //今日收益
    UILabel *lb_todayProfit = [UILabel new];
    //lb_todayProfit.backgroundColor = [UIColor redColor];
    lb_todayProfit.text = @"今日收益 0.00 元";
    lb_todayProfit.textColor = [UIColor whiteColor];
    lb_todayProfit.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    [cardView addSubview:lb_todayProfit];
    self.lb_todayProfit = lb_todayProfit;
    
    
    //3.提现
    UIButton *btn_tx = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_tx setTitle:@"我要提现" forState:UIControlStateNormal];
    btn_tx.backgroundColor = [UIColor colorWithHex:0xf35511];
    [btn_tx setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn_tx.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    btn_tx.frame = CGRectMake(ScreenWidth - 110 * WIDTHRADIU, 18 * WIDTHRADIU, 90 * WIDTHRADIU, 32 * WIDTHRADIU);
    [btn_tx addTarget:self action:@selector(txBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_tx.tag = 200;
    [cardView addSubview:btn_tx];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn_tx.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(16 * WIDTHRADIU,16 * WIDTHRADIU)];

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

    maskLayer.frame = btn_tx.bounds;

    maskLayer.path = maskPath.CGPath;

    btn_tx.layer.mask = maskLayer;
    
    
    //累计收益
    UILabel *lb_profitSum = [UILabel new];
    //lb_profitSum.backgroundColor = [UIColor redColor];
    lb_profitSum.text = @"累计收益 0.00 元";
    lb_profitSum.textColor = [UIColor whiteColor];
    lb_profitSum.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    [cardView addSubview:lb_profitSum];
    self.lb_profitSum = lb_profitSum;
    
    //UI约束
    [lb_balance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.offset(15 * WIDTHRADIU);
    }];
    
    [lb_balanceMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_balance);
        make.top.equalTo(lb_balance.mas_bottom).offset(5 * WIDTHRADIU);
    }];
    
    
    [lb_todayProfit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_balance);
        make.bottom.offset(-22 * WIDTHRADIU);
    }];
    
    [lb_profitSum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15 * WIDTHRADIU);
        make.bottom.offset(-22 * WIDTHRADIU);
    }];
    
    
    
}

- (void)reloadDataWithData:(NSDictionary *)dic
{
    if (dic) {
        NSString *currentDayProfit = [NSString stringWithFormat:@"%@",dic[@"currentDayProfit"]];
        if ([Tools isBlankString:currentDayProfit]) {
            currentDayProfit = @"0.0";
        }
        
        NSString *userCurrentAccrue = [NSString stringWithFormat:@"%@",dic[@"userCurrentAccrue"]];
        if ([Tools isBlankString:userCurrentAccrue]) {
            userCurrentAccrue = @"0.0";
        }
        
        NSString *currenttotalMoney = [NSString stringWithFormat:@"%@",dic[@"currenttotalMoney"]];
        if ([Tools isBlankString:currenttotalMoney]) {
            currenttotalMoney = @"0.0";
        }
        
        NSMutableAttributedString *todayProfitStr = [self setupAttributeString:[NSString stringWithFormat:@"今日收益 %@ 元",currentDayProfit] rangeText:[NSString stringWithFormat:@"%@",currentDayProfit] font:[UIFont boldSystemFontOfSize:15 * WIDTHRADIU]];
        
        NSMutableAttributedString *profitSumStr = [self setupAttributeString:[NSString stringWithFormat:@"累计收益 %@ 元",userCurrentAccrue] rangeText:[NSString stringWithFormat:@"%@",userCurrentAccrue] font:[UIFont boldSystemFontOfSize:15 * WIDTHRADIU]];
        
        self.lb_balance.text = currenttotalMoney;
        self.lb_todayProfit.attributedText = todayProfitStr;
        self.lb_profitSum.attributedText = profitSumStr;
    }
    
}

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

- (NSMutableAttributedString *)setupAttributeString:(NSString *)text rangeText:(NSString *)rangeText font:(UIFont *)font  {
    NSRange hightlightTextRange = [text rangeOfString:rangeText];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (hightlightTextRange.length > 0) {
        [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
