//
//  MPPositive_ArtistShowTypeView.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/15.
//

import UIKit
import MJRefresh
class MPPositive_ArtistShowTypeView: UIView, JXPagingViewListViewDelegate {
    //tableView
    private lazy var tableView:UITableView = {
        let tableView = UITableView()
        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_ArtistShowSongTableViewCell.self, forCellReuseIdentifier: MPPositive_ArtistShowSongTableViewCellID)
        tableView.register(MPPositive_ArtistShowListableViewCell.self, forCellReuseIdentifier: MPPositive_ArtistShowListableViewCellID)
        tableView.register(MPPositive_ArtistDescriptionTableViewCell.self, forCellReuseIdentifier: MPPositive_ArtistDescriptionTableViewCellID)
        //添加一个上拉加载
        let footer = MJRefreshAutoGifFooter {
            [weak self] in
            self?.footerRefresh()
        }
        footer.setTitle("Pull Up", for: .idle)
        footer.setTitle("Release Refresh", for: .pulling)
        footer.setTitle("Loading...", for: .refreshing)
        footer.setTitle("No Data", for: .noMoreData)
        tableView.mj_footer = footer
        return tableView
    }()
    private let MPPositive_ArtistShowSongTableViewCellID = "MPPositive_ArtistShowSongTableViewCell"
    private let MPPositive_ArtistShowListableViewCellID = "MPPositive_ArtistShowListableViewCell"
    private let MPPositive_ArtistDescriptionTableViewCellID = "MPPositive_ArtistDescriptionTableViewCell"
    private var sectionList:MPPositive_ArtistContentListViewModel!{
        didSet{
            if sectionList != nil {
                //检索当前数据是否为全部
                if sectionList.browseId != nil && sectionList.params != nil {
                    //当前数据并不完整，需要初次补全一部分
                    sectionList.requestArtistType()
                }else {
                    //当前数据完整
                    tableView.reloadData()
                    tableView.mj_footer?.endRefreshing()
                }
                sectionList.completionBlock = {
                    [weak self] in
                    self?.tableView.reloadData()
                    self?.tableView.mj_footer?.endRefreshing()
                }
            }else {
                tableView.reloadData()
            }
        }
    }
    var header:MPPositive_ArtistHeaderModel!
    //scroll滚动回调
    fileprivate var listViewDidScrollCallback: ((UIScrollView) -> ())?
    //选中内容
    var chooseItemBlock:((MPPositive_BrowseItemViewModel) -> Void)?
    init(frame: CGRect, list:MPPositive_ArtistContentListViewModel) {
        super.init(frame: frame)
        backgroundColor = .init(hex: "#151718")
        //赋值
        DispatchQueue.main.async {
            [weak self] in
            self?.sectionList = list
        }
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    //上拉刷新
    @objc private func footerRefresh() {
        guard sectionList.continuation != nil, sectionList.itct != nil else {
            tableView.mj_footer?.endRefreshingWithNoMoreData()
            return
        }
        sectionList.requestArtistContinuation()
    }
    
    //MARK: - 遵循分页代理
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        listViewDidScrollCallback?(scrollView)
    }
    //获取当前视图
    func listView() -> UIView {
        return self
    }
    //获取当前视图中的ScrollView
    func listScrollView() -> UIScrollView {
        return tableView
    }
    //当前视图滚动时回调闭包
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        listViewDidScrollCallback = callback
    }
}
//MARK: - tableView
extension MPPositive_ArtistShowTypeView:UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let first = sectionList?.itemViews.first {
            switch first.browseItem.itemType {
            case .single:
                return sectionList.itemViews.count
            case .list:
                return 1
            case .artist:
                return 2
            default:
                return 0
            }
        }else {
            return 0
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let first = sectionList?.itemViews.first {
            switch first.browseItem.itemType {
            case .single:
                let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistShowSongTableViewCellID) as! MPPositive_ArtistShowSongTableViewCell
                cell.itemView = sectionList.itemViews[indexPath.row]
                return cell
            case .list:
                let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistShowListableViewCellID, for: indexPath) as! MPPositive_ArtistShowListableViewCell
                cell.itemViews = sectionList.itemViews
                cell.chooseItemBlock = {
                    [weak self] item in
                    guard let self = self else {return}
                    if chooseItemBlock != nil {
                        chooseItemBlock!(item)
                    }
                }
                cell.showType = .List
                return cell
            case .artist:
                switch indexPath.row {
                case 0:
                    let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistShowListableViewCellID, for: indexPath) as! MPPositive_ArtistShowListableViewCell
                    cell.itemViews = sectionList.itemViews
                    cell.chooseItemBlock = {
                        [weak self] item in
                        guard let self = self else {return}
                        if chooseItemBlock != nil {
                            chooseItemBlock!(item)
                        }
                    }
                    cell.showType = .Artist
                    return cell
                default:
                    let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistDescriptionTableViewCellID, for: indexPath) as! MPPositive_ArtistDescriptionTableViewCell
                    cell.titleLabel.text = header?.fordescription ?? ""
                    return cell
                }
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistShowSongTableViewCellID) as! MPPositive_ArtistShowSongTableViewCell
                
                return cell
            }
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_ArtistShowSongTableViewCellID) as! MPPositive_ArtistShowSongTableViewCell
            
            return cell
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let first = sectionList?.itemViews.first {
            switch first.browseItem.itemType {
            case .single:
                //当前是单曲分页，点击cell可以直接触发事件块
                if chooseItemBlock != nil {
                    chooseItemBlock!(sectionList.itemViews[indexPath.row])
                }
            case .list:
                //当前是歌单专辑分页，用户点击不能触发
                break
            case .artist:
                //当前是艺术家页，用户点击不能触发
                break
            default:
                break
            }
        }
    }
}
