//
//  PlayDetaiListViewCell.swift
//  ColorfulWallpaper
//
//  Created by ni on 2024/9/19.
//

import Foundation
import TSVideoKit
import UIKit

class PlayDetailListViewCell: UITableViewCell {
    var deleteButtonClickEvent: (String) -> Void = { _ in }

    lazy var bgView: UIView = .simpleView(color: .clear)

    lazy var iconView: UIImageView = {
        let img: UIImageView = .init(image: UIImage(named: ""))
        img.customCornerRadius = 12
        img.contentMode = .scaleAspectFill
        return img
    }()

    lazy var titleLabel: UILabel = .simpleLabel(text: "", font: .systemFont14, color: .white)
    lazy var detailLabel: UILabel = .simpleLabel(text: "", font: .systemFont14, color: .white.withAlphaComponent(0.4))
    lazy var stackView: UIStackView = {
        let stack: UIStackView = .vStack
        stack.spacing = 8
        return stack
    }()

    lazy var hStackView: UIStackView = {
        let stack: UIStackView = .hStack
        stack.spacing = 8
        stack.alignment = .center
        return stack
    }()

    lazy var playingTag: UIImageView = .simpleImage(imageName: "ic_playing_tag")

    lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(.init(named: "ic_close"), for: .normal)
        btn.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        return btn
    }()

    private var videoId: String = ""

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        addChildren()
        makeConstraints()
    }

    func bindData(video: TSVideo) {
        videoId = video.videoId ?? ""
        titleLabel.text = video.title
        detailLabel.text = video.artist
        let isPlaying = video.videoId == PlayerManager.shared.currentVideo?.videoId
        playingTag.isHidden = !isPlaying

        if video.isOnline {
            iconView.kf.setImage(with: video.iconUrl)
        } else {
            if let imgData = video.artwork {
                iconView.image = UIImage(data: imgData)
            } else {
                iconView.image = UIImage(named: "ic_default")
            }
        }
    }

    func bindData(data: VideoOnlineModel) {
        videoId = data.videoId
        titleLabel.text = data.title
        detailLabel.text = data.channelName
        let isPlaying = data.videoId == PlayerManager.shared.currentVideo?.videoId
        playingTag.isHidden = !isPlaying
        iconView.kf.setImage(with: data.iconUrl)
    }

    @objc func deleteButtonAction() {
        deleteButtonClickEvent(videoId)
    }

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

    func addChildren() {
        contentView.addSubview(bgView)
        bgView.addSubview(hStackView)
        hStackView.addArrangedSubview(iconView)
        hStackView.addArrangedSubview(stackView)
        hStackView.addArrangedSubview(playingTag)
        stackView.addArrangedSubview(titleLabel)
        stackView.addArrangedSubview(detailLabel)
        bgView.addSubview(deleteButton)
    }

    func makeConstraints() {
        bgView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(8)
        }

        hStackView.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(16)
            make.trailing.equalTo(deleteButton.snp.leading).offset(-8)
            make.verticalEdges.equalToSuperview()
        }

        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(60)
        }

        playingTag.snp.makeConstraints { make in
            make.width.height.equalTo(24)
        }

        deleteButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
    }
}
