//
//  YMMineHeadFunctionItemLayout.m
//  YUMI
//
//  Created by YUMI on 2022/8/4.
//

#import "XPMineHeadFunctionItemLayout.h"

@interface XPMineHeadFunctionItemLayout ()
@property (nonatomic, copy) NSMutableDictionary *sectionDic;
@property (nonatomic, strong) NSMutableArray *allAttributes;
@end

@implementation XPMineHeadFunctionItemLayout

#pragma mark - life cycle
- (instancetype)init {
    self = [super init];
    if (self) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return self;
}

#pragma mark - overload

- (void)prepareLayout {
    
    [super prepareLayout];
    
    _sectionDic = [NSMutableDictionary dictionary];
    self.allAttributes = [NSMutableArray array];
    //获取section的数量
    NSUInteger section = [self.collectionView numberOfSections];
    
    for (int sec = 0; sec < section; sec++) {
        //获取每个section的cell个数
        NSUInteger count = [self.collectionView numberOfItemsInSection:sec];

        for (NSUInteger item = 0; item<count; item++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:sec];
            //重新排列
            UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
            [self.allAttributes addObject:attributes];
        }
//        NSInteger sectionCount = [self.collectionView numberOfSections];
//        for (int i = 0; i<sectionCount; i++) {
//            NSInteger count = [self.collectionView numberOfItemsInSection:i];
//            for (int index = 0; index<count; index++) {
//                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:i];
//                UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
//                [self.allAttributes addObject:attributes];
//            }
//        }
    }
}

- (CGSize)collectionViewContentSize {
    
    //每个section的页码的总数
    NSInteger actualLo = 0;
    for (NSString *key in [_sectionDic allKeys]) {
        actualLo += [_sectionDic[key] integerValue];
    }
    return CGSizeMake(actualLo*self.collectionView.frame.size.width, self.collectionView.contentSize.height);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attr = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
    [self applyLayoutAttributes:attr];
//    NSInteger itemNew = indexPath.item;
//    NSInteger itemOld = itemNew;
//    switch (itemNew) {
//        case 0:
//            itemOld = 0;
//            break;
//        case 1:
//            itemOld = 2;
//            break;
//        case 2:
//            itemOld = 4;
//            break;
//        case 3:
//            itemOld = 6;
//            break;
//        case 4:
//            itemOld = 1;
//            break;
//        case 5:
//            itemOld = 3;
//            break;
//        case 6:
//            itemOld = 5;
//            break;
//        case 7:
//            itemOld = 7;
//            break;
//        default:
//            break;
//    }
//    UICollectionViewLayoutAttributes *oldAttributes = [super layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:itemOld inSection:indexPath.section]].copy;
    return attr;
//    let itemNew = indexPath.row
//           var itemOld = itemNew
//           switch itemNew {
//           case 0:
//               itemOld = 0
//           case 1:
//               itemOld = 2
//           case 2:
//               itemOld = 4
//           case 3:
//               itemOld = 6
//           case 4:
//               itemOld = 1
//           case 5:
//               itemOld = 3
//           case 6:
//               itemOld = 5
//           case 7:
//               itemOld = 7
//           default:
//               itemOld = itemNew
//           }
//           let attribute = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes
//           let oldAttributes = super.layoutAttributesForItem(at: IndexPath(item: itemOld, section: indexPath.section))
//           attribute?.frame = oldAttributes?.frame ?? CGRect.zero
//           return attribute
//    return attr;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return self.allAttributes;
}

#pragma mark - private method

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes {
   
    
    if(attributes.representedElementKind != nil){
        return;
    }
    CGFloat minLine = self.minimumLineSpacing;
    CGFloat minSpacing = self.minimumInteritemSpacing;

    //attributes 的宽度
    CGFloat itemW = attributes.frame.size.width;
    //attributes 的高度
    CGFloat itemH = attributes.frame.size.height;
    UIEdgeInsets sectionInsets = self.sectionInset;
    //collectionView 的宽度
    CGFloat width = self.collectionView.frame.size.width;
    //collectionView 的高度
    CGFloat height = self.collectionView.frame.size.height;
    //每个attributes的下标值 从0开始
    NSInteger itemIndex = attributes.indexPath.item;

    CGFloat stride = (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) ? width : height;


    //获取现在的attributes是第几组
    NSInteger section = attributes.indexPath.section;
    //获取每个section的item的个数
    NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];


    CGFloat offset = section * stride;

    //计算x方向item个数
    NSInteger xCount = (width / itemW);
    //计算y方向item个数
    NSInteger yCount = (height / itemH);
    //计算一页总个数
    NSInteger allCount = (xCount * yCount);
    //获取每个section的页数，从0开始
    NSInteger page = itemIndex / allCount;

    //余数，用来计算item的x的偏移量
    NSInteger remain = (itemIndex % xCount);
    //取商，用来计算item的y的偏移量
    NSInteger merchant = (itemIndex-page*allCount)/xCount;


    //x方向每个item的偏移量
    CGFloat xCellOffset = remain * (itemW + minLine)+ sectionInsets.left;
    //y方向每个item的偏移量
    CGFloat yCellOffset = merchant * (itemH + minSpacing);

    //获取每个section中item占了几页
    NSInteger pageRe = (itemCount % allCount == 0)? (itemCount / allCount) : (itemCount / allCount) + 1;
    //将每个section与pageRe对应，计算下面的位置
    [_sectionDic setValue:@(pageRe) forKey:[NSString stringWithFormat:@"%ld", section]];

    if(self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {

        NSInteger actualLo = 0;
        //将每个section中的页数相加
        for (NSString *key in [_sectionDic allKeys]) {
            actualLo += [_sectionDic[key] integerValue];
        }
        //获取到的最后的数减去最后一组的页码数
        actualLo -= [_sectionDic[[NSString stringWithFormat:@"%ld", [_sectionDic allKeys].count-1]] integerValue];
        xCellOffset += page*width + actualLo*width;

    } else {

        yCellOffset += offset;
    }

    attributes.frame = CGRectMake(xCellOffset, yCellOffset, itemW, itemH);
}


@end
