//
//  ICPhotoLocationViewController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/6.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICPhotoLocationViewController.h"
#import "WKClearPhotoManager.h"
#import "ICLocationPhotoListViewCell.h"
#import "ICNormalPhotoViewController.h"
#import "UIView+DHExtension.h"

@interface ICPhotoLocationViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WKClearPhotoManagerDelegate>{
    BOOL first;
}

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) WKClearPhotoManager *photoMgr;

@end

@implementation ICPhotoLocationViewController

- (WKClearPhotoManager *)photoMgr {
    if (!_photoMgr) {
        _photoMgr = [WKClearPhotoManager shareManager];
    }
    return _photoMgr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = ICLocalized(@"Place"); //@"地点";
    
    if (self.photoMgr.locationArray.count <= 0) {
        [self.view addEmptyDataSet:0 image:[UIImage imageNamed:@"global_empty_set"] title: ICLocalized(@"EmptyPlacePhoto")];//@"无地点照片"];
        return;
    } else {
        [self.view removeEmptyDataSet:true];
    }
    [self.collectionView reloadData];
    first = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (first) {
        first = NO;
    }else{
        [self.collectionView reloadData];
    }
}

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

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ICLocationPhotoListViewCell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"ICLocationPhotoListViewCell"
                                              forIndexPath:indexPath];
    NSDictionary *dict = self.photoMgr.locationArray[indexPath.row];
    cell.countryLabel.text = dict.allKeys.firstObject;
    NSArray *itemArray = dict.allValues.firstObject;
    cell.countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)itemArray.count];
    ICBasePhotoModel *model = itemArray.firstObject;
    cell.coverImageView.image = model.image;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ICNormalPhotoViewController *vc = [ICNormalPhotoViewController new];
    
    NSMutableArray *items = [NSMutableArray array];
    NSDictionary *dict = self.photoMgr.locationArray[indexPath.row];
    for (NSArray *models in dict.allValues) {
        [items addObjectsFromArray:models];
    }
    vc.currentlocationGroudIndex = indexPath.row;
    vc.similarArr = items;
    vc.photoType = LocationPhoto;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemCount = 2;
        CGFloat distance = 15;
        CGFloat width = self.view.frame.size.width;
        CGFloat itemWH = (width - distance * (itemCount + 1)) / itemCount - 1;
        layout.itemSize = CGSizeMake(itemWH, itemWH + 60);
        layout.sectionInset = UIEdgeInsetsMake(distance, distance, distance, distance);
        layout.minimumLineSpacing = distance;
        layout.minimumInteritemSpacing = distance;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
                                             collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"ICLocationPhotoListViewCell" bundle:nil] forCellWithReuseIdentifier:@"ICLocationPhotoListViewCell"];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_collectionView];
        
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _collectionView.delaysContentTouches = NO;
        if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
            if ([UIScreen mainScreen].bounds.size.height == 812) {
                if (@available(iOS 11.0, *)) {
                    _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
                } else {
                    // Fallback on earlier versions
                }
                CGFloat h1 = [UIApplication sharedApplication].statusBarFrame.size.height;
                CGFloat h2 = self.navigationController.navigationBar.bounds.size.height;
                _collectionView.contentInset = UIEdgeInsetsMake(h1 + h2, 0, 0, 0);
                _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(h1 + h2, 0, 0, 0);
            }
        }
        UIEdgeInsets inset = _collectionView.contentInset;
        _collectionView.contentInset = UIEdgeInsetsMake(inset.top, inset.left, 50, inset.right);
        _collectionView.scrollIndicatorInsets = _collectionView.contentInset;
    }
    return _collectionView;
}

@end
