//
//  RecommendViewCell.m
//  Open
//
//  Created by 雷俊博 on 2020/7/6.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "RecommendViewCell.h"
#import "RecommendTableCell.h"
#import "XMSDK.h"
#import "UIImageView+WebCache.h"

@interface RecommendViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSMutableArray* dataArray;

@property (nonatomic, strong)NSArray* subArray;
@property (weak, nonatomic) IBOutlet UIButton *refreshBtn;

@end

@implementation RecommendViewCell

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.subArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendTableCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    XMAlbum*model = self.subArray[indexPath.section];
    
    [cell.img sd_setImageWithURL:[NSURL URLWithString:model.coverUrlLarge
                                  ] placeholderImage:[UIImage imageNamed:@""]];
    cell.lb_title.text = model.albumTitle;
    
    cell.lb_subTitle.text = model.albumIntro;
    cell.lb_count.text = [self getStrWithCount:model.playCount];
    
    cell.line.hidden = indexPath.section == 2 ? YES : NO;
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 106;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbum*model = self.subArray[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectCell:model:)]) {
        [self.delegate didSelectCell:self model:model];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
                         
    [self.tableView registerNib:[UINib nibWithNibName:@"RecommendTableCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.layer.cornerRadius = 5;
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [UIColor colorWithHex:0xcccccc].CGColor;
    self.dataArray = [NSMutableArray array];
    [self loadData];
}

#pragma mark - event response

- (IBAction)refreshBtnclick:(UIButton *)sender {
    if (self.dataArray.count > 0) {
        self.subArray = [self getsubArrayWithData:self.dataArray];
        [self.tableView reloadData];
    }
}

- (NSArray *)getsubArrayWithData:(NSMutableArray *)dataArray
{
    NSMutableArray*array = [NSMutableArray arrayWithArray:dataArray];
    NSMutableArray*array2 = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        int x = arc4random() % array.count;
        [array2 addObject:array[x]];
        [array removeObject:array[x]];
    }
    return array2;
}


#pragma mark - load data
- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@10 forKey:@"like_count"];
    WeakSelf
    [[XMReqMgr sharedInstance] requestXMData:XMReqType_AlbumsGuessLike
    params:params withCompletionHander:^(id result, XMErrorModel *error) {
    __strong typeof(self) strongSelf = weakSelf;
        if(!error){
        strongSelf.dataArray = [strongSelf showReceivedData:result className:@"XMAlbum"
    valuePath:nil titleNeedShow:@"recommendSrc"];
            strongSelf.subArray = [strongSelf getsubArrayWithData:strongSelf.dataArray];
            [strongSelf.tableView reloadData];
        }
    else
    NSLog(@"%@ %@",error.description,result);
    }];
}

- (NSMutableArray *)showReceivedData:(id)result className:(NSString*)className valuePath:(NSString *)path titleNeedShow:(NSString*)title
{
    NSMutableArray *models = [NSMutableArray array];
    Class dataClass = NSClassFromString(className);
    if([result isKindOfClass:[NSArray class]]){
        for (NSDictionary *dic in result) {
            id model = [[dataClass alloc] initWithDictionary:dic];
            [models addObject:model];
        }
    }
    else if([result isKindOfClass:[NSDictionary class]]){
        if(path.length == 0)
        {
            id model = [[dataClass alloc] initWithDictionary:result];
            [models addObject:model];
        }
        else
        {
            NSArray *paths = [path componentsSeparatedByString:@"."];
            
//            NSLog(@"paths have %lu objects inside", (unsigned long)[paths count]);
//            NSLog(@"paths last object is %@", paths.lastObject);
            
            NSDictionary *dic = result;
            for (int i=0;i<paths.count-1;i++)
            {
                NSString *subPath = paths[i];
                dic = dic[subPath];
            }
            for (NSDictionary *dict in dic[paths.lastObject])
            {
                id model = [[dataClass alloc] initWithDictionary:dict];
                [models addObject:model];
                
            }
        }
    }
    
    return models;
}
#pragma mark - private methods
- (NSString *)getStrWithCount:(NSInteger)count
{
    if (count < 10000) {
        return [NSString stringWithFormat:@"%ld",count];
    }else if (count < 100000000&& count>= 10000 ){
        return [NSString stringWithFormat:@"%ld万",count/10000];
    }else{
         return [NSString stringWithFormat:@"%ld亿",count/100000000];
    }
}

@end
