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

import ObjectMapper
class TSDiscoverVM {

    var colDataArray:[TSComponent] = [TSComponent]()
    
    lazy var contentSecitonModel: TSColVVMSectionModel = {
        let contentSeciton = TSColVVMSectionModel()
        contentSeciton.style = .ringCategories
        return contentSeciton
    }()
    
    
    var listModelArray: [TSRingCategoryModel] = []
    
    init() {
        combinedData()
    }
    
    func combinedData(){
        colDataArray.removeAll()
        colDataArray.append(contentSecitonModel)
    }
    
    weak var animationView:UIView?
    var pageSize:Int = kPageSize
    var pageNum:Int = kPageNum


}

extension TSDiscoverVM {
    func setContentItemModels(){
        var array = [TSColVVMItemModel]()
        listModelArray.forEach { themeModel in
            let itemModel = TSColVVMItemModel()
            itemModel.dataModel = themeModel
            itemModel.style = .ringCategories
            array.append(itemModel)
        }
        contentSecitonModel.items = array
    }
}
 
extension TSDiscoverVM {
    
    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:.ringCategories)
        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)
            }
        }
    }

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


