//
//  MPPositive_SearchResultTypeShowView.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/14.
//

import UIKit
import MJRefresh
///对搜索结果进行分类详情展示
class MPPositive_SearchResultTypeShowView: UIView, JXSegmentedListContainerViewListDelegate {
    //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_SearchResultShowTableViewCell.self, forCellReuseIdentifier: MPPositive_SearchResultShowTableViewCellID)
        //添加一个上拉加载
        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_SearchResultShowTableViewCellID = "MPPositive_SearchResultShowTableViewCell"
    //对应的分类详情展示
    var sectionList:MPPositive_SearchResultListViewModel!{
        didSet{
            if sectionList != nil {
                if sectionList.allItemViews.count == 0 {
                    //开始请求内容
                    sectionList.requestSearchType()
                }
                sectionList.completionBlock = {
                    [weak self] in
                    self?.tableView.reloadData()
                    self?.tableView.mj_footer?.endRefreshing()
                }
            }else {
                tableView.reloadData()
            }
        }
    }
    var scrollBlock:(() -> Void)?
    
    init(frame: CGRect, list:MPPositive_SearchResultListViewModel) {
        super.init(frame: frame)
        backgroundColor = .init(hex: "1A1A1A")
        DispatchQueue.main.async {
            [weak self] in
            self?.sectionList = list
        }
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    //MARK: - 分页代理
    func listView() -> UIView {
        return self
    }
    //配置
    private func configure() {
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
    }
    //上拉刷新
    @objc private func footerRefresh() {
        guard sectionList.continuation != nil, sectionList.itct != nil else {
            tableView.mj_footer?.endRefreshingWithNoMoreData()
            return
        }
        sectionList.requestSearchContinuation()
    }
}
//MARK: - tableView
extension MPPositive_SearchResultTypeShowView:UITableViewDataSource, UITableViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        if scrollBlock != nil {
            scrollBlock!()
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionList?.allItemViews.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_SearchResultShowTableViewCellID, for: indexPath) as! MPPositive_SearchResultShowTableViewCell
        cell.itemView = sectionList.allItemViews[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
}
