//
//  GetAlbums.m
//  nativeModules
//
//  Created by 398403854@qq.com陈伟 on 2017/3/31.
//  Copyright © 2017年 drwine. All rights reserved.
//

#import "GetAlbums.h"
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>

@implementation GetAlbums


RCT_EXPORT_MODULE(GetPicturesNative);

RCT_EXPORT_METHOD(getAlbums:(RCTResponseSenderBlock)callback)
{
    NSMutableArray *photoAblumList = [[NSMutableArray alloc]init];
    
    //获取所有智能相册
    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    [smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
        //过滤掉视频和最近删除
        if (!([collection.localizedTitle isEqualToString:@"Recently Deleted"] ||
              [collection.localizedTitle isEqualToString:@"Videos"])) {
            
            [photoAblumList addObject:collection.localizedTitle];
            
//            NSMutableArray<PHAsset *> *assets = [NSMutableArray array];
            
//            PHFetchOptions *option = [[PHFetchOptions alloc] init];
//            //ascending 为YES时，按照照片的创建时间升序排列;为NO时，则降序排列
//            option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//            
//            PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option];
//            
//            [result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                PHAsset *asset = (PHAsset *)obj;
//                [assets addObject:asset];
//            }];
        }
    }];
    
    //获取用户创建的相册
    PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
    [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    [photoAblumList addObject:collection.localizedTitle];
    }];
    
    callback(@[photoAblumList]);
}

@end
