//
//  ICPhotoViewerController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/5.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICPhotoViewerController.h"
#import "ICBigPhotoViewerCell.h"
#import "ICPvwPhotoViewerCell.h"
#import "ICPhotoViewerController.h"

@interface ICPhotoViewerController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (weak, nonatomic) IBOutlet UICollectionView *pvwCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *pvwFlowLayout;
@property (nonatomic,assign) BOOL isClick;
@property (nonatomic,assign) BOOL isPvwViewHidden;

@property (nonatomic, strong) PHImageRequestOptions *imageOpt;

@end

@implementation ICPhotoViewerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.itemSize  = [UIScreen mainScreen].bounds.size;
    
    self.pvwFlowLayout.minimumLineSpacing = 10;
    self.pvwFlowLayout.minimumInteritemSpacing = 0;
    self.pvwFlowLayout.itemSize  = CGSizeMake(50, 50);
    self.pvwCollectionView.contentInset = UIEdgeInsetsMake(0, ([UIScreen mainScreen].bounds.size.width - 50)/2, 0, ([UIScreen mainScreen].bounds.size.width - 50)/2);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.currentIndex) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
            [self.pvwCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        }
    });

    switch (_photoType) {
        case SimilayNormalPhoto:
            self.navigationItem.title = ICLocalized(@"SimilarMotionPhoto"); //@"相似照片";
            break;
        case SimilayLivePhoto:
            self.navigationItem.title = ICLocalized(@"SimilarPhoto"); //@"类似动态照片";
            break;
        case ScreenShotPhoto:
            self.navigationItem.title = ICLocalized(@"ScreenCapture"); //@"屏幕截图";
            break;
        case LocationPhoto:
            self.navigationItem.title = ICLocalized(@"Place"); //@"地点";
            break;
        default:
            self.navigationItem.title = @"";
            break;
    }
    
}

#pragma UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.itemSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.collectionView) {
        ICBigPhotoViewerCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"ICBigPhotoViewerCell" forIndexPath:indexPath];
        
        ICBasePhotoModel *item = self.itemSource[indexPath.row];
        cell.bigImageView.image = item.image;
        cell.selectedButton.selected = item.isSelected;
        __weak typeof(self) weakSelf = self;
        cell.refreshViewDataClosre = ^(BOOL isSelected) {
            item.isSelected = isSelected;
            if (weakSelf.updatePreViewListCallBack) {
                weakSelf.updatePreViewListCallBack(item);
            }
            [self.pvwCollectionView reloadData];
        };
        return cell;
    }
    ICPvwPhotoViewerCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"ICPvwPhotoViewerCell" forIndexPath:indexPath];
    ICBasePhotoModel *item = self.itemSource[indexPath.row];
    cell.pvwImageView.image = item.image;
    cell.coverView.hidden = !item.isSelected;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionView) {
        ICBasePhotoModel *item = self.itemSource[indexPath.row];
        ICBigPhotoViewerCell *bigCell = (ICBigPhotoViewerCell *)cell;
        if (item.imageData) {
            bigCell.bigImageView.image = [UIImage imageWithData:item.imageData];
        } else {
            bigCell.bigImageView.image = item.image; 
        }
        bigCell.selectedButton.selected = item.isSelected;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionView) {
        return [UIScreen mainScreen].bounds.size;
    } else {
        return CGSizeMake(50, 50);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _pvwCollectionView) {
//        if (indexPath) {
            [self.pvwCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:false];
//        }
        self.isClick = true;
    } else {
        __weak typeof (self)weakSelf = self;
        self.isPvwViewHidden = !self.isPvwViewHidden;
        [UIView animateWithDuration:0.25 animations:^{
            if (weakSelf.isPvwViewHidden) {
                [weakSelf.navigationController setNavigationBarHidden:true animated:true];
                weakSelf.pvwCollectionView.transform = CGAffineTransformMakeTranslation(0, 100);
            } else {
                weakSelf.pvwCollectionView.transform = CGAffineTransformMakeTranslation(0, 0);
                [weakSelf.navigationController setNavigationBarHidden:false animated:true];
            }
        }];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _pvwCollectionView && !self.isClick) {
        CGPoint p = [self.view convertPoint:_pvwCollectionView.center toView:_pvwCollectionView];
        NSIndexPath *index = [_pvwCollectionView indexPathForItemAtPoint:p];
        [self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionNone animated:false];
    }
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == _collectionView) {
        self.isClick = true;
        NSIndexPath *index = [self.collectionView indexPathsForVisibleItems].firstObject;
        [self.pvwCollectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    self.isClick = false;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (scrollView == _pvwCollectionView) {
        
        NSIndexPath *index = [self.collectionView indexPathsForVisibleItems].firstObject;
        [_pvwCollectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
    }
}

- (PHImageRequestOptions *)imageOpt {
    if (!_imageOpt) {
        _imageOpt = [[PHImageRequestOptions alloc] init];
        // resizeMode 属性控制图像的剪裁
        _imageOpt.resizeMode = PHImageRequestOptionsResizeModeNone;
        // deliveryMode 则用于控制请求的图片质量
        _imageOpt.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    }
    return _imageOpt;
}

@end
