//
//  NvAlbumViewController.m
//  SDKDemo
//
//  Created by Meicam on 2018/5/25.
//  Copyright © 2018年 刘东旭. All rights reserved.
//

#import "NvAlbumViewController.h"
#import "NvAllAssetCell.h"
#import "NvAlbumItem.h"
#import "NvAlbumCollectionView.h"
#import "NvFetchAlbum.h"
#import "NvAlbumUtils.h"

@import Photos;

@interface NvAlbumViewController ()<UIScrollViewDelegate> {
    UIView *bottomView;
}

@property (nonatomic, strong) UIView *tabView;
@property (nonatomic, strong) UIButton *allButton;
@property (nonatomic, strong) UIButton *videoButton;
@property (nonatomic, strong) UIButton *imageButton;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NvAlbumCollectionView *albumCollectionView;
@property (nonatomic, strong) NvAlbumCollectionView *videoCollectionView;
@property (nonatomic, strong) NvAlbumCollectionView *imageCollectionView;

@property (nonatomic, assign) NSInteger selectAssetCount;
@property (nonatomic, assign) NSInteger selectVideoAssetCount;
@property (nonatomic, assign) NSInteger selectImageAssetCount;
@property (nonatomic, strong) UIImage *selectImage;
@property (nonatomic, strong) NvFetchAlbum *album;

@property (nonatomic, strong) NSMutableArray <NvAlbumAsset *>*selectAssetSource;//被选择的资源
@property (nonatomic, assign) NSInteger selectIndex;//被选择的资源index用于计算被选择的个数

//底部按钮
@property (nonatomic, strong) UIButton *customButton;

@property (nonatomic, strong) NSString *nextText;

@end

@implementation NvAlbumViewController

- (void)dealloc {
    NSLog(@"%s",__func__);
    //[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    if (self = [super init]) {
        self.selectAssetSource = [NSMutableArray array];
        self.mutableSelect = YES;
        
        self.maxSelectCount = 0;
        self.minSelectCount = 1;
        self.hiddenSelectAll = NO;
        self.alwaysShowCustomBottom = NO;
    }
    return self;
}

#pragma mark viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:0.001],
                                 NSForegroundColorAttributeName:[UIColor clearColor]};
    [item setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = [UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [NvAlbumUtils fontWithSize:16], NSFontAttributeName, nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[self leftNavigationBarItemView]];
    [self initSubViews];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(realodData) name:@"NvReloadAlbumData" object:nil];
}

// MARK: 设置标题
- (void)setTitleWithCount:(NSInteger)count {
    if (count == 0) {
        self.title = @"选择内容";
    } else {
        if (!self.mutableSelect) {
            self.title = @"已选择";
        } else {
            self.title = [NSString stringWithFormat:@"已选%ld项",(long)count];
        }
    }
}

