//
//  RepairAddPicTableViewCell.m
//  patrol
//
//  Created by Javen on 2016/10/23.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "InspectRepairAddPicTableViewCell.h"
#import "RepairAddPicCollectionViewCell.h"
#import "TZImagePickerController.h"
#import "GalleryViewController.h"
@interface InspectRepairAddPicTableViewCell () <UICollectionViewDelegate,
                                                UICollectionViewDataSource>

@end
@implementation InspectRepairAddPicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.layOut.itemSize = kPicCellSize;
    self.layOut.minimumInteritemSpacing = 10;
    self.layOut.minimumLineSpacing = 10;
    self.layOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // Initialization code
}

- (void)configCellWithViewModel:(PicViewModel *)viewModel indexPath:(NSIndexPath *)indexPath target:(UIViewController *)target {
    self.vc = target;
    self.viewModel = viewModel;
    //如果是只读的，则直接刷新。
    if (self.viewModel.type == kPicCellTypeRead) {
        [self.collectionView reloadData];
        return;
    }

    //非只读情况，则是添加或者编辑
    if (self.viewModel.arrPics.count == kMaxAddRepairImgNumber) {
        self.viewModel.type = kPicCellTypeEdit;
    } else {
        self.viewModel.type = kPicCellTypeAdd;
    }
    [self.collectionView reloadData];
}

#pragma mark - collectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (self.viewModel.type) {
        case kPicCellTypeAdd: {
            return self.viewModel.arrPics.count + 1;
            break;
        }
        default: {
            return self.viewModel.arrPics.count;
            break;
        }
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RepairAddPicCollectionViewCell *cell = [collectionView
        dequeueReusableCellWithReuseIdentifier:@"RepairAddPicCollectionViewCell"
                                  forIndexPath:indexPath];
    [cell configCellWithArr:self.viewModel.arrPics indexPath:indexPath];
    //删除
    cell.blockDelete = ^(NSInteger index) {

    };
    return cell;
}

#pragma mark - collectionView DataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    switch (self.viewModel.type) {
        case kPicCellTypeRead: {
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"ZJPicture" bundle:nil];
            GalleryViewController *galleryVC = [board instantiateViewControllerWithIdentifier:@"GalleryViewController"];
            galleryVC.arrData = [self.viewModel.arrPics mutableCopy];
            galleryVC.page = indexPath.item;
            [self.vc.navigationController pushViewController:galleryVC animated:YES];
            break;
        }
        default: {
            if (indexPath.row == self.viewModel.arrPics.count) { //说明点击的是添加图片的那个按钮
                [self pushImagePickerController];
            } else { //点击查看大图
                [self seePicsWithIndex:indexPath.item];
            }
            break;
        }
    }
}

#pragma mark - TZImagePickerController

- (void)pushImagePickerController {
    CGFloat canSelectNum = kMaxAddRepairImgNumber;
    TZImagePickerController *imagePickerVc =
        [[TZImagePickerController alloc] initWithMaxImagesCount:canSelectNum - self.viewModel.arrPics.count delegate:nil];
    imagePickerVc.isSelectOriginalPhoto = NO;
    imagePickerVc.navigationBar.barTintColor = kMainColor;
    imagePickerVc.oKButtonTitleColorNormal = kMainColor;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.showSelectBtn = YES;
    WS(weakSelf);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf.viewModel.arrPics addObjectsFromArray:photos];
        weakSelf.blockReloadData();
//        [weakSelf.collectionView reloadData];
    }];

    [self.vc presentViewController:imagePickerVc animated:YES completion:nil];
}

/**
 查看大图

 @param index 点击中的图片下标
 */
- (void)seePicsWithIndex:(NSInteger)index {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"ZJPicture" bundle:nil];
    GalleryViewController *galleryVC = [board instantiateViewControllerWithIdentifier:@"GalleryViewController"];
    galleryVC.arrData = self.viewModel.arrPics;
    galleryVC.page = index;
    galleryVC.type = GalleryTypeEdit;
    [self.vc.navigationController pushViewController:galleryVC animated:YES];
    WS(weakSelf);
    galleryVC.blockDelete = ^(NSInteger index) {

        [weakSelf.collectionView reloadData];
        weakSelf.blockReloadData();
    };
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
