//
//  OperateTopView.swift
//  TSLiveWallpaper
//
//  Created by nkl on 2024/12/23.
//

import Foundation
import TSVideoKit
import UIKit

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

    lazy var raduiButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_mine_ring_mutil_unselect"), for: .normal)
        btn.setImage(UIImage(named: "ic_mine_ring_mutil_select"), for: .selected)
        btn.isHidden = true
        btn.isUserInteractionEnabled = false

        return btn
    }()

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

    lazy var bgImageView: UIImageView = .simpleImage(imageName: "ic_minibar_bg")
    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: "", 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
    }()

    private var mVideo: TSVideo?
    var mPlaylist: TSPlayList?

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

    func bindData(video: TSVideo, isMutiSelected: Bool = false, isSelected: Bool = false, needOperate: Bool = false) {
        mVideo = video
        let isPlaying = mVideo?.videoId == PlayerManager.shared.currentVideo?.videoId
//        titleLabel.textColor = isPlaying ? .hexColor("#89FC42") : .white
//        timeLabel.textColor = (isPlaying ? UIColor.hexColor("#89FC42") : .white).withAlphaComponent(0.4)
        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")
            }
        }
        if PlayerManager.shared.currentVideo?.videoId == video.videoId {
            bgImageView.isHidden = false
        } else {
            bgImageView.isHidden = true
        }
        raduiButton.isHidden = !isMutiSelected
        raduiButton.isSelected = isSelected
    }

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

        bgView.addSubview(bgImageView)
        bgView.addSubview(iconView)
        bgView.addSubview(stackView)
        stackView.addArrangedSubview(titleLabel)
        stackView.addArrangedSubview(timeLabel)
        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)
        }

        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        iconView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.width.height.equalTo(56)
        }
        stackView.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(16)
            make.centerY.equalTo(iconView)
            make.trailing.equalToSuperview().offset(-12)
        }
        
        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")
    }
}