- (UIView *)leftNavigationBarItemView {
    UIButton *backButton;
    backButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvAlbumUtils imageWithName:@"icon_back"]];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15*SCREANSCALE, 0, 0);
    __weak typeof(self)weakSelf = self;
    [backButton nv_BtnClickHandler:^{
        if ([weakSelf.delegate respondsToSelector:@selector(nvAlbumViewControllerCancelClick:)]) {
            [weakSelf.delegate nvAlbumViewControllerCancelClick:weakSelf];
        } else {
            if (weakSelf.navigationController.viewControllers.count == 1) {
                [weakSelf dismissViewControllerAnimated:YES completion:NULL];
            } else {
                if (weakSelf.navigationController.topViewController != weakSelf) {
                    [weakSelf dismissViewControllerAnimated:YES completion:NULL];
                } else {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }
            }
        }
    }];
    return backButton;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    
    self.selectAssetCount = _selectIndex;
    [self setTitleWithCount:_selectIndex];
    //一直显示底部按钮
    if (self.alwaysShowCustomBottom) {
        bottomView.hidden = NO;
        
    } else {
        if (_selectIndex > 0) {
            //显示开始制作按钮
            if ([self.delegate respondsToSelector:@selector(nvAlbumViewControllerCustomBottomButton)] && bottomView != nil) {
                if (self.selectAssetSource.count != 0) {
                    bottomView.hidden = NO;
                }
            } else {
                bottomView.hidden = NO;
                if (selectIndex >= self.minSelectCount) {
                    [self.customButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
                } else {//否则置灰按钮
                    [self.customButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#FFA3A3A3"] forState:UIControlStateNormal];
                }
                if (bottomView != nil) {
                    return;
                }
                
                bottomView = [UIView new];
                NSLog(@"======>%f",self.view.height);
                bottomView.frame = CGRectMake(0, SCREANHEIGHT - NV_STATUSBARHEIGHT - 44 - (49*SCREANSCALE + INDICATOR), SCREANWIDTH, 49*SCREANSCALE + INDICATOR);
                [self.view addSubview:bottomView];
                bottomView.backgroundColor = [UIColor nv_colorWithHexRGB:@"#363738"];
                //如果被选择的个数大于最小限制值，则允许点击按钮
                if (_selectIndex >= self.minSelectCount) {
                    [self.customButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
                } else {//否则置灰按钮
                    [self.customButton setTitleColor:[UIColor nv_colorWithHexARGB:@"#FFA3A3A3"] forState:UIControlStateNormal];
                }
                
                if (self.nextText && ![self.nextText isEqualToString:@""]) {
                    [self.customButton setTitle:self.nextText forState:UIControlStateNormal];
                }
                self.customButton.frame = CGRectMake(0, 0, SCREANWIDTH, 49*SCREANSCALE);
                [bottomView addSubview:self.customButton];
                __weak typeof(self)weakSelf = self;
                [self.customButton nv_BtnClickHandler:^{
                    if (weakSelf.selectIndex >= weakSelf.minSelectCount) {
                        if ([weakSelf.delegate respondsToSelector:@selector(nvAlbumViewController:selectAlbumAssets:)]) {
                            [weakSelf.delegate nvAlbumViewController:weakSelf selectAlbumAssets:weakSelf.selectAssetSource];
                        }
                    } else {
                        if ([weakSelf.delegate respondsToSelector:@selector(nvAlbumViewController:selectAlbumAssetsUnderMinCountLimit:)]) {
                            [weakSelf.delegate nvAlbumViewController:weakSelf selectAlbumAssetsUnderMinCountLimit:weakSelf.selectAssetSource];
                        }
                    }
                }];
            }
            self.scrollView.frame = CGRectMake(0, self.tabView.bottom, SCREANWIDTH, SCREANHEIGHT - NV_STATUSBARHEIGHT - 44 - self.tabView.height - (49*SCREANSCALE + INDICATOR));
            self.scrollView.contentSize = CGSizeMake(3*SCREANWIDTH, self.scrollView.height);
            float collectionViewHeight = 0;
            if (self.isOnlyImage || self.isOnlyVideo) {
                collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44- 49*SCREANSCALE;
            } else {
                collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44 - 44*SCREANSCALE - 49*SCREANSCALE;
            }
            
            if (self.isOnlyImage) {
                self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight-INDICATOR);
            } else if (self.isOnlyVideo) {
                self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight-INDICATOR);
            } else {
                self.albumCollectionView.frame = CGRectMake(self.albumCollectionView.left, self.albumCollectionView.top, self.albumCollectionView.width, collectionViewHeight-INDICATOR);
                self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight-INDICATOR);
                self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight-INDICATOR);
            }
        } else {
            //隐藏开始制作按钮
            bottomView.hidden = YES;
            self.scrollView.frame = CGRectMake(0, self.tabView.bottom, SCREANWIDTH, SCREANHEIGHT - NV_STATUSBARHEIGHT - 44);
            self.scrollView.contentSize = CGSizeMake(3*SCREANWIDTH, self.scrollView.height);
            float collectionViewHeight = 0;
            if (self.isOnlyImage || self.isOnlyVideo) {
                collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44;
            } else {
                collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44 - 44*SCREANSCALE;
            }
            
            if (self.isOnlyImage) {
                self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight);
            } else if (self.isOnlyVideo) {
                self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight);
            } else {
                self.albumCollectionView.frame = CGRectMake(self.albumCollectionView.left, self.albumCollectionView.top, self.albumCollectionView.width, collectionViewHeight);
                self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight);
                self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight);
            }
        }
    }
}

