//
//  MPPositive_PlayerListShowViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/10.
//

import UIKit

class MPPositive_PlayerListShowViewController: UIViewController {
    //小角标图片
    private lazy var indictorImageView:UIImageView = .init(image: .init(named: "Player_Indictor'logo"))
    //tableView（列表展示）
    private lazy var tableView:UITableView = {
        let tableView = UITableView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), style: .plain)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.contentInset = .init(top: 0, left: 0, bottom: 30*width, right: 0)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(MPPositive_PlayerListShowTableViewCell.self, forCellReuseIdentifier: MPPositive_PlayerListShowTableViewCellID)
        return tableView
    }()
    private let MPPositive_PlayerListShowTableViewCellID = "MPPositive_PlayerListShowTableViewCell"
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .init(hex: "#1A1A1A")
        view.layer.masksToBounds = true
        view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        view.layer.cornerRadius = 18*width
        configure()
        //添加监听，当前音乐切换后，刷新
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(currentVideoReloadAction(_:)), notificationName: .positive_player_reload)
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    private func configure() {
        view.addSubview(indictorImageView)
        indictorImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(29*width)
            make.height.equalTo(4*width)
            make.top.equalTo(12*width)
        }
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(38*width)
            make.left.right.bottom.equalToSuperview()
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    @objc private func currentVideoReloadAction(_ sender:Notification) {
        tableView.reloadData()
    }
    
}
//MARK: - tableView
extension MPPositive_PlayerListShowViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //展示MP_PlayerManager中的列表数据
        return MP_PlayerManager.shared.loadPlayer.songVideos.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_PlayerListShowTableViewCellID, for: indexPath) as! MPPositive_PlayerListShowTableViewCell
        cell.song = MP_PlayerManager.shared.loadPlayer.songVideos[indexPath.row]
        cell.removeBlock = {
            [weak self] in
            if MP_PlayerManager.shared.loadPlayer.songVideos.count > 1 {
                //从列表中移除音乐
                MP_PlayerManager.shared.loadPlayer.removeData(MP_PlayerManager.shared.loadPlayer.songVideos[indexPath.row].videoId)
                tableView.reloadData()
            }else {
                //当音乐库只有一首的话，移除的话，直接关闭播放器
                MP_PlayerManager.shared.stop()
                MP_PlayerManager.shared.loadPlayer = nil
                self?.dismiss(animated: true)
            }
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //播放选中音乐
        dismiss(animated: true) {
            //更新播放音乐
            MP_PlayerManager.shared.loadPlayer.improveData(MP_PlayerManager.shared.loadPlayer.songVideos[indexPath.row].videoId)
        }
    }
}
