//
//  MPPositive_LoveArtistsViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/28.
//

import UIKit

class MPPositive_LoveArtistsViewController: MPPositive_BaseViewController {
    ///广告View
    fileprivate lazy var adContainerView:UIView = {
        let adContainerView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 300*width))
        adContainerView.backgroundColor = .clear
        return adContainerView
    }()
    private lazy var numbersLabel:UILabel = createLabel(font: .systemFont(ofSize: 18*width, weight: .regular), textColor: .white, textAlignment: .left)
    ///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_LoveArtistTableViewCell.self, forCellReuseIdentifier: MPPositive_LoveArtistTableViewCellID)
        tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
        return tableView
    }()
    private let MPPositive_LoveArtistTableViewCellID = "MPPositive_LoveArtistTableViewCell"
    override func viewDidLoad() {
        super.viewDidLoad()
        setTitle("Love Artists")
        setPopBtn()
        configure()
        MP_AdMobManager.shared.layoutLibraryNativeAd(in: adContainerView, index: 1)
        MP_AdMobManager.shared.onLibraryNativeAdBlock = {
            [weak adContainerView] in
            guard let adContainerView = adContainerView else {return}
            MP_AdMobManager.shared.layoutLibraryNativeAd(in: adContainerView, index: 1)
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        reload()
    }
    //刷新列表
    private func reload() {
        MPPositive_LoadCoreModel.shared.reloadCollectionArtistViewModels {
            [weak self] in
            guard let self = self else {return}
            tableView.showMessage(MPPositive_LoadCoreModel.shared.artistViewModels.count, title: "No Artists")
            numbersLabel.text = "\(MPPositive_LoadCoreModel.shared.artistViewModels.count) Artists"
            tableView.reloadData()
        }
    }
    private func configure() {
        view.addSubview(adContainerView)
        adContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navView.snp.bottom)
            make.height.equalTo(0)
        }
        view.addSubview(numbersLabel)
        numbersLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18*width)
            make.top.equalTo(adContainerView.snp.bottom).offset(32*width)
        }
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(numbersLabel.snp.bottom).offset(18*width)
            make.left.right.bottom.equalToSuperview()
        }
    }
}
//MARK: - tableView
extension MPPositive_LoveArtistsViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MPPositive_LoadCoreModel.shared.artistViewModels.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_LoveArtistTableViewCellID, for: indexPath) as! MPPositive_LoveArtistTableViewCell
        cell.artistViewModel = MPPositive_LoadCoreModel.shared.artistViewModels[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = MPPositive_LoadCoreModel.shared.artistViewModels[indexPath.row]
        //列表专辑
        let artistVC = MPPositive_ArtistShowViewController(item.collectionArtist.artistId ?? "")
        navigationController?.pushViewController(artistVC, animated: true)
    }
}