- (void)customSelectAssetButtonText:(NSString *)text {
    self.nextText = text;
    [self.customButton setTitle:self.nextText forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// MARK: 分类点击
- (void)allContentsClick {
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.allButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
    [self.videoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.imageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.bottomLine.frame = CGRectMake(self.allButton.left, self.allButton.bottom, self.allButton.width, 3*SCREANSCALE);
    [self setTitleWithCount:self.selectAssetCount];
}

- (void)videoClick {
    [self.scrollView setContentOffset:CGPointMake(SCREEN_WDITH, 0) animated:YES];
    [self.allButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.videoButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
    [self.imageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.bottomLine.frame = CGRectMake(self.allButton.right, self.allButton.bottom, self.allButton.width, 3*SCREANSCALE);
    [self setTitleWithCount:self.selectAssetCount];
}

- (void)imageClick {
    [self.scrollView setContentOffset:CGPointMake(2*SCREEN_WDITH, 0) animated:YES];
    [self.allButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.videoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.imageButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
    self.bottomLine.frame = CGRectMake(self.videoButton.right, self.allButton.bottom, self.allButton.width, 3*SCREANSCALE);
    [self setTitleWithCount:self.selectAssetCount];
}

- (void)realodData {
    [self.albumCollectionView realoadData];
    [self.videoCollectionView realoadData];
    [self.imageCollectionView realoadData];
}

// MARK: 初始化试图
- (void)initSubViews {
    self.title = @"选择内容";
    self.view.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
    __weak typeof(self)weakSelf = self;
    NSLog(@"--------->%@",NSStringFromCGRect(self.view.frame));
    NSLog(@"---------->>%f",SCREANHEIGHT);
    self.tabView = [[UIView alloc] init];
    self.tabView.backgroundColor = [UIColor nv_colorWithHexRGB:@"#363738"];
    [self.view addSubview:self.tabView];
    float tabViewHeight = 44*SCREANSCALE;
    if (self.isOnlyImage||self.isOnlyVideo) {
        tabViewHeight = 0;
    }
    self.tabView.frame = CGRectMake(0, 0, SCREANWIDTH, tabViewHeight);
    [self.tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.right.equalTo(@0);
        make.height.equalTo(@(tabViewHeight));
    }];
    //如果是图片模式或者是视频模式不显示“所有内容”，“视频”，“图片”选项卡
    if (!self.isOnlyImage && !self.isOnlyVideo) {
        self.allButton = [UIButton nv_buttonWithTitle:@"所有内容" textColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] fontSize:12];
        [self.tabView addSubview:self.allButton];
        self.allButton.frame = CGRectMake(0, 0, SCREEN_WDITH/3.0, self.tabView.height);
        
        [self.allButton nv_BtnClickHandler:^{
            [weakSelf allContentsClick];
        }];
        
        self.videoButton = [UIButton nv_buttonWithTitle:@"视频" textColor:[UIColor whiteColor] fontSize:12];
        self.videoButton.frame = CGRectMake(self.allButton.right, 0, SCREEN_WDITH/3.0, self.tabView.height);
        [self.tabView addSubview:self.videoButton];
        
        [self.videoButton nv_BtnClickHandler:^{
            [weakSelf videoClick];
        }];
        
        self.imageButton = [UIButton nv_buttonWithTitle:@"图片" textColor:[UIColor whiteColor] fontSize:12];
        self.imageButton.frame = CGRectMake(self.videoButton.right, 0, SCREEN_WDITH/3.0, self.tabView.height);
        [self.tabView addSubview:self.imageButton];
        
        [self.imageButton nv_BtnClickHandler:^{
            [weakSelf imageClick];
        }];
        
        self.bottomLine = [UIView new];
        self.bottomLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
        [self.tabView addSubview:self.bottomLine];
        self.bottomLine.frame = CGRectMake(self.allButton.left, self.allButton.bottom, self.allButton.width, 3*SCREANSCALE);
        
    }
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.frame = CGRectMake(0, self.tabView.bottom, SCREANWIDTH, self.view.height - NV_STATUSBARHEIGHT - 44 - self.tabView.height - INDICATOR);
    self.scrollView.contentSize = CGSizeMake(3*SCREANWIDTH, self.scrollView.height);
    
    float collectionViewHeight = 0;
    if (self.isOnlyImage || self.isOnlyVideo) {
        collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44;
    } else {
        collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44 - 44*SCREANSCALE;
    }
    NvAlbumAssetType type;
    if (self.isOnlyImage) {
        type = NvAlbumAssetAllImage;
    } else if (self.isOnlyVideo) {
        type = NvAlbumAssetAllVideo;
    } else {
        type = NvAlbumAssetAll;
    }
    //图片
    if (self.isOnlyImage) {
        self.imageCollectionView = [[NvAlbumCollectionView alloc] initWithFrame:CGRectZero withMediaType:NvAlbumAssetAllImage];
        self.imageCollectionView.mutableSelect = self.mutableSelect;
        self.imageCollectionView.hiddenSelectAll = self.hiddenSelectAll;
        [self.scrollView addSubview:self.imageCollectionView];
        self.imageCollectionView.delegate = self;
        self.imageCollectionView.frame = CGRectMake(0, 0, SCREANWIDTH, self.scrollView.height);
        
        self.album = [NvFetchAlbum new];
        [self.album loadAssetcomplete:^(NSMutableArray<NvAlbumItem *> *dataSource, NSMutableArray<NvAlbumItem *> *videoDataSource, NSMutableArray<NvAlbumItem *> *imageDataSource) {
            weakSelf.imageCollectionView.assetDataSource = imageDataSource;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.imageCollectionView realoadData];
            });
        }];
    } else if (self.isOnlyVideo) {//视频
        self.videoCollectionView = [[NvAlbumCollectionView alloc] initWithFrame:CGRectZero withMediaType:NvAlbumAssetAllVideo];
        self.videoCollectionView.mutableSelect = self.mutableSelect;
        self.videoCollectionView.hiddenSelectAll = self.hiddenSelectAll;
        [self.scrollView addSubview:self.videoCollectionView];
        self.videoCollectionView.delegate = self;
        self.videoCollectionView.frame = CGRectMake(0, 0, SCREANWIDTH, self.scrollView.height);
        
        self.album = [NvFetchAlbum new];
        [self.album loadAssetcomplete:^(NSMutableArray<NvAlbumItem *> *dataSource, NSMutableArray<NvAlbumItem *> *videoDataSource, NSMutableArray<NvAlbumItem *> *imageDataSource) {
            weakSelf.videoCollectionView.assetDataSource = videoDataSource;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.videoCollectionView realoadData];
            });
        }];
    } else {//所有
        self.albumCollectionView = [[NvAlbumCollectionView alloc] initWithFrame:CGRectZero withMediaType:type];
        self.albumCollectionView.mutableSelect = self.mutableSelect;
        self.albumCollectionView.hiddenSelectAll = self.hiddenSelectAll;
        [self.scrollView addSubview:self.albumCollectionView];
        self.albumCollectionView.delegate = self;
        self.albumCollectionView.frame = CGRectMake(0, 0, SCREANWIDTH, self.scrollView.height);
        
        self.videoCollectionView = [[NvAlbumCollectionView alloc] initWithFrame:CGRectZero withMediaType:NvAlbumAssetAllVideo];
        self.videoCollectionView.mutableSelect = self.mutableSelect;
        self.videoCollectionView.hiddenSelectAll = self.hiddenSelectAll;
        [self.scrollView addSubview:self.videoCollectionView];
        self.videoCollectionView.delegate = self;
        self.videoCollectionView.frame = CGRectMake(self.albumCollectionView.right, 0, SCREANWIDTH, self.scrollView.height);
        
        self.imageCollectionView = [[NvAlbumCollectionView alloc] initWithFrame:CGRectZero withMediaType:NvAlbumAssetAllImage];
        self.imageCollectionView.mutableSelect = self.mutableSelect;
        self.imageCollectionView.hiddenSelectAll = self.hiddenSelectAll;
        [self.scrollView addSubview:self.imageCollectionView];
        self.imageCollectionView.delegate = self;
        self.imageCollectionView.frame = CGRectMake(self.videoCollectionView.right, 0, SCREANWIDTH, self.scrollView.height);
        
        self.album = [NvFetchAlbum new];
        [self.album loadAssetcomplete:^(NSMutableArray<NvAlbumItem *> *dataSource, NSMutableArray<NvAlbumItem *> *videoDataSource, NSMutableArray<NvAlbumItem *> *imageDataSource) {
            weakSelf.albumCollectionView.assetDataSource = dataSource;
            weakSelf.videoCollectionView.assetDataSource = videoDataSource;
            weakSelf.imageCollectionView.assetDataSource = imageDataSource;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.videoCollectionView realoadData];
                [weakSelf.imageCollectionView realoadData];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.albumCollectionView realoadData];
                });
            });
        }];
    }
    if ([self.delegate respondsToSelector:@selector(nvAlbumViewControllerCustomBottomButton)]) {
        bottomView = [self.delegate nvAlbumViewControllerCustomBottomButton];
        if (bottomView) {
            [self.view addSubview:bottomView];
            bottomView.hidden = !self.alwaysShowCustomBottom;
            bottomView.frame = CGRectMake(0, self.view.height - NV_STATUSBARHEIGHT - 44 - (49*SCREANSCALE + INDICATOR), SCREANWIDTH, 49*SCREANSCALE + INDICATOR);
            
            if (self.alwaysShowCustomBottom) {
                self.scrollView.frame = CGRectMake(0, self.tabView.bottom, SCREANWIDTH, self.view.height - (49*SCREANSCALE + INDICATOR));
                self.scrollView.contentSize = CGSizeMake(3*SCREANWIDTH, self.scrollView.height);
                float collectionViewHeight = 0;
                if (self.isOnlyImage || self.isOnlyVideo) {
                    collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44- 49*SCREANSCALE;
                } else {
                    collectionViewHeight = SCREEN_HEIGTH - NV_STATUSBARHEIGHT - 44 - 44*SCREANSCALE - 49*SCREANSCALE;
                }
                
                if (self.isOnlyImage) {
                    self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight-INDICATOR);
                } else if (self.isOnlyVideo) {
                    self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight-INDICATOR);
                } else {
                    self.albumCollectionView.frame = CGRectMake(self.albumCollectionView.left, self.albumCollectionView.top, self.albumCollectionView.width, collectionViewHeight-INDICATOR);
                    self.videoCollectionView.frame = CGRectMake(self.videoCollectionView.left, self.videoCollectionView.top, self.videoCollectionView.width, collectionViewHeight-INDICATOR);
                    self.imageCollectionView.frame = CGRectMake(self.imageCollectionView.left, self.imageCollectionView.top, self.imageCollectionView.width, collectionViewHeight-INDICATOR);
                }
            }
        }
    }
}

