//
//  HomeViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/27.
//

import UIKit

class HomeViewController: BaseViewController {
    @IBOutlet weak var tableView: UITableView!{
        didSet{
            if #available(iOS 15.0, *) {
                tableView.sectionHeaderTopPadding = 0
            }
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.estimatedRowHeight = 200
            tableView.rowHeight = UITableView.automaticDimension
            tableView.tableHeaderView = headView
            //额外加载一段下边距
            tableView.contentInset = .init(top: 0, left: 0, bottom: 100*width, right: 0)
            tableView.dataSource = self
            tableView.delegate = self
            tableView.register(UINib(nibName: Home_RowListsTableViewCellID, bundle: nil), forCellReuseIdentifier: Home_RowListsTableViewCellID)
        }
    }
    private lazy var headView:Home_HeadBannerView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 135*width))
    private let Home_RowListsTableViewCellID = "Home_RowListsTableViewCell"
    //组头标题
    private lazy var sectionTitles:[String] = ["", MusicShowType.First.title, MusicShowType.Second.title, MusicShowType.Third.title]

    override func viewDidLoad() {
        super.viewDidLoad()
        //触发音乐缺失闭包
        nullMusicAction = {
            [weak self] in
            //刷新页面
            self?.reload()
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //添加通知
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(fileMissAction(_ :)), notificationName: .null_music)
//        NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .play_music)
//        NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .stop_music)
//        NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .resume_music)
//        NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .pause_music)
        reload()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //移除所有通知
        NotificationCenter.default.removeObserver(self)
        
    }
    deinit {
        
    }
    //刷新渲染页面
    private func reload() {
        LoadDataMusic.shared.reloadListData()
        tableView.reloadData()
    }
//    //当音乐播放器状态改变时
//    @objc private func switchPlayerStateAction(_ sender:Notification) {
//        //刷新页面内容
//        tableView.reloadData()
//    }
}
//MARK: - tableView
extension HomeViewController:UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionTitles.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Home_RowListsTableViewCellID, for: indexPath) as! Home_RowListsTableViewCell
        cell.layout = .init(rawValue: indexPath.section) ?? .Zero
        return cell
    }
    //设置组头View
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 40*width))
        sectionView.backgroundColor = .clear
        //添加一个Label
        let label = UILabel()
        label.textColor = .white
        label.font = .systemFont(ofSize: 20, weight: .regular)
        label.text = sectionTitles[section]
        sectionView.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16*width)
            make.top.equalToSuperview()
        }
        return sectionView
    }
    //设置组头View高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section != 0 ? 40*width:0
    }
}

