//
//  MPPositive_ArtistShowSongTableViewCell.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/16.
//

import UIKit
import DownloadButton
class MPPositive_ArtistShowSongTableViewCell: UITableViewCell, PKDownloadButtonDelegate {
    //特殊图片（展示预览图片）
    private lazy var iconImageView:UIImageView = {
        let imageView:UIImageView = .init()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        return imageView
    }()
    private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
    private lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.6), textAlignment: .left)
    ///更多按钮
    private lazy var moreBtn:UIButton = {
        let btn:UIButton = .init()
        btn.setBackgroundImage(UIImage(named: "Song_More'logo"), for: .normal)
        btn.addTarget(self, action: #selector(moreActionClick(_ :)), for: .touchUpInside)
        return btn
    }()
    //设置下载按钮
    private lazy var loadBtn:PKDownloadButton = {
        let btn:PKDownloadButton = .init()
        //开始下载状态
        btn.startDownloadButton.cleanDefaultAppearance()
        btn.startDownloadButton.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
        //下载结束状态
        btn.downloadedButton.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
        btn.downloadedButton.setAttributedTitle(nil, for: .normal)
        //停止下载状态
        btn.stopDownloadButton.stopButton.setImage(UIImage(named: "download"), for: .normal)
        btn.stopDownloadButton.tintColor = UIColor(hex: "#80F988")
        btn.stopDownloadButton.stopButtonWidth = 2
        btn.stopDownloadButton.filledLineWidth = 3*width
        btn.stopDownloadButton.filledLineStyleOuter = true
        
        //加载状态设置
        btn.pendingView.tintColor = UIColor(hex: "#80F988")
        btn.pendingView.radius = 12*width
        btn.pendingView.emptyLineRadians = 2*width
        btn.pendingView.spinTime = 3
        btn.delegate = self
        return btn
    }()
    var itemView:MPPositive_BrowseItemViewModel!{
        didSet{
            itemView.setUrltoImage(iconImageView)
            titleLabel.text = itemView.title
            subtitleLabel.text = itemView.subtitle
            setProgress(itemView.browseItem.videoId ?? "")
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadProgressAction(_ :)), notificationName: .download_progress_source)
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadEndAction(_ :)), notificationName: .dowload_end_source)
        configure()
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    //收到progress加载
    @objc private func downloadProgressAction(_ sender:Notification) {
        if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = itemView?.browseItem.videoId, videoId == currentVideoId {
            //比较当前videoId
            setProgress(videoId)
        }
    }
    //收到下载结束
    @objc private func downloadEndAction(_ sender:Notification) {
        if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = itemView?.browseItem.videoId, videoId == currentVideoId {
            setProgress(videoId)
        }
    }
    private func configure() {
        contentView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.width.height.equalTo(60*width)
            make.top.equalToSuperview().offset(8*width).priority(999)
            make.bottom.equalToSuperview().offset(-8*width)
            make.left.equalToSuperview().offset(18*width)
        }
        contentView.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24*width)
            make.centerY.equalTo(iconImageView.snp.centerY)
            make.right.equalToSuperview().offset(-18*width)
        }
        contentView.addSubview(loadBtn)
        loadBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24*width)
            make.centerY.equalTo(iconImageView.snp.centerY)
            make.right.equalToSuperview().offset(-54*width)
        }
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconImageView.snp.top).offset(10*width)
            make.left.equalTo(iconImageView.snp.right).offset(12*width)
            make.right.equalTo(loadBtn.snp.left).offset(-10*width)
        }
        contentView.addSubview(subtitleLabel)
        subtitleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(iconImageView.snp.bottom).offset(-10*width)
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(titleLabel.snp.right)
        }
    }
    var moreBlock:(() -> Void)?
    //删除
    var deleteBlock:(() -> Void)?
    //取消
    var cancelBlock:(() -> Void)?
    //根据输入videoId检索是否下载
    func setProgress(_ videoId:String) {
        guard videoId.isEmpty == false, videoId == itemView.browseItem.videoId else {
            DispatchQueue.main.async {
                //未加入下载队列
                self.loadBtn.state = .startDownload
            }
            return
        }
        MP_DownloadManager.shared.loadQueue.async {
            [weak self] in
            guard let self = self else {return}
            //判断是否下载完成
            MP_DownloadManager.shared.isDownloadedFileDocuments(videoId) {statu in
                if statu == false {
                    //未下载,判断是否加入了下载队列
                    if MP_DownloadManager.shared.isTasksQueue(for: videoId) {
                        //加入下载队列，确定是否活跃下中
                        if MP_DownloadManager.shared.isActiveTask(for: videoId) {
                            DispatchQueue.main.async {
                                self.loadBtn.state = .downloading
                            }
                            //活跃中，获取下载值
                            if let progress = MP_DownloadManager.shared.getProgress(for: videoId) {
                                //下载中
                                DispatchQueue.main.async {
                                    self.loadBtn.stopDownloadButton.progress = progress
                                }
                            }
                        }else {
                            //处于队列中，还未开始下载
                            DispatchQueue.main.async {
                                //未加入下载队列
                                self.loadBtn.state = .pending
                            }
                        }
                    }else {
                        //未加入下载队列
                        DispatchQueue.main.async {
                            //未加入下载队列
                            self.loadBtn.state = .startDownload
                        }
                    }
                }else {
                    DispatchQueue.main.async {
                        //已经完成下载
                        self.loadBtn.state = .downloaded
                    }
                }
            }
        }
    }
    //点击更多
    @objc private func moreActionClick(_ sender:UIButton) {
        guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
            MP_HUD.text("Weak connection.", delay: 2.0, completion: nil)
            return
        }
        guard moreBlock != nil else {
            return
        }
        moreBlock!()
    }
    //下载按钮代理
    func downloadButtonTapped(_ downloadButton: PKDownloadButton!, currentState state: PKDownloadButtonState) {
        var video:MPPositive_SongItemModel!
        //根据当前按钮状态确定进度
        switch state {
        case .startDownload://开始状态，点击后进入下载状态
            //检索当前网络状态
            guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
                MP_HUD.text("Weak connection.", delay: 2.0, completion: nil)
                return
            }
            //检索当前歌曲是否下载过
            guard let videoId = itemView.browseItem.videoId else {
                return
            }
            //未下载
//            MP_AdMobManager.shared.showLoadInterstitialAdIfAvailable(completion: nil)
            //切换为准备状态
            downloadButton.state = .pending
            //通过网络请求获取当前预览项资源(转为Song)
            MP_NetWorkManager.shared.requestNextList("", videoId: itemView.browseItem.videoId ?? ""){
                [weak self] listSongs in
                guard let self = self, let first = listSongs.first else {
                    //获取资源失败
                    self?.setProgress(videoId)
                    return
                }
                let group = DispatchGroup()
                group.enter()
                improveDataforLycirsAndRelated(first) {(result)  in
                    first.lyricsID = result.0
                    first.relatedID = result.1
                    group.leave()
                }
                group.enter()
                //补全资源路径组和封面路径组
                improveDataforResouceAndCover(first) {(resourceUrls, coverUrls) in
                    if let resourceUrls = resourceUrls {
                        first.resourceUrls = resourceUrls.0
                        first.itags = resourceUrls.1
                        first.mimeTypes = resourceUrls.2
                    }
                    first.coverUrls = coverUrls
                    group.leave()
                }
                group.notify(queue: .main, execute: {
                    [weak self] in
                    guard let self = self else {
                        self?.setProgress(videoId)
                        return
                    }
                    video = first
                    //事件打点
                    MP_AnalyticsManager.shared.player_b_download_clickAction(video.videoId, videoname: video.title ?? "", artistname: video.shortBylineText ?? "")
                    //补全了数据,执行下载
                    MP_DownloadManager.shared.prepareVideoDownloadTask(from: video)
                })
            }
        case .downloaded://已下载
            if let block = deleteBlock {
                block()
            }
        default:
            if let block = cancelBlock {
                block()
            }
        }
        
    }
}
