//
//  TSUserDefaultData.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/5.
//
import ObjectMapper


//海报历史记录
class TSTextToPicHistory{
    @UserDefault(key: "textPicHistoryListString", defaultValue: "")
    static private var historyString: String
    static var listModelArray: [TSGenmojiModel] = {
        
//        if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
//            insertExampleData()
//            UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
//            UserDefaults.standard.synchronize()
//        }
        
        if let listModelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: historyString){
            return listModelArray
        }
        return []
    }()
    
    static func saveModel(model:TSGenmojiModel){
        listModelArray.insert(model, at: 0)
        saveHistoryString()
    }
    
    static func removeModel(model:TSGenmojiModel){
        listModelArray.removeAll { $0 === model }
        saveHistoryString()
    }
    
    static func removeIndex(index:Int){
        listModelArray.remove(at: index)
        saveHistoryString()
    }
    
    static func removeAll(){
        listModelArray.removeAll()
        saveHistoryString()
    }
    
    static func saveHistoryString(){
        if let jsonString = listModelArray.toJSONString() {
            historyString = jsonString
        }
    }
    
//    private static func insertExampleData(){
//        let array = [
//            createExampleModel(imageName: "poster_example_0"),
//            createExampleModel(imageName: "poster_example_1"),
//            createExampleModel(imageName: "poster_example_2")
//        ]
//        if let jsonString = array.toJSONString() {
//            historyString = jsonString
//        }
//    }
    
//    private static func createExampleModel(imageName:String)->TSActionInfoModel{
//        let model = TSActionInfoModel()
//        model.modelType = .example
//        model.request.prompt = "Example"
//        model.request.promptSort = "Example"
//        model.request.width = kTextPicW
//        model.request.height = kTextPicH
//        model.response.resultUrl = imageName
//        return model
//    }
}
