//
//  MPPositive_HomeViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/19.
//

import UIKit

class MPPositive_HomeViewController: MPPositive_BaseViewController{
    //背景图片
    private lazy var bgImageView:UIImageView = {
        let imageView:UIImageView = .init(image: .init(named: "B_Home_BG'bg"))
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    //菜单按钮
    private lazy var memuBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "Home_Menu'logo"), for: .normal)
        btn.addTarget(self, action: #selector(menuRightClick(_ :)), for: .touchUpInside)
        return btn
    }()
    //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.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(MPPositive_HomeShowTableViewCell.self, forCellReuseIdentifier: MPPositive_HomeShowTableViewCellID)
        return tableView
    }()
    private let MPPositive_HomeShowTableViewCellID = "MPPositive_HomeShowTableViewCell"
//    private var loadViewModel:MPPositive_BrowseLoadViewModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        setTitle("Musicoo")
        confirgue()
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(reloadAction(_ :)), notificationName: .positive_browses_reload)
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    //MARK: - UI生成与配置
    //配置
    private func confirgue() {
        view.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(981*width)
        }
        view.addSubview(tableView)
        navView.addSubview(memuBtn)
        memuBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16*width)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24*width)
        }
        tableView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(32*width)
            make.left.right.bottom.equalToSuperview()
        }
        
    }
    //MARK: - 页面刷新
    //页面刷新
    @objc private func reloadAction(_ sender:Notification) {
        DispatchQueue.main.async {
            [weak self] in
            self?.tableView.reloadData()
        }
    }
    //MARK: - 点击
    //点击顶部右侧弹出菜单
    @objc private func menuRightClick(_ sender:UIButton) {
        
    }
}
//MARK: - tableView
extension MPPositive_HomeViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return MPPositive_BrowseLoadViewModel.shared.browseModuleLists.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_HomeShowTableViewCellID, for: indexPath) as! MPPositive_HomeShowTableViewCell
        cell.browseViewModel = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section]
        cell.showType = .init(rawValue: (indexPath.section > 4 ? 4:indexPath.section))!
        cell.requestNextBlock = {
            [weak self] (item) in
            guard let self = self else {return}
            switch item.browseItem.itemType {
            case .single:
                //单曲/视频跳转
                //触发next请求，优先获取列表全部单曲基础数据（不完善）
                MP_NetWorkManager.shared.requestNextList(item.browseItem.playListId ?? "", videoId: item.browseItem.videoId ?? ""){ [weak self] listSongs in
                    guard let self = self else {return}
                    //回掉的数据并不完善，生成一个playerloadViewModel
                    let lodaViewModel = MPPositive_PlayerLoadViewModel(listSongs, currentVideoId: item.browseItem.videoId ?? "")
                    lodaViewModel.improveData(item.browseItem.videoId ?? "")
                    MP_PlayerManager.shared.loadPlayer = lodaViewModel
                    NotificationCenter.notificationKey.post(notificationName: .pup_player_vc)
                }
            case .list:
                //列表专辑
                let listVC = MPPositive_ListShowViewController(item.browseItem.browseId ?? "", params: item.browseItem.params ?? "")
                navigationController?.pushViewController(listVC, animated: true)
            default:
                break
            }
        }
        cell.findMoreBlock = {
            [weak self] in
            guard let self = self else {return}
            let moreVC = MPPositive_MoreContentViewController()
            moreVC.browseModuleList = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section]
            navigationController?.pushViewController(moreVC, animated: true)
        }
        return cell
    }
}
