//
//  BoPhotoGroupView.m
//  PhotoPicker
//
//  Created by AlienJunX on 15/11/2.
//  Copyright © 2015年 com.alienjun.demo. All rights reserved.
//

#import "AJPhotoGroupView.h"
#import "AJPhotoGroupCell.h"

@interface AJPhotoGroupView()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation AJPhotoGroupView

#pragma mark - init
- (instancetype)init {
    self = [super init];
    if (self) {
        [self initCommon];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self initCommon];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initCommon];
    }
    return self;
}

- (void)initCommon {
    self.delegate = self;
    self.dataSource = self;
    [self registerClass:[AJPhotoGroupCell class] forCellReuseIdentifier:@"cell"];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.backgroundColor = [UIColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1.0];
}

- (void)setupGroup {
    [self.albumGroups removeAllObjects];
    
    BOOL canGetVideo = NO;
    BOOL canGetPhoto = NO;
    
    if(_isPhotoType){
        canGetPhoto = YES;
    }
    else{
        canGetVideo = YES;
    }
    
    [[TZImageManager manager] getAllAlbums:canGetVideo allowPickingImage:canGetPhoto needFetchAssets:NO completion:^(NSArray<TZAlbumModel *> *models) {
        
        self->_albumGroups = [[NSMutableArray alloc] initWithArray:models];
        [self dataReload];
    }];

}

#pragma mark - Reload Data
- (void)dataReload {
    if (self.albumGroups.count == 0)
        //没有图片
        [self showNoAssets];
    
    if (self.albumGroups.count >0 && [_my_delegate respondsToSelector:@selector(didSelectGroup:)]) {
        [_my_delegate didSelectGroup:self.albumGroups[0]];
    }
    [self reloadData];
}

#pragma mark - Not allowed / No assets
- (void)showNotAllowed {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotAllowedPhoto" object:nil];
    if ([_my_delegate respondsToSelector:@selector(didSelectGroup:)]) {
        [_my_delegate didSelectGroup:nil];
    }
}

- (void)showNoAssets {
    NSLog(@"%s",__func__);
}


#pragma mark - uitableviewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifer = @"cell";
    AJPhotoGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
    if(cell == nil){
        cell = [[AJPhotoGroupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }
    
    [cell bindModel:[self.albumGroups objectAtIndex:indexPath.row]];

    if (indexPath.row == self.selectIndex) {
        cell.backgroundColor = [UIColor hwColor:@"#EEFBFF" alpha:1.0];
        cell.selectButton.selected = YES;
    }
    return cell;
}

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectIndex = indexPath.row;
    [self reloadData];
    TZAlbumModel *model = [self.albumGroups objectAtIndex:indexPath.row];
    if ([_my_delegate respondsToSelector:@selector(didSelectGroup:)]) {
        [_my_delegate didSelectGroup:model];
    }
}

#pragma mark - getter/setter
- (NSMutableArray *)albumGroups {
    if (!_albumGroups) {
        _albumGroups = [[NSMutableArray alloc] init];
    }
    return _albumGroups;
}

@end
