//
//  GalleryViewController.m
//  vanke
//
//  Created by Z on 16/7/18.
//  Copyright © 2016年 gomore. All rights reserved.
//

#import "ZJBaseFileModel.h"
#import "GalleryCollectionViewCell.h"
#import "GalleryViewController.h"
@interface GalleryViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@end

@implementation GalleryViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.flowLayout.itemSize = CGSizeMake(kWidth, kHeight);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    switch (self.type) {
        case GalleryTypeReadOnly: {
            break;
        }
        case GalleryTypeEdit: {
            UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deletePic)];
            self.navigationItem.rightBarButtonItem = rightBtn;
            break;
        }
    }
}

- (void)deletePic
{
    WS(weakSelf);
    [self alertTitle:@"确认删除这张图片？"
                 msg:nil
            okAction:^(UIAlertAction *action) {
                weakSelf.page = weakSelf.collectionView.contentOffset.x / weakSelf.view.frame.size.width;
                [weakSelf.arrData removeObjectAtIndex:weakSelf.page];
                if (self.arrData.count == 0) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                } else {
                    NSIndexPath *index = [NSIndexPath indexPathForRow:weakSelf.page inSection:0];
                    [weakSelf.collectionView deleteItemsAtIndexPaths:@[ index ]];

                }
                //删除之后的回调，这里的数字是随便传的
                weakSelf.blockDelete(0);

            }
        cancelAction:nil];
}

- (void)viewDidLayoutSubviews
{
    if (self.page < 0) {
        return;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.page inSection:0]
                                atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                        animated:NO];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalleryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];
    [cell configCellWithArray:self.arrData indexPath:indexPath];
    
    self.title = [NSString stringWithFormat:@"(%ld/%.0lu)", indexPath.row + 1, (unsigned long)self.arrData.count];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.title = [NSString stringWithFormat:@"(%.0f/%.0lu)", scrollView.contentOffset.x / kWidth + 1, (unsigned long)self.arrData.count];
    self.page = scrollView.contentOffset.x / self.view.frame.size.width;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
