//
//  TSAIRintoneVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/6.
//
import ObjectMapper
class TSAIRintoneHistoryModel {
    var title:String = ""
    var list:[Any] = []
    var type:ModelStyle = .history
    
    enum ModelStyle:Int {
        case history
        case recommend
    }
    
    init(title: String, list: [Any],type:ModelStyle = .history) {
        self.title = title
        self.list = list
        self.type = type
    }
}

class TSAIRintoneVM {
    
    lazy var modelList: [TSAIRintoneHistoryModel] = {
        getModelList()
    }()
    
    private func getModelList()->[TSAIRintoneHistoryModel]{
        var list = [TSAIRintoneHistoryModel]()
        
        if aiRintoneHistoryModel.list.count > 0 {
            list.append(aiRintoneHistoryModel)
        }
        
        if recommendRingtonesModel.list.count > 0 {
            list.append(recommendRingtonesModel)
        }
        
        return list
    }
    
    //生成的历史
    lazy var aiRintoneHistoryModel: TSAIRintoneHistoryModel = {
        let model = TSAIRintoneHistoryModel(title: "Generate History", list:TSAIRintoneHistory.listModelArray,type: .history)
        return model
    }()
    

    //每日推荐的歌曲
    lazy var recommendRingtonesModel: TSAIRintoneHistoryModel = {
        let model = TSAIRintoneHistoryModel(title: "Recommend Ringtones", list:[],type: .recommend)
        return model
    }()

}


extension TSAIRintoneVM {
    
    func updateRecentData() {
        aiRintoneHistoryModel.list = Array(TSAIRintoneHistory.listModelArray.prefix(2))
        modelList = getModelList()
    }
    
    func removeModel(model:TSActionInfoModel){
        TSAIRintoneHistory.removeModel(model: model)
        updateRecentData()
    }
    
}

extension TSAIRintoneVM {
    func getRingRecommend(useCache:Bool,complete:@escaping ()->Void){
        _ = TSNetworkShared.get(urlType: .ringRecommend,useCache: useCache) { [weak self] data,error in
            guard let self = self else { return }
            if let result = kNetWorkResultArraySuccess(data: data) {
                if let list =  Mapper<TSRingModel>().mapArray(JSONObject: result) {
                    recommendRingtonesModel.list = list
                    updateRecentData()
                }
            }
            complete()
        }
    }
}
