//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class ListFlowLayout: UICollectionViewFlowLayout {
	static let alphaForMove: CGFloat = 0.7
	
	override func prepare() {
		itemSize.width = widthForRow(forBounds: collectionView!.bounds, collectionView: collectionView)
		super.prepare()
	}
	
	override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
		if newBounds.size.equalTo(collectionView!.bounds.size) {
			return false
		}
		itemSize.width = widthForRow(forBounds: newBounds, collectionView: collectionView)
		return true
	}
	
	public func widthForRow(forBounds bounds:CGRect, collectionView: UICollectionView?) -> CGFloat {
		guard let cV = collectionView else {
			return 0.0
		}
		let contentInsets = cV.contentInset
		return bounds.width - sectionInset.left - sectionInset.right - contentInsets.left - contentInsets.right
	}

	override func layoutAttributesForInteractivelyMovingItem(at indexPath: IndexPath, withTargetPosition position: CGPoint) -> UICollectionViewLayoutAttributes {
		let attributes = super.layoutAttributesForInteractivelyMovingItem(at: indexPath, withTargetPosition: position)
		
		attributes.alpha = ListFlowLayout.alphaForMove
		
		return attributes
	}
}
