//
//  PlaylistTopView.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/21.
//

import Foundation
import TSVideoKit
import UIKit

class PlaylistTopView: UIControl {
    lazy var cdOne: CDView = CDView()
    lazy var cdTwo: CDView = CDView()
    lazy var cdThree: CDView = CDView()

    lazy var titleLabel: UILabel = .simpleLabel(text: "Playlist".localized(), font: .systemFont16, color: .white.withAlphaComponent(0.8))
    lazy var countLabel: UILabel = .simpleLabel(text: "0", font: .systemFont14, color: .white.withAlphaComponent(0.4))

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraints()
    }

    func bindData(playlists: [TSPlayList]) {
        countLabel.text = "\(playlists.count)"

        let imageViews = [cdOne.icImageView, cdTwo.icImageView, cdThree.icImageView]

        for (index, imageView) in imageViews.enumerated() {
            // 检查 playlists 是否有对应的索引
            if playlists.indices.contains(index) {
                // 获取图片，如果为空则设置默认图片
                if playlists[index].firstVideo?.isOnline == true {
                    imageView.kf.setImage(with: playlists[index].firstVideo?.iconUrl)
                } else {
//                    imageView.image = playlists[index].artworkImage ?? UIImage(resource: .icPlayPlaceholder)
                }
            } else {
                // 如果索引超出范围，直接设置默认图片
//                imageView.image = UIImage(resource: .icPlayPlaceholder)
            }
        }
    }

    func addChildren() {
        backgroundColor = .white.withAlphaComponent(0.1)
        layer.cornerRadius = 16
        layer.masksToBounds = true
        addSubview(cdThree)
        addSubview(cdTwo)
        addSubview(cdOne)
        cdOne.isUserInteractionEnabled = false
        cdTwo.isUserInteractionEnabled = false
        cdThree.isUserInteractionEnabled = false

        addSubview(titleLabel)
        addSubview(countLabel)
    }

    func makeConstraints() {
        cdOne.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.leading.equalToSuperview().offset(6)
            make.width.height.equalTo(80)
        }

        cdTwo.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.leading.equalToSuperview().offset(36)
            make.width.height.equalTo(80)
        }

        cdThree.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.leading.equalToSuperview().offset(70)
            make.width.height.equalTo(80)
        }

        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.bottom.equalTo(countLabel.snp.top).offset(-8)
        }

        countLabel.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-12)
            make.leading.equalToSuperview().offset(16)
        }
    }

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