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

import UIKit

class MPPositive_SearchResultsShowView: UIView {
    ///搜索结果管理模型
    var loadModel:MPPositive_SearchResultsLoadViewModel!{
        didSet{
            DispatchQueue.main.async {
                [weak self] in
                guard let self = self else {return}
                if loadModel == nil {
                    isHidden = true
                }else {
                    loadModel.resultReloadBlock = {
                        [weak self] in
                        //刷新分页控制器
                        guard let self = self else {return}
                        isHidden = false
                        dataSource.titles = loadModel?.sectionLists.compactMap({$0.title}) ?? []
                        dataSource.reloadData(selectedIndex: 0)
                        segmentView.reloadData()
                        emptyImageView.isHidden = !(dataSource.titles.count == 0)
                    }
                }
            }
        }
    }
    //MARK: - 分页栏设置
    //分页栏View
    private lazy var segmentView:JXSegmentedView = {
        var jxSegmentView = JXSegmentedView()
        jxSegmentView.backgroundColor = .init(hex: "1A1A1A")
        return jxSegmentView
    }()
    //数据源
    private lazy var dataSource:JXSegmentedTitleDataSource = {
        var dataSource = JXSegmentedTitleDataSource()
        //标题未选中状态
        dataSource.titleNormalColor = .init(hex: "#666666")
        dataSource.titleNormalFont = .systemFont(ofSize: 16*width, weight: .regular)
        //标题选中状态
        dataSource.titleSelectedColor = .init(hex: "#80F988")
        dataSource.titleSelectedFont = .systemFont(ofSize: 16*width, weight: .bold)
        //是否颜色过度
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isSelectedAnimable = true
        dataSource.isItemTransitionEnabled = true
        return dataSource
    }()
    //指示器
    private lazy var indicator:JXSegmentedIndicatorLineView = {
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 16*width
        indicator.indicatorHeight = 3*width
        indicator.indicatorCornerRadius = 1.5*width
        indicator.indicatorColor = .init(hex: "#80F988")
        indicator.indicatorPosition = .bottom
        return indicator
    }()
    //分页内容承载框
    fileprivate lazy var listContainerView:JXSegmentedListContainerView = {
        let listContainerView = JXSegmentedListContainerView(dataSource: self)
        listContainerView.backgroundColor = .clear
        return listContainerView
    }()
    //数据空图片
    private lazy var emptyImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "empty"))
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    var scrollBlock:(() -> Void)?
    //选中内容
    var chooseItemBlock:((MPPositive_SearchResultItemViewModel) -> Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(hex: "1A1A1A")
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    //配置
    private func configure() {
        addSubview(emptyImageView)
        emptyImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(211*width)
            make.height.equalTo(172*width)
        }
        segmentView.indicators = [indicator]
        segmentView.dataSource = dataSource
        //配置View
        addSubview(segmentView)
        segmentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(60*width)
        }
        addSubview(listContainerView)
        listContainerView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview().priority(999)
            make.top.equalTo(segmentView.snp.bottom).priority(999)
        }
        segmentView.contentScrollView = listContainerView.scrollView
        segmentView.listContainer = listContainerView
    }
}
//MARK: - JXSegmentedTitleDataSource
extension MPPositive_SearchResultsShowView: JXSegmentedListContainerViewDataSource, JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return dataSource.titles.count
    }
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            //展示预览结果
            let showView:MPPositive_SearchResultPreviewShowView = .init(frame: listContainerView.frame, sectionLists: loadModel.sectionLists)
            showView.scrollBlock = {
                [weak self] in
                if self?.scrollBlock != nil {
                    self?.scrollBlock!()
                }
            }
            showView.chooseMoreIndexBlock = {
                [weak self] (selectedIndex) in
                guard let self = self else {
                    return
                }
                segmentView.selectItemAt(index: selectedIndex)
            }
            showView.chooseItemBlock = {
                [weak self] item in
                guard let self = self else {
                    return
                }
                if chooseItemBlock != nil {
                    chooseItemBlock!(item)
                }
            }
            return showView
        }else {
            //展示分类结果
            let showView:MPPositive_SearchResultTypeShowView = .init(frame: listContainerView.frame, list: loadModel.sectionLists[index])
            showView.scrollBlock = {
                [weak self] in
                if self?.scrollBlock != nil {
                    self?.scrollBlock!()
                }
            }
            return showView
        }
    }
}
