//
//  MPPositive_PlayerListShowTableViewCell.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/10.
//

import UIKit
import Kingfisher
class MPPositive_PlayerListShowTableViewCell: UITableViewCell {
    ///封面图片
    private lazy var coverImageView:UIImageView = {
        let imageView:UIImageView = .init()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 10*width
        return imageView
    }()
    ///标题
    private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
    ///副标题
    private lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.6), textAlignment: .left)
    ///移除按钮
    private lazy var removeBtn:UIButton = {
        let btn:UIButton = .init()
        btn.setBackgroundImage(UIImage(named: "Player_ListRemove'logo"), for: .normal)
        btn.addTarget(self, action: #selector(removeClick(_ :)), for: .touchUpInside)
        return btn
    }()
    var song:MPPositive_SongItemModel!{
        didSet{
            //判断是否当前播放
            if song.videoId == MP_PlayerManager.shared.loadPlayer.currentVideo?.song?.videoId {
                titleLabel.textColor = .init(hex: "#80F988")
                subtitleLabel.textColor = .init(hex: "#80F988")
            }else {
                titleLabel.textColor = .white
                subtitleLabel.textColor = .init(hex: "#FFFFFF", alpha: 0.6)
            }
            coverImageView.kf.setImage(with: URL(string: song.reviewUrls?.first ?? ""), placeholder: placeholderImage)
            titleLabel.text = song.title
            subtitleLabel.text = song.shortBylineText
        }
    }
    var removeBlock:(() -> Void)?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    private func configure() {
        contentView.addSubview(coverImageView)
        //设置封面图片，同时撑开cell
        coverImageView.snp.makeConstraints { make in
            make.width.height.equalTo(60*width)
            make.left.equalToSuperview().offset(18*width)
            make.top.equalToSuperview().offset(8*width).priority(999)
            make.bottom.equalToSuperview().offset(-8*width).priority(999)
        }
        contentView.addSubview(removeBtn)
        removeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24*width)
            make.centerY.equalTo(coverImageView.snp.centerY)
            make.right.equalToSuperview().offset(-18*width)
        }
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(coverImageView.snp.top).offset(10*width)
            make.left.equalTo(coverImageView.snp.right).offset(12*width)
            make.right.equalTo(removeBtn.snp.left).offset(-12*width)
        }
        contentView.addSubview(subtitleLabel)
        subtitleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(coverImageView.snp.bottom).offset(-10*width)
            make.left.right.equalTo(titleLabel)
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    //从歌单中移除
    @objc private func removeClick(_ sender:UIButton) {
        guard removeBlock != nil else {
            return
        }
        removeBlock!()
    }
}
