//
//  TSDiscoverListVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/17.
//


import ObjectMapper
class TSDiscoverListVM {

//    var colDataArray:[TSComponent] = [TSComponent]()
//    
//    lazy var contentSecitonModel: TSColVVMSectionModel = {
//        let contentSeciton = TSColVVMSectionModel()
//        contentSeciton.style = .ringList
//        return contentSeciton
//    }()
//    
//    
  
    lazy var modelList: [TSAIRintoneHistoryModel] = {
        return [discoverList]
    }()
    
    lazy var discoverList: TSAIRintoneHistoryModel = {
        let model = TSAIRintoneHistoryModel(title: "Generate History", list:[])
        return model
    }()
    
//    var listModelArray: [TSRingModel] = []
    
    
    init(categoryID: String,animationView:UIView) {
        self.categoryID = categoryID
        self.animationView = animationView
        combinedData()
    }
    
    func combinedData(){
    }
    
    weak var animationView:UIView?
    var pageSize:Int = kPageSize
    var pageNum:Int = kPageNum
    var categoryID:String

}

extension TSDiscoverListVM {
    
    func getData(complete:@escaping (Bool)->Void){
        pageNum = kPageNum
        getNetWorkData(useCache: true,animationView: animationView) { [weak self] modelArray,success  in
            guard let self = self else { return }
            if modelArray.count > 0 {
                discoverList.list = modelArray
                complete(true)
            }else{
                complete(false)
            }
        }
    }
    
    /// 上拉刷新
    /// - Parameter complete: (是否成功)
    func reloadAllData(complete:@escaping (Bool)->Void){
        TSNetworkShared.removeCache(urlType:.rings)
        pageNum = kPageNum
        getNetWorkData(useCache: false,animationView: nil) {[weak self] modelArray,success  in
            guard let self = self else { return }
            if modelArray.count > 0 {
                discoverList.list = modelArray
                complete(true)
            }else{
                complete(false)
            }
        }
    }
    
    /// 获取更多数据
    /// - Parameter complete: (是否成功,还有更多数据)
    func getMoreData(complete:@escaping (Bool,Bool)->Void){
        pageNum+=1
        getNetWorkData(useCache: true,animationView: nil) { [weak self] modelArray,success in
            guard let self = self else { return }
            if modelArray.count > 0 {
                discoverList.list = discoverList.list + modelArray
                complete(success,true)
            }else{
                complete(success,false)
            }
        }
    }

    func getNetWorkData(useCache:Bool,animationView:UIView?,complete:@escaping ([TSRingModel],Bool)->Void){
        
        let parameters:[String:Any] = [
            "page_size":pageSize,
            "page":pageNum,
            "category_id":categoryID,
        ]
        
        _ = TSNetworkShared.get(
            urlType: .rings,
            parameters: parameters,
            animationView: animationView,
            useCache: useCache)
        {  data, error in
            if let result = kNetWorkResultArraySuccess(data: data) {
                let modelArray = Mapper<TSRingModel>().mapArray(JSONArray: result)
                complete(modelArray,true)
            }else{
                complete([],false)
            }
        }
    }
    
}



//
//extension TSDiscoverListVM {
//
//    
////    func setContentItemModels(){
////        var array = [TSColVVMItemModel]()
////        listModelArray.forEach { themeModel in
////            array.append(getTSColVVMItemModel(model: themeModel))
////        }
////        contentSecitonModel.items = array
////    }
////    
////    func appendContentItemModels(modelArray:[TSRingModel]){
////        var array = [TSColVVMItemModel]()
////        modelArray.forEach { themeModel in
////            array.append(getTSColVVMItemModel(model: themeModel))
////        }
////        contentSecitonModel.items = contentSecitonModel.items + array
////    }
////    
////    func getTSColVVMItemModel(model:TSRingModel)->TSColVVMItemModel{
////        let itemModel = TSColVVMItemModel()
////        itemModel.dataModel = model
////        itemModel.style = .ringList
////        return itemModel
////    }
//}
// 
//extension TSDiscoverListVM {
//    
//    func getData(complete:@escaping (Bool)->Void){
//        pageNum = kPageNum
//        getNetWorkData(useCache: true,animationView: animationView) { [weak self] modelArray,success  in
//            guard let self = self else { return }
//            if modelArray.count > 0 {
//                listModelArray.removeAll()
//                listModelArray.append(contentsOf: modelArray)
//                setContentItemModels()
//                complete(true)
//            }else{
//                complete(false)
//            }
//        }
//    }
//    
//    /// 上拉刷新
//    /// - Parameter complete: (是否成功)
//    func reloadAllData(complete:@escaping (Bool)->Void){
//        TSNetworkShared.removeCache(urlType:.rings)
//        pageNum = kPageNum
//        getNetWorkData(useCache: false,animationView: nil) {[weak self] modelArray,success  in
//            guard let self = self else { return }
//            if modelArray.count > 0 {
//                listModelArray.removeAll()
//                listModelArray.append(contentsOf: modelArray)
//                setContentItemModels()
//                complete(true)
//            }else{
//                complete(false)
//            }
//        }
//    }
//    
//    /// 获取更多数据
//    /// - Parameter complete: (是否成功,还有更多数据)
//    func getMoreData(complete:@escaping (Bool,Bool)->Void){
//        pageNum+=1
//        getNetWorkData(useCache: true,animationView: nil) { [weak self] modelArray,success in
//            guard let self = self else { return }
//            if modelArray.count > 0 {
//                listModelArray.append(contentsOf: modelArray)
////                setContentItemModels()
//                appendContentItemModels(modelArray: modelArray)
//                complete(success,true)
//            }else{
//                complete(success,false)
//            }
//        }
//    }
//
//    func getNetWorkData(useCache:Bool,animationView:UIView?,complete:@escaping ([TSRingModel],Bool)->Void){
//        
//        let parameters:[String:Any] = [
//            "page_size":pageSize,
//            "page":pageNum,
//            "category_id":categoryID,
//        ]
//        
//        _ = TSNetworkShared.get(
//            urlType: .rings,
//            parameters: parameters,
//            animationView: animationView,
//            useCache: useCache)
//        {  data, error in
//            if let result = kNetWorkResultArraySuccess(data: data) {
//                let modelArray = Mapper<TSRingModel>().mapArray(JSONArray: result)
//                complete(modelArray,true)
//            }else{
//                complete([],false)
//            }
//        }
//    }
//    
//}
//

