//
//  FeedAddImageView.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2020/9/10.
//  Copyright © 2020 雷俊博. All rights reserved.
//

#import "FeedAddImageView.h"
#import "LQPhotoViewCell.h"
#import "CLAmplifyView.h"

#define MAXImageCount 4

@interface FeedAddImageView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate, UINavigationControllerDelegate>



@property (weak, nonatomic) IBOutlet UILabel *addImgStrLabel;


@end

@implementation FeedAddImageView
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.imagesArray = [NSMutableArray array];
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LQPhotoViewCell" bundle:nil] forCellWithReuseIdentifier:@"LQPhotoViewCell"];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.imagesArray.count < 4) {
        return self.imagesArray.count + 1;
    }
    
    return self.imagesArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LQPhotoViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LQPhotoViewCell" forIndexPath:indexPath];
    
    if (indexPath.row == self.imagesArray.count) {
        cell.profilePhoto.image = [UIImage imageNamed:@"plus"];
        cell.closeButton.hidden = YES;
        //没有任何图片
        if (self.imagesArray.count == 0) {
            self.addImgStrLabel.hidden = NO;
        }
        else{
            self.addImgStrLabel.hidden = YES;
        }
        
        cell.profilePhoto.hidden = MAXImageCount== self.imagesArray.count;
        cell.profilePhoto.userInteractionEnabled = NO;
        
    }else{
        [cell.profilePhoto setImage:self.imagesArray[indexPath.item]];
        cell.closeButton.hidden = NO;
        cell.closeButton.tag = 200 + indexPath.item;
        [cell.closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.profilePhoto.userInteractionEnabled = YES;
        UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        [cell.profilePhoto addGestureRecognizer:tap];
        
    }
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width-64) /4 ,([UIScreen mainScreen].bounds.size.width-64) /4);
}

//定义每个UICollectionView 的 margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 8, 15, 8);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ((indexPath.item == self.imagesArray.count)  && (self.imagesArray.count < 4)) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(addImage)]) {
            [self.delegate addImage];
        }
    }
}

#pragma mark - event response
- (void)closeButtonClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 200;
    if (self.delegate && [self.delegate respondsToSelector:@selector(deleteImgWithIndex:)]) {
        [self.delegate deleteImgWithIndex:index];
    }
}
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    CLAmplifyView *amplifyView = [[CLAmplifyView alloc]initWithFrame:[UIScreen mainScreen].bounds andGesture:tap andSuperView:self.superview.superview.superview];
     [[UIApplication sharedApplication].keyWindow addSubview:amplifyView];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
