//
//  LearninglistViewController.m
//  ChinaTheoryNetwork
//
//  Created by 张毅成 on 2019/1/28.
//  Copyright © 2019 张毅成. All rights reserved.
//

#import "LearninglistViewController.h"
#import "LearninglistTableViewCell.h"
#import "BlacklistModel.h"

#define cacheKey @"LearninglistCacheKey"

@interface LearninglistViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *arrayData;

@end

@implementation LearninglistViewController

- (NSMutableArray *)arrayData {
    if (!_arrayData) {
        _arrayData = @[].mutableCopy;
    }
    return _arrayData;
}

- (void)Network {
    NSString *URL = @"my/studyRank";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [[HttpManager sharedHttpManager] GET:URL parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            NSArray *array = [NSArray modelArrayWithClass:[BlacklistModel class] json:responseObject[@"data"][@"memberUserInfoList"]].mutableCopy;
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                BlacklistModel *modelNew = obj;
                [self.arrayData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    BlacklistModel *modelOld = obj;
                    if ([modelNew.UserId isEqualToString:modelOld.UserId]) {
                        modelNew.isLike = modelOld.isLike;
                    }
                }];
            }];
            [self.arrayData removeAllObjects];
            [self.arrayData addObjectsFromArray:array];
            [self.tableView reloadData];
            YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
            [cache setObject:self.arrayData forKey:cacheKey];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学习榜单";
    [self createTableView];
    [self getCache];
    [self Network];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
    [cache setObject:self.arrayData forKey:cacheKey];
}

- (void)createTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
//    self.tableView.separatorStyle = 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LearninglistTableViewCell *cell = [LearninglistTableViewCell cellWithTableView:tableView];
    BlacklistModel *model = self.arrayData[indexPath.row];
    cell.labelTitile.text = model.BlackUserName;
    cell.labelTime.text = model.LastModifiedDate;
    [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage];
    cell.labelNumber.text = @(indexPath.row + 1).stringValue;
    cell.buttonLike.selected = model.isLike;
    cell.blockDidTouchButtonLike = ^{
        model.isLike = !model.isLike;
        [tableView reloadRowAtIndexPath:indexPath withRowAnimation:(UITableViewRowAnimationNone)];
    };
    if (indexPath.row < 3) {
        [cell.labelNumber setTextColor:[UIColor redColor]];
    }else{
        [cell.labelNumber setTextColor:[UIColor hexStringToColor:@"666666"]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (void)getCache {
    [self.arrayData removeAllObjects];
    YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
    BOOL isContains = [cache containsObjectForKey:cacheKey];
    NSLog(@"%d",isContains);
    id value = [cache objectForKey:cacheKey];
    NSLog(@"%@",value);
    if (value) {
        self.arrayData = value;
        [self.tableView reloadData];
    }
}

@end