- (UIButton *)customButton {
    if (!_customButton) {
        _customButton = [UIButton nv_buttonWithTitle:@"开始制作" textColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] fontSize:16];
    }
    return _customButton;
}

- (void)addAsset:(NvAlbumAsset *)selectAsset removeContains:(BOOL)isRemoveContains {
    if ([self.selectAssetSource containsObject:selectAsset]) {
        if (isRemoveContains) {
            [self.selectAssetSource removeObject:selectAsset];
        }
    } else {
        [self.selectAssetSource addObject:selectAsset];
    }
    [self ascendingWithSelectAsset];
    self.selectIndex = self.selectAssetSource.count;
}

- (void)removeAsset:(NvAlbumAsset *)selectAsset {
    if ([self.selectAssetSource containsObject:selectAsset]) {
        [self.selectAssetSource removeObject:selectAsset];
    }
    
    [self ascendingWithSelectAsset];
    self.selectIndex = self.selectAssetSource.count;
}

// 重新排序被选择的asset
- (void)ascendingWithSelectAsset {
    for (int i = 0; i<self.selectAssetSource.count; i++) {
        NvAlbumAsset *asset = self.selectAssetSource[i];
        if (asset.isShowLayer) {
            asset.number = i+1;
        }
    }
}

//判断是否全选
- (void)reserIsSelectAll {
    for (int i = 0; i < self.albumCollectionView.assetDataSource.count; i++) {
        NvAlbumItem *item = self.albumCollectionView.assetDataSource[i];
        NSMutableArray<NvAlbumAsset *> *assets = item.collectionList;
        BOOL isContains = NO;
        for (int j = 0; j < assets.count; j++) {
            NvAlbumAsset *asset = assets[j];
            isContains = [self.selectAssetSource containsObject:asset];
            if (isContains) {
                continue;
            } else {
                break;
            }
        }
        item.isSelectAll = isContains;
    }
    for (int i = 0; i < self.videoCollectionView.assetDataSource.count; i++) {
        NvAlbumItem *item = self.videoCollectionView.assetDataSource[i];
        NSMutableArray<NvAlbumAsset *> *assets = item.collectionList;
        BOOL isContains = NO;
        for (int j = 0; j < assets.count; j++) {
            NvAlbumAsset *asset = assets[j];
            isContains = [self.selectAssetSource containsObject:asset];
            if (isContains) {
                continue;
            } else {
                break;
            }
        }
        item.isSelectAll = isContains;
    }
    for (int i = 0; i < self.imageCollectionView.assetDataSource.count; i++) {
        NvAlbumItem *item = self.imageCollectionView.assetDataSource[i];
        NSMutableArray<NvAlbumAsset *> *assets = item.collectionList;
        BOOL isContains = NO;
        for (int j = 0; j < assets.count; j++) {
            NvAlbumAsset *asset = assets[j];
            isContains = [self.selectAssetSource containsObject:asset];
            if (isContains) {
                continue;
            } else {
                break;
            }
        }
        item.isSelectAll = isContains;
    }
}

