//
//  PQStuckPointMusicTagsCell.swift
//  PQSpeed
//
//  Created by SanW on 2021/4/29.
//  Copyright © 2021 BytesFlow. All rights reserved.
//

import BFMediaKit
import UIKit

class PQStuckPointMusicTagsCell: UICollectionViewCell {
    /// 点击标签
    var tagsDidSelectedHandle: ((_ indexPath: IndexPath, _ tagData: PQStuckPointMusicTagsModel?) -> Void)?

    lazy var tagsFlowLayout: PQStuckPointMusciTagsFlowLayout = {
        let tagsFlowLayout = PQStuckPointMusciTagsFlowLayout()
        tagsFlowLayout.sectionInset = UIEdgeInsets.zero
        tagsFlowLayout.minimumLineSpacing = 8
        tagsFlowLayout.minimumInteritemSpacing = 8
        tagsFlowLayout.scrollDirection = .horizontal
        return tagsFlowLayout
    }()

    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), collectionViewLayout: tagsFlowLayout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.clear
        collectionView.register(PQStuckPointMusicTagsContentCell.self, forCellWithReuseIdentifier: String(describing: PQStuckPointMusicTagsContentCell.self))
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }
        // 延迟scrollView上子视图的响应，所以当直接拖动UISlider时，如果此时touch时间在150ms以内，UIScrollView会认为是拖动自己，从而拦截了event，导致UISlider接收不到滑动的event
        collectionView.delaysContentTouches = false
        return collectionView
    }()

    @objc class func stuckPointMusicTagsCell(collectionView: UICollectionView, indexPath: IndexPath) -> PQStuckPointMusicTagsCell {
        let cell: PQStuckPointMusicTagsCell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQStuckPointMusicTagsCell.self), for: indexPath) as! PQStuckPointMusicTagsCell
        return cell
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(collectionView)
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 所有分类数据
    var itemData: ([PQStuckPointMusicTagsModel], ([UICollectionViewLayoutAttributes], CGFloat)) = ([PQStuckPointMusicTagsModel].init(), ([UICollectionViewLayoutAttributes].init(), CGFloat())) {
        didSet {
            collectionView.frame = CGRect(x: 16, y: 25, width: frame.width - 32, height: frame.height - 25)
            tagsFlowLayout.layoutAttributesArray = itemData.1.0
            tagsFlowLayout.maxH = itemData.1.1
            collectionView.reloadData()
        }
    }
}

// MARK: - 标签cell相关代理

/// 标签cell相关代理
extension PQStuckPointMusicTagsCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
        return itemData.0.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = PQStuckPointMusicTagsContentCell.stuckPointMusicTagsContentCell(collectionView: collectionView, indexPath: indexPath)
        cell.tagData = itemData.0[indexPath.item]
        return cell
    }

    func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        itemData.0.forEach { item in
            item.isSelected = false
        }
        itemData.0[indexPath.item].isSelected = true
        collectionView.reloadData()
        if tagsDidSelectedHandle != nil {
            tagsDidSelectedHandle!(indexPath, itemData.0[indexPath.item])
        }
    }
}
