//
//  TSThemeVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/2/28.
//

import ObjectMapper
class TSThemeVM {

    var colDataArray:[TSComponent] = [TSComponent]()
    
    lazy var generateSectionModel: TSColVVMSectionModel = {
        let generateSectionModel = TSColVVMSectionModel()
        generateSectionModel.name = ""
        generateSectionModel.items = [TSColVVMItemModel()]
        
        generateSectionModel.style = .themeGuide
        return generateSectionModel
    }()
    
    lazy var contentSecitonModel: TSColVVMSectionModel = {
        let contentSeciton = TSColVVMSectionModel()
        contentSeciton.style = .themeContent
        return contentSeciton
    }()
    
    
    var listModelArray: [TSThemeModel] = []
    
    init() {
        combinedData()
    }
    
    func combinedData(){
        colDataArray.removeAll()
        colDataArray.append(generateSectionModel)
        colDataArray.append(contentSecitonModel)
    }
    
    weak var animationView:UIView?
    var pageSize:Int = kPageSize
    var pageNum:Int = kPageNum


}

extension TSThemeVM {
    func setContentItemModels(){
        var array = [TSColVVMItemModel]()
        listModelArray.forEach { themeModel in
            let itemModel = TSColVVMItemModel()
            itemModel.dataModel = themeModel
            itemModel.style = .themeContent
            array.append(itemModel)
        }
        contentSecitonModel.items = array
    }
}
 
extension TSThemeVM {
    
    func getThemeData(complete:@escaping (Bool)->Void){
        pageNum = kPageNum
        getThemeNetWorkData(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:.themes)
        pageNum = kPageNum
        getThemeNetWorkData(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
        getThemeNetWorkData(useCache: true,animationView: nil) { [weak self] modelArray,success in
            guard let self = self else { return }
            if modelArray.count > 0 {
                listModelArray.append(contentsOf: modelArray)
                setContentItemModels()
                complete(success,true)
            }else{
                complete(success,false)
            }
        }
    }
    
    func getThemeNetWorkData(useCache:Bool,animationView:UIView?,complete:@escaping ([TSThemeModel],Bool)->Void){
        
        let parameters:[String:Any] = [
            "page_size":pageSize,
            "page":pageNum
        ]
        
        _ = TSNetworkShared.get(
            urlType: .themes,
            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<TSThemeModel>().mapArray(JSONArray: result)
                complete(modelArray,true)
            }else{
                complete([],false)
            }
        }
    }
    
}

