//
//  CCCollectionViewFlowLayout.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit

protocol CCCollectionViewFlowLayoutDelegate:UICollectionViewDelegateFlowLayout  {
    
    func backgroundColorForSection(section:NSInteger, collectionView:UICollectionView,layout:UICollectionViewLayout) ->UIColor
    
}


class CCCollectionViewLayoutAttributes: UICollectionViewLayoutAttributes {
    
    var backgroundColor:UIColor?
}


let CCCollectionReusableViewId = "CCCollectionReusableViewId"

class CCCollectionReusableView: UICollectionReusableView {
    
    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        super.apply(layoutAttributes)
        if(layoutAttributes .isKind(of: CCCollectionViewLayoutAttributes.classForCoder())){
            let attr:CCCollectionViewLayoutAttributes = layoutAttributes as! CCCollectionViewLayoutAttributes
            self.backgroundColor = attr.backgroundColor
        }
    }
}


class CCCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    var decorationViewAttrs:[UICollectionViewLayoutAttributes]?
    var minimumInteritemSpacings:NSArray?
    
    override init() {
        super.init()
        self.decorationViewAttrs = (NSMutableArray.init() as! [UICollectionViewLayoutAttributes])
        self.register(CCCollectionReusableView.classForCoder(), forDecorationViewOfKind: CCCollectionReusableViewId)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepare() {
        super.prepare()
        self.decorationViewAttrs?.removeAll()
        
        guard let numberOfSections = self.collectionView?.numberOfSections
        else {
            return
        }
        
        guard let delegate = self.collectionView?.delegate as? CCCollectionViewFlowLayoutDelegate
            else {
                return
        }
        
        if(numberOfSections == 0){
            return
        }
        
        for section in 0...numberOfSections-1 {
            
            let numberOfItems:NSInteger = (self.collectionView?.numberOfItems(inSection: section))!
            if(numberOfItems <= 0){
                continue
            }
            
            guard let firstItem:UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath.init(item: 0, section: section))
                else{
                    continue
            }
            
            guard let lastItem:UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: numberOfItems - 1, section: section)) else {
                    continue
            }
            
            var sectionInset = self.sectionInset
            if let inset:UIEdgeInsets = delegate.collectionView?(self.collectionView!, layout: self, insetForSectionAt: section){
                sectionInset = inset
            }
           
            var sectionFrame = firstItem.frame.union(lastItem.frame)
            sectionFrame.origin.x = 0
            sectionFrame.origin.y -= sectionInset.top
            
            if self.scrollDirection == .horizontal {
                sectionFrame.size.width += sectionInset.left + sectionInset.right
                sectionFrame.size.height = self.collectionView!.frame.height
            } else {
                sectionFrame.size.width = self.collectionView!.frame.width
                sectionFrame.size.height += sectionInset.top + sectionInset.bottom
            }
            
            let attr:CCCollectionViewLayoutAttributes = CCCollectionViewLayoutAttributes.init(forDecorationViewOfKind: CCCollectionReusableViewId, with: IndexPath.init(item: 0, section: section))
            attr.frame = sectionFrame
            attr.zIndex = -1
            attr.backgroundColor = delegate.backgroundColorForSection(section: section, collectionView: self.collectionView!, layout: self)
            
            self.decorationViewAttrs?.append(attr)
        }
    }
    

    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        var attributes = super.layoutAttributesForElements(in: rect)
        for attr:UICollectionViewLayoutAttributes in self.decorationViewAttrs!{
            if(rect.intersects(attr.frame)){
               attributes?.append(attr)
            }
        }
        return attributes
    }
    
    override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        if(elementKind == CCCollectionReusableViewId){
            return self.decorationViewAttrs![indexPath.section]
        }
        else{
            return super.layoutAttributesForDecorationView(ofKind: elementKind , at: indexPath)
        }
    }
}

