//
//  NvMoreFilterViewController.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/5/29.
 //
//

#import "NvMoreCustomStickerViewController.h"
#import "NvMoreAssetCell.h"
#import "NvAssetCellModel.h"
#import "NvAssetManager.h"
#import "MJRefresh.h"
#import "NvAsset.h"
#import "NvSDKUtils.h"

@interface NvMoreCustomStickerViewController ()<UITableViewDelegate, UITableViewDataSource, NvAssetManagerDelegate,NvMoreAssetCellDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NvAssetCellModel *model;

@end

@implementation NvMoreCustomStickerViewController {
    NvAssetManager *assetManager;
    NSMutableArray *assets;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NvSDKUtils getMoreTitleName:self.type];
    [self addSubViews];
    // Do any additional setup after loading the view.
    
    assets = NSMutableArray.new;
    assetManager = [NvAssetManager sharedInstance];
    assetManager.delegate = self;
}

- (void)dealloc {
}

- (void)addSubViews{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[NvMoreAssetCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        make.width.offset(SCREEN_WDITH);
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self->assets removeAllObjects];
        [self->assetManager downloadRemoteAssetsInfo:self.type aspectRatio:AspectRatio_All categoryId:NV_CATEGORY_ID_ALL page:0 pageSize:NV_FILTER_PAGE_SIZE];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        int page = (int)self->assets.count/NV_FILTER_PAGE_SIZE;
        [self->assetManager downloadRemoteAssetsInfo:self.type aspectRatio:AspectRatio_All categoryId:NV_CATEGORY_ID_ALL page:page pageSize:NV_FILTER_PAGE_SIZE];
    }];
}

#pragma mark UITableViewDataSource

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

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

- (NvMoreAssetCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NvMoreAssetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.type = self.type;
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.model = assets[indexPath.row];
    
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70 * SCREANSCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    [assetManager downloadAsset:[(NvFilterItem *)filters[indexPath.row] package]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onRemoteAssetsChanged:(BOOL)hasNext {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [assets removeAllObjects];
    NSArray *array = [assetManager getRemoteAssets:self.type aspectRatio:AspectRatio_All categoryId:NV_CATEGORY_ID_ALL];
    for (NvAsset *asset in array) {
        NvAssetCellModel *assetModel = [[NvAssetCellModel alloc]init];
        assetModel.displayName = asset.name;
        assetModel.cover = [asset.coverUrl absoluteString];
        assetModel.size = [NvSDKUtils getAssetPackageSizeString:asset.remotePackageSize];
        assetModel.draw = [NvSDKUtils getAssetAspectRatioString:asset.aspectRatio];
        assetModel.package = asset.uuid;
        if ([asset isUsable]) {
            if ([asset hasUpdate]) {
                assetModel.state = Update;
            }else{
                assetModel.state = Finish;
            }
        }else{
            assetModel.state = NODownload;
        }
        
        [assets addObject:assetModel];
    }
    [self.tableView reloadData];
}

- (void)onDownloadAssetProgress:(NSString *)uuid progress:(int)progress {
    NSLog(@"%d", progress);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0; i < self->assets.count; i++) {
            NvMoreAssetCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            NvAssetCellModel *item = cell.model;
            if ([item.package isEqualToString:uuid]) {
                cell.download.progress = progress/100.f;
            }
        }
    });
    
}

- (void)onDonwloadAssetSuccess:(NSString *)uuid {
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0; i < self->assets.count; i++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            NvMoreAssetCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            NvAssetCellModel *item = cell.model;
            if ([item.package isEqualToString:uuid]) {
                item.state = Finish;
                NSMutableArray *array = NSMutableArray.new;
                [array addObject:indexPath];
                [self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    });
}

- (void)onDonwloadAssetFailed:(NSString *)uuid {
    
}


- (void)onGetRemoteAssetsFailed {
    
}

- (void)nvMoreAssetCell:(NvMoreAssetCell *)nvMoreAssetCell nvAssetItem:(NvAssetCellModel *)nvAssetItem {
    [assetManager downloadAsset:nvAssetItem.package];
}

@end
