//
//  MPPositive_HomeLibraryListstableViewCell.swift
//  relax.offline.mp3.music
//
//  Created by Mr.Zhou on 2024/7/24.
//

import UIKit
///首页曲库内容展示Cell
class MPPositive_HomeLibraryListstableViewCell: UITableViewCell {
    //标题Label
    private lazy var titleLabel:UILabel = createLabel("Library", font: .systemFont(ofSize: 18*width, weight: .regular), textColor: .white, textAlignment: .left)
    private lazy var layout:UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = .init(width: 109*width, height: 135*width)
        layout.sectionInset = .init(top: 10*width, left: 16*width, bottom: 0, right: 16*width)
        layout.minimumLineSpacing = 8*width
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        return layout
    }()
    //collectionView
    private lazy var collectionView:UICollectionView = {
        let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(MPPositive_HomeLibraryListCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeLibraryListCollectionViewCellID)
        return collectionView
    }()
    fileprivate let MPPositive_HomeLibraryListCollectionViewCellID = "MPPositive_HomeLibraryListCollectionViewCell"
    var libraryViewModels: [MPPositive_LibraryViewModel] = [] {
        didSet{
            collectionView.reloadData()
        }
    }
    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()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }
    private func configure() {
        //配置UI
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16*width)
            make.top.equalToSuperview().offset(8*width)
            make.right.equalToSuperview().offset(-16*width)
        }
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-25*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()
        //横向
        let height = layout.itemSize.height + layout.sectionInset.top + layout.sectionInset.bottom
        return CGSize(width: size.width, height: size.height + height)
    }
}
//MARK: - collectionView
extension MPPositive_HomeLibraryListstableViewCell:UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return libraryViewModels.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeLibraryListCollectionViewCellID, for: indexPath) as! MPPositive_HomeLibraryListCollectionViewCell
        cell.library = libraryViewModels[indexPath.row]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard (libraryViewModels.count - 1) >= indexPath.row else {
            return
        }
        let item = libraryViewModels[indexPath.row]
        switch item.library.libraryType {
        case .love_songs:
            MP_AnalyticsManager.shared.library_clickAction("home page", folder: "love")
            let loveSongsVC = MPPositive_LoveSongsViewController()
            parentController()?.navigationController?.pushViewController(loveSongsVC, animated: true)
        case .love_aritists:
            MP_AnalyticsManager.shared.library_clickAction("home page", folder: "artist")
            let loveArtistsVC = MPPositive_LoveArtistsViewController()
            parentController()?.navigationController?.pushViewController(loveArtistsVC, animated: true)
        case .offline_songs:
            MP_AnalyticsManager.shared.library_clickAction("home page", folder: "offline")
            let offlineVC = MPPositive_OfflineSongsViewController()
            parentController()?.navigationController?.pushViewController(offlineVC, animated: true)
        case .custom_playlist:
            if let playListId = item.library.identifier {
                //检索出对应的自定义歌单
                MPPositive_CustomPlayListModel.fetchAll {[weak self] playList in
                    guard let self = self else {return}
                    if let list = playList.first(where: {$0.playListId == playListId}) {
                        MP_AnalyticsManager.shared.library_clickAction("home page", folder: "custom")
                        let playListVC = MPPositive_CustomPlayListViewController(list)
                        parentController()?.navigationController?.pushViewController(playListVC, animated: true)
                    }
                }
            }
        }
        MP_AdMobManager.shared.showLibraryInterstitialAdIfAvailable(completion: nil)
    }
}
