//
//  MPPositive_PlayListsShowTypeView.swift
//  relax.offline.mp3.music
//
//  Created by Mr.Zhou on 2024/6/21.
//

import UIKit
///曲库PlayLits分页展示View
class MPPositive_PlayListsShowTypeView: UIView, JXSegmentedListContainerViewListDelegate, UITableViewDataSource, UITableViewDelegate {
    //标题
    private lazy var playListsLabel:UILabel = createLabel("Playlists", font: .systemFont(ofSize: 14*width), textColor: .init(hex: "#999999"), textAlignment: .left)
    //新增按钮
    private lazy var addBtn:UIButton = {
        let btn:UIButton = .init()
        btn.setBackgroundImage(UIImage(named: "Center_PlayLists'logo"), for: .normal)
        btn.addTarget(self, action: #selector(addClick(_ :)), for: .touchUpInside)
        return btn
    }()
    //新增事件包
    var addBlock:(() -> Void)?
    //选中事件包
    var chooseBlock:((MPPositive_CustomPlayListViewModel) -> Void)?
    //tableView
    private lazy var tableView:UITableView = {
        let tableView = UITableView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), style: .plain)
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        }
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(MPPositive_LibraryTableViewCell.self, forCellReuseIdentifier: MPPositive_LibraryTableViewCellID)
        tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
        return tableView
    }()
    private let MPPositive_LibraryTableViewCellID = "MPPositive_LibraryTableViewCell"
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    //配置
    private func configure() {
        addSubview(playListsLabel)
        playListsLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16*width)
            make.top.equalToSuperview().offset(24*width)
        }
        addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.width.height.equalTo(26*width)
            make.right.equalToSuperview().offset(-16*width)
            make.centerY.equalTo(playListsLabel.snp.centerY)
        }
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(playListsLabel.snp.bottom).offset(12*width)
            make.left.right.bottom.equalToSuperview()
        }
        //注册监听
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(reloadAction(_ :)), notificationName: .create_custom_playlist)
    }
    deinit{
        NotificationCenter.default.removeObserver(self)
    }
    //收到通知，刷新页面
    @objc private func reloadAction(_ sender:Notification) {
        reload()
    }
    //刷新页面
    private func reload(){
        MPPositive_LoadCoreModel.shared.reloadCustomPlayLists {
            [weak self] in
            guard let self = self else {return}
            tableView.showMessage(MPPositive_LoadCoreModel.shared.playLists.count)
            tableView.reloadData()
        }
    }
    
    //MARK: - 分页代理
    func listView() -> UIView {
        return self
    }
    func listWillAppear() {
        reload()
    }
    ///新增自定义歌单
    @objc private func addClick(_ sender:UIButton) {
        //弹出新增自定义框
        if let block = addBlock {
            block()
        }
    }
    //MARK: - tableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MPPositive_LoadCoreModel.shared.playLists.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_LibraryTableViewCellID, for: indexPath) as! MPPositive_LibraryTableViewCell
        cell.playList = MPPositive_LoadCoreModel.shared.playLists[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = MPPositive_LoadCoreModel.shared.playLists[indexPath.row]
        if let block = chooseBlock {
            block(item)
        }
    }
}