// MARK: UIScrollViewDelegate
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if ((*targetContentOffset).x/SCREEN_WDITH == 0) {
        [self allContentsClick];
    } else if ((*targetContentOffset).x/SCREEN_WDITH == 1) {
        [self videoClick];
    } else {
        [self imageClick];
    }
}

// MARK: NvAlbumCollectionViewDelegate
//cell单独被选中
- (void)nvAlbumCollectionView:(NvAlbumCollectionView *)nvAlbumCollectionView selectAsset:(NvAlbumAsset *)selectAsset {
    //如果不允许多选
    if (!self.mutableSelect) {
        if (![self.selectAssetSource containsObject:selectAsset]) {
            if (self.selectAssetSource.count != 0) {
                for (int i = 0; i < self.selectAssetSource.count; i++) {
                    NvAlbumAsset *asset = self.selectAssetSource[i];
                    asset.isShowLayer = NO;
                }
                [self.selectAssetSource removeAllObjects];
            }
            [self.selectAssetSource addObject:selectAsset];
        } else {
            [self.selectAssetSource removeObject:selectAsset];
        }
        
        self.selectIndex = self.selectAssetSource.count;
        
        [self realodData];
        if ([self.delegate respondsToSelector:@selector(nvAlbumViewController:didSelectAlbumAssets:)]) {
            [self.delegate nvAlbumViewController:self didSelectAlbumAssets:self.selectAssetSource];
        }
    } else {//如果允许多选
        //如果所选的内容不包含在已选择的里面，并且已选择的个数大于最大限制个数给出回调
        if (![self.selectAssetSource containsObject:selectAsset] && self.selectAssetSource.count >= self.maxSelectCount && self.maxSelectCount!=0) {
            selectAsset.isShowLayer = NO;
            [self realodData];
            if ([self.delegate respondsToSelector:@selector(nvAlbumViewController:selectAlbumAssetsOverMaxCountLimit:)]) {
                [self.delegate nvAlbumViewController:self selectAlbumAssetsOverMaxCountLimit:self.selectAssetSource];
            }
        } else {
            [self addAsset:selectAsset removeContains:YES];
            [self reserIsSelectAll];
            [self realodData];
        }
        if ([self.delegate respondsToSelector:@selector(nvAlbumViewController:didSelectAlbumAssets:)]) {
            [self.delegate nvAlbumViewController:self didSelectAlbumAssets:self.selectAssetSource];
        }
    }
}

//点击全选
- (void)nvAlbumCollectionView:(NvAlbumCollectionView *)nvAlbumCollectionView selectAssets:(NSMutableArray <NvAlbumAsset *>*)selectAssets {
    for (NvAlbumAsset *asset in selectAssets) {
        [self addAsset:asset removeContains:NO];
    }
    
    [self reserIsSelectAll];
    [self realodData];
    
    if ([self.delegate respondsToSelector:@selector(nvAlbumViewController:didSelectAlbumAssets:)]) {
        [self.delegate nvAlbumViewController:self didSelectAlbumAssets:self.selectAssetSource];
    }
}

//点击反全选
- (void)nvAlbumCollectionView:(NvAlbumCollectionView *)nvAlbumCollectionView deselectAssets:(NSMutableArray <NvAlbumAsset *>*)selectAssets {
    for (NvAlbumAsset *asset in selectAssets) {
        [self removeAsset:asset];
    }
    
    [self reserIsSelectAll];
    [self realodData];
    
    if ([self.delegate respondsToSelector:@selector(nvAlbumViewController:didSelectAlbumAssets:)]) {
        [self.delegate nvAlbumViewController:self didSelectAlbumAssets:self.selectAssetSource];
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
