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

import UIKit
///搜索结果模块列表
class MPPositive_SearchResultListViewModel: NSObject {
    ///模块标题
    var title:String!
    ///预览内容模块组（只展示预览部分内容/通常是3-4个）
    var previewItemViews:[MPPositive_SearchResultItemViewModel] = []
    ///搜索文本(非最佳结果模块才有)
    var query:String?
    ///搜索键值(非最佳结果模块才有)
    var params:String?
    ///当前键值下全部的内容模块组
    var allItemViews:[MPPositive_SearchResultItemViewModel] = []{
        didSet{
            print("用户加载\(title ?? "")的新内容")
            if completionBlock != nil {
                completionBlock!()
            }
        }
    }
    ///当前分页信息
    var continuation:String?
    ///当前分页键值
    var itct:String?
    ///请求完成
    var completionBlock:(() -> Void)?
    ///请求分类详情数据
    func requestSearchType() {
        guard let query = self.query, let params = self.params else {
            print("当前\(title ?? "")已是全部内容")
            return
        }
        //调用搜索分页结果接口补全全部的内容模块组
        MP_NetWorkManager.shared.requestSearchTypeResults(query, params: params) { [weak self] (items, continuation, itct) in
            guard let self = self else {return}
            //对全部的内容模块组进行追加
            allItemViews.append(contentsOf: items)
            //记录分页内容
            self.continuation = continuation
            self.itct = itct
        }
    }
    ///请求分类继续内容
    func requestSearchContinuation() {
        guard let continuation = self.continuation, let itct = self.itct else {
            //没有更多了
            return
        }
        MP_NetWorkManager.shared.requestSearchTypeContinuation(continuation, itct: itct) { [weak self] (items, continuation, itct) in
            guard let self = self else {return}
            //对全部的内容模块组进行追加
            allItemViews.append(contentsOf: items)
            //记录分页内容
            self.continuation = continuation
            self.itct = itct
        }
    }
}
