//
//  DiscoverPopularSongs.swift
//  wyy
//
//  Created by 施书顺 on 2021/3/5.
//

import UIKit

class DiscoverPopularSongsView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    struct SongData {
        var albumImageName: String
        var songName: String
        var authorName: String
        var comment: String
    }
    
    let popularSongLabel = UILabel()
    let playButton = UIButton()
    
    var collectionView : UICollectionView!
    let layout = UICollectionViewFlowLayout()
    var dataSource = [
        SongData(albumImageName: "album1", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
        SongData(albumImageName: "album2", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
        SongData(albumImageName: "album3", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
        SongData(albumImageName: "album3", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
        SongData(albumImageName: "album2", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
        SongData(albumImageName: "album1", songName: "Song Name", authorName: "Author", comment: "This is a comment")]
    
    init(){
        super.init(frame: CGRect.zero)
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 250, height: 50)
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.collectionViewLayout = layout
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = UIColor.white
//        collectionView.alwaysBounceVertical = true
        collectionView.register(PopularSongCell.self, forCellWithReuseIdentifier: "cell")
//        collectionView.contentSize = CGSize(width: 600, height: 200)
        addSubview(collectionView)
        collectionView.snp.makeConstraints{ make in
            make.width.equalToSuperview()
            make.height.equalTo(200)
            make.top.equalTo(20)
        }
        
        popularSongLabel.text = "歌曲推荐"
        popularSongLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        addSubview(popularSongLabel)
        popularSongLabel.snp.makeConstraints{ make in
            make.top.equalTo(5)
            make.left.equalTo(15)
        }
        
        playButton.setTitle(" ▶ 播放 ", for: .normal)
        playButton.setTitleColor(.black, for: .normal)
        playButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        playButton.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
        playButton.layer.borderWidth = 1.0
        playButton.layer.cornerRadius = 10
        addSubview(playButton)
        playButton.snp.makeConstraints{ make in
            make.top.equalTo(0)
            make.right.equalTo(-10)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        
        collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? PopularSongCell
        let data = dataSource[indexPath.row]
        if cell == nil {
            cell = PopularSongCell(frame: CGRect.zero)
        }
        cell!.albumImageView.image = UIImage(named: data.albumImageName)
        cell!.songLabel.text = data.songName
        cell!.authorLabel.text = " - \(data.authorName)"
        cell!.commentLabel.text = data.comment
        return cell!
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return CGSize(width: 200, height: 50)
//    }
    
}
