//
//  PlayMiniBar.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/2/23.
//

import Foundation
import TSVideoKit
import UIKit

class PlayMiniBar: UIView {
    lazy var iconView: UIImageView = .simpleImage(imageName: "ic_default")
    lazy var titleLabel: UILabel = .simpleLabel(text: "", color: .white)
    lazy var bgImageView: UIView = UIView()
    lazy var playButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_detail_playBtn"), for: .normal)
        btn.setImage(UIImage(named: "ic_detail_pauseBtn"), for: .selected)
        return btn
    }()

    lazy var nextButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_play_next"), for: .normal)
        return btn
    }()

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

    func addNotifaction() {
        NotificationCenter.default.addObserver(self, selector: #selector(updateMiniBarInfo), name: NotifactionKey.kVideoDidChangedNotifactionName, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateMiniBarStatus), name: NotifactionKey.kVideoStatusDidChangedNotifactionName, object: nil)
    }

    @objc func playButtonAction() {
        /// 播放器没源，但是有当前播放歌曲
        if PlayerManager.shared.player?.playControl.player.urlAsset == nil, let current = PlayerManager.shared.currentVideo {
            PlayerManager.shared.playVideo(video: current, list: [], scene: .local, onceAdKey: "")
        } else {
            PlayerManager.shared.playOrPause()
        }
    }

    @objc func playNextButtonAction() {
        PlayerManager.shared.playNext()
    }

    @objc func updateMiniBarStatus() {
    }

    @objc func updateMiniBarInfo() {
    }

    func resetVideoInfo() {
        iconView.contentMode = .scaleAspectFill
        titleLabel.text = "Unknown".localized()
        iconView.image = UIImage(named: "ic_default")
        playButton.setImage(UIImage(named: "ic_detail_playBtn"), for: .normal)
    }

    func updateVideoInfo(video: TSVideo, state: PlayState) {
        titleLabel.text = video.title
//        titleLabel.speed = .duration(10)

        let isPlaying = video.videoId == PlayerManager.shared.currentVideo?.videoId

        if video.isOnline {
            iconView.kf.setImage(with: video.iconUrl)
        } else {
            if let imageData = video.artwork {
                iconView.image = UIImage(data: imageData)
            } else {
                iconView.image = UIImage(named: "ic_default")
            }
        }
        if state == .pause {
            playButton.setImage(UIImage(named: "ic_detail_playBtn"), for: .normal)
        } else {
            playButton.setImage(UIImage(named: "ic_detail_pauseBtn"), for: .normal)
        }
    }

    func addChildren() {
        iconView.customCornerRadius = 19
        iconView.contentMode = .scaleAspectFill

        playButton.addTarget(self, action: #selector(playButtonAction), for: .touchUpInside)
        nextButton.addTarget(self, action: #selector(playNextButtonAction), for: .touchUpInside)
        bgImageView.isUserInteractionEnabled = true
        addSubview(bgImageView)
        bgImageView.addSubview(iconView)
        bgImageView.addSubview(titleLabel)
        bgImageView.addSubview(playButton)
        bgImageView.addSubview(nextButton)
    }

    func makeConstraints() {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        iconView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(38)
        }
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
            make.trailing.equalTo(playButton.snp.leading)
        }
        playButton.snp.makeConstraints { make in
            make.trailing.equalTo(nextButton.snp.leading).offset(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        nextButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        bgImageView.backgroundColor = .hexColor("#222222")
    }

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