//
//  MPPositive_ArtistShowListableViewCell.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/16.
//

import UIKit

class MPPositive_ArtistShowListableViewCell: UITableViewCell {
    ///艺术家Layout类型
    enum ArtistType:Int {
        case List = 0
        case Artist = 1
        var layout:UICollectionViewFlowLayout {
            let layout = UICollectionViewFlowLayout()
            layout.itemSize = .init(width: 164*width, height: 223*width)
            switch self {
            case .List:
                layout.sectionInset = .init(top: 15*width, left: 18*width, bottom: 40*width, right: 18*width)
                layout.minimumInteritemSpacing = 10*width
                layout.minimumLineSpacing = 16*width
                layout.scrollDirection = .vertical
            case .Artist:
                layout.sectionInset = .init(top: 15*width, left: 18*width, bottom: 40*width, right: 18*width)
                layout.minimumLineSpacing = 10*width
                layout.scrollDirection = .horizontal
            }
            return layout
        }
        
    }
    //展示CollectionViewCell
    private lazy var collectionView:UICollectionView = {
        let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: showType.layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(MPPositive_ArtistShowListCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_ArtistShowListCollectionViewCellID)
        collectionView.register(MPPositive_ArtistShowCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_ArtistShowCollectionViewCellID)
        return collectionView
    }()
    private let MPPositive_ArtistShowListCollectionViewCellID = "MPPositive_ArtistShowListCollectionViewCell"
    private let MPPositive_ArtistShowCollectionViewCellID = "MPPositive_ArtistShowCollectionViewCell"
    var itemViews:[MPPositive_BrowseItemViewModel]!
    ///列表展示类型
    var showType:ArtistType = .List{
        didSet{
            collectionView.collectionViewLayout = showType.layout
            collectionView.reloadData()
        }
    }
    //选中内容
    var chooseItemBlock:((MPPositive_BrowseItemViewModel) -> Void)?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    private func configure() {
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-1*width).priority(999)
        }
    }
    //经过计算设置tableViewCell真实高度
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        collectionView.layoutIfNeeded()
        switch showType {
        case .List:
            //纵向
            let height = collectionView.collectionViewLayout.collectionViewContentSize.height
            return CGSize(width: size.width, height: size.height + height)
        case .Artist:
            //横向
            let height = showType.layout.itemSize.height + showType.layout.sectionInset.top + showType.layout.sectionInset.bottom
            return CGSize(width: size.width, height: size.height + height)
        }
    }
}
//MARK: - tableView
extension MPPositive_ArtistShowListableViewCell:UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemViews?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch showType {
        case .List:
            //纵向
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_ArtistShowListCollectionViewCellID, for: indexPath) as! MPPositive_ArtistShowListCollectionViewCell
            cell.itemView = itemViews[indexPath.row]
            return cell
        case .Artist:
            //横向
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_ArtistShowCollectionViewCellID, for: indexPath) as! MPPositive_ArtistShowCollectionViewCell
            cell.itemView = itemViews[indexPath.row]
            return cell
        }
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if chooseItemBlock != nil {
            chooseItemBlock!(itemViews[indexPath.row])
        }
    }
}
