//
//  PlayListCell.swift
//  ColorfulWallpaper
//
//  Created by ni on 2024/9/18.
//

import Foundation
import TSVideoKit
import UIKit

class PlayListCell: UITableViewCell {
    lazy var bgView: UIView = {
        let v: UIView = .simpleView(color: .clear)
        return v
    }()

    lazy var iconView: UIImageView = {
        let img: UIImageView = .init(image: .icDefault)
        img.customCornerRadius = 8
        img.contentMode = .scaleAspectFill
        return img
    }()

    lazy var titleLabel: UILabel = .simpleLabel(text: "", color: .white)
    lazy var countLabel: UILabel = .simpleLabel(text: "0", color: .white.withAlphaComponent(0.4))

    lazy var moreButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_more"), for: .normal)
        btn.addTarget(self, action: #selector(showMoreViewController), for: .touchUpInside)
        return btn
    }()

    private var playlist: TSPlayList?

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

    @objc func showMoreViewController() {
        // 创建 UIAlertController，样式为 actionSheet
        let alert = UIAlertController()

        // 添加第一个选项
        let option1 = UIAlertAction(title: "Rename".localized(), style: .default) { [weak self] _ in
            guard let self = self else { return }
            if let mPlaylist = self.playlist {
                CWOperateViewController.renamePlaylist(playlist: mPlaylist)
            }
        }
        alert.addAction(option1)

        // 添加第二个选项
        let option2 = UIAlertAction(title: "Delete Playlist".localized(), style: .destructive) { [weak self] _ in
            guard let self = self else { return }
            if let mPlaylist = self.playlist {
                CWOperateViewController.deletePlaylist(playlist: mPlaylist)
            }
        }
        alert.addAction(option2)

        // 添加取消按钮
        let cancelAction = UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil)
        alert.addAction(cancelAction)

        // 弹出弹窗
        PlayerManager.shared.rootVc?.present(alert, animated: true)
    }

    func bindData(list: TSPlayList, needMoreButton: Bool) {
        moreButton.isHidden = !needMoreButton
        playlist = list
        titleLabel.text = list.title
        countLabel.text = "\(list.videos?.count ?? 0)"
        if let firstVideo = list.firstVideo, firstVideo.isOnline == true {
            if firstVideo.isAudio,!firstVideo.isOnline {
                iconView.image = .icDefault
            } else {
                iconView.kf.setImage(with: firstVideo.iconUrl,placeholder: UIImage.init(named: "ic_default"))
            }
        } else {
            iconView.image = list.artworkImage ?? .icDefault
        }
    }

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

    func addChildren() {
        contentView.addSubview(bgView)
        bgView.addSubview(iconView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(countLabel)
        bgView.addSubview(moreButton)
    }

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

        iconView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.bottom.equalToSuperview().offset(-12)
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(56)
        }

        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(12)
            make.trailing.equalTo(moreButton.snp.leading).offset(-12)
            make.top.equalToSuperview().offset(20)
        }

        countLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(12)
            make.trailing.equalTo(moreButton.snp.leading).offset(-12)
            make.bottom.equalToSuperview().offset(-20)
        }

        moreButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(64)
        }
    }
}
