//
//  MusicEmptyView.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/13.
//

import Foundation
import TSVideoKit
import UIKit

class SongListCell: UITableViewCell {
    var isMuti: Bool = false
    var needOperate: Bool = true

    lazy var hStack: UIStackView = {
        let stack: UIStackView = .hStack
        stack.spacing = 14
        return stack
    }()

    lazy var raduiButton: UIButton = {
        let btn = UIButton()
        btn.setImage(.icCheckN, for: .normal)
        btn.setImage(.icCheckS, for: .selected)
        btn.isHidden = true
        btn.isUserInteractionEnabled = false

        return btn
    }()

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

    lazy var lineView: UIView = .simpleView(color: .white.withAlphaComponent(0.1))

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

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

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

    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
    }()

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

    private var mVideo: TSVideo?
    var mPlaylist: TSPlayList?

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

        addChildren()
        makeConstraints()
    }

    func bindData(video: TSVideo, isMutiSelected: Bool = false, isSelected: Bool = false, needOperate: Bool = false) {
        mVideo = video
        self.needOperate = needOperate
        let isPlaying = mVideo?.videoId == PlayerManager.shared.currentVideo?.videoId
        playingTag.isHidden = !isPlaying

        isMuti = isMutiSelected
        titleLabel.text = video.title
        if let artist = video.artist {
            timeLabel.text = artist.isEmpty == true ? "UnKnown".localized() : video.artist
        } else {
            timeLabel.text = "UnKnown".localized()
        }

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

        raduiButton.isHidden = !isMutiSelected
        raduiButton.isSelected = isSelected
        moreButton.isHidden = isMutiSelected || !needOperate
    }

    @objc func showMoreViewController() {
        let vc = CWOperateViewController()
        if let playlist = mPlaylist {
            vc.playlist = playlist
            vc.types = [.like, .addPlaylist, .removeFromPlaylist, .share, .deleteVideo]
        } else {
            vc.types = [.like, .addPlaylist, .share, .deleteVideo]
        }
        vc.operteItem = mVideo
        vc.modalPresentationStyle = .overFullScreen
        PlayerManager.shared.rootVc?.present(vc, animated: true)

        // todo.kailen
//        if isMuti {
//            return
//        }
//        let operateTypes: [OperateType]
//        if isFromPlaylist {
//            operateTypes = [.edit, .addPlaylist, .like, .removeFromlist, .delete,.share, .ring]
//        } else {
//            operateTypes = [.edit, .addPlaylist, .like, .delete,.share, .ring]
//        }
//        let vc = RingOperateViewController(operateTypes: operateTypes)
//        vc.video = mVideo
//        vc.playlist = mPlaylist
//        vc.modalPresentationStyle = .overFullScreen
//        PlayRootController.shared.present(vc, animated: true)
    }

    func addChildren() {
        contentView.addSubview(hStack)
        hStack.addArrangedSubview(bgView)
        hStack.addArrangedSubview(raduiButton)

        bgView.addSubview(hContentStack)
        hContentStack.addArrangedSubview(iconView)
        hContentStack.addArrangedSubview(stackView)
        hContentStack.addArrangedSubview(playingTag)
        hContentStack.addArrangedSubview(moreButton)
        stackView.addArrangedSubview(titleLabel)
        stackView.addArrangedSubview(timeLabel)
        contentView.addSubview(lineView)
    }

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

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

        hContentStack.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(56)
        }

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

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

        moreButton.snp.makeConstraints { make in
            make.width.height.equalTo(44)
        }

        lineView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.horizontalEdges.equalToSuperview().inset(16)
            make.height.equalTo(1)
        }
    }

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