//
//  AJPhotoListCell.m
//  AJPhotoPicker
//
//  Created by AlienJunX on 15/11/2.
//  Copyright (c) 2015 AlienJunX
//
//  This source code is licensed under the MIT-style license found in the
//  LICENSE file in the root directory of this source tree.
//

#import "AJPhotoListCell.h"
#import <AssetsLibrary/AssetsLibrary.h>


@interface AJPhotoListCell()
@property (strong, nonatomic) UIImageView *imageView;

@end

@implementation AJPhotoListCell

- (void)bind:(TZAssetModel *)model selectionFilter:(NSPredicate*)selectionFilter isSelected:(BOOL)isSelected {
    
    if (self.imageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        [self.contentView addSubview:imageView];
        self.imageView = imageView;
        
        [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
        self.imageView.layer.cornerRadius = 3;
        self.imageView.clipsToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
    }
    
    int32_t imageRequestID = [[TZImageManager manager] getPhotoWithAsset:model.asset photoWidth:self.frame.size.width completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
        // Set the cell's thumbnail image if it's still showing the same asset.
        self.imageView.image = photo;
        [self setNeedsLayout];
        
    } progressHandler:nil networkAccessAllowed:NO];
    
    
    if (self.selectButton == nil) {
        UIButton *but = [[UIButton alloc] init];
        [but setImage:[UIImage imageNamed:@"upload_file_uncheck2"] forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
        [self.contentView addSubview:but];
        [but addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
        
        //but.backgroundColor = [UIColor greenColor];
        //but.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -10 - 25, 0, 25, 25);
        [but mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(30);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
        
        self.selectButton = but;
    }
    
    self.selectButton.selected = isSelected;
   
}

- (void)isSelected:(BOOL)isSelected {
    //_tapAssetView.selected = isSelected;
    self.selectButton.selected = isSelected;
}

#pragma mark - Utility
//时间格式化
- (NSString *)timeFormatted:(double)totalSeconds {
    NSTimeInterval timeInterval = totalSeconds;
    long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
    int hour = 0;
    int minute = seconds / 60.0f;
    int second = seconds % 60;
    if (minute > 59) {
        hour = minute / 60;
        minute = minute % 60;
        return [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];
    } else {
        return [NSString stringWithFormat:@"%02d:%02d", minute, second];
    }
}

#pragma mark 选中按钮
- (void)didClickButFun:(UIButton*)but
{
    //but.selected = !but.selected;
    if(_didClckSelectBut){
        _didClckSelectBut(but.selected);
    }
}
@end
