//
//  MPPositive_SearchResultPreviewShowView.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/13.
//

import UIKit
///对搜索预览结果展示
class MPPositive_SearchResultPreviewShowView: 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)
        return tableView
    }()
    private let MPPositive_SearchResultShowTableViewCellID = "MPPositive_SearchResultShowTableViewCell"
    private var sectionLists:[MPPositive_SearchResultListViewModel]!{
        didSet{
            DispatchQueue.main.async {
                [weak self] in
                guard let self = self else {return}
                if sectionLists != nil {
                    tableView.reloadData()
                }
            }
        }
    }
    var scrollBlock:(() -> Void)?
    var chooseMoreIndexBlock:((Int) -> Void)?
    //选中内容
    var chooseItemBlock:((MPPositive_SearchResultItemViewModel) -> Void)?
    init(frame: CGRect, sectionLists:[MPPositive_SearchResultListViewModel]) {
        super.init(frame: frame)
        backgroundColor = .init(hex: "1A1A1A")
        self.sectionLists = sectionLists
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    //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 moreFindClick(_ sender:UIButton) {
        let selectedTag = sender.tag
        if chooseMoreIndexBlock != nil {
            chooseMoreIndexBlock!(selectedTag)
        }
    }
}
//MARK: - tableView
extension MPPositive_SearchResultPreviewShowView:UITableViewDataSource, UITableViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        if scrollBlock != nil {
            scrollBlock!()
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionLists.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionLists[section].previewItemViews.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_SearchResultShowTableViewCellID, for: indexPath) as! MPPositive_SearchResultShowTableViewCell
        cell.itemView = sectionLists[indexPath.section].previewItemViews[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 50*width))
        sectionView.backgroundColor = .init(hex: "#1A1A1A")
        let label = createLabel(sectionLists[section].title, font: .systemFont(ofSize: 16*width, weight: .semibold), textColor: .white, textAlignment: .left)
        sectionView.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18*width)
            make.centerY.equalToSuperview()
        }
        if section != 0 {
            //更多按钮
            let btn:UIButton = .init()
            btn.setBackgroundImage(UIImage(named: "Home_More'logo"), for: .normal)
            btn.tag = section
            btn.addTarget(self, action: #selector(moreFindClick(_ :)), for: .touchUpInside)
            sectionView.addSubview(btn)
            btn.snp.makeConstraints { make in
                make.width.height.equalTo(24*width)
                make.right.equalToSuperview().offset(-18*width)
                make.centerY.equalToSuperview()
            }
        }
        return sectionView
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50*width
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if chooseItemBlock != nil {
            chooseItemBlock!(sectionLists[indexPath.section].previewItemViews[indexPath.row])
        }
    }
}
