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


//海报历史记录
class TSPosterHistory{
    @UserDefault(key: "kPosterTextPicHistoryListString", defaultValue: "")
    static private var historyString: String
    static var listModelArray: [TSActionInfoModel] = {
        
        if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
            UserDefaults.standard.synchronize()
        }
        
        if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
            return listModelArray
        }
        return []
    }()
    
    static func saveModel(model:TSActionInfoModel){
        self.listModelArray.insert(model, at: 0)
        self.saveHistoryString()
    }
    
    static func removeModel(model:TSActionInfoModel){
        self.listModelArray.removeAll { $0 === model }
        self.saveHistoryString()
    }
    
    static func removeIndex(index:Int){
        self.listModelArray.remove(at: index)
        self.saveHistoryString()
    }
    
    static func saveHistoryString(){
        if let jsonString = listModelArray.toJSONString() {
            historyString = jsonString
        }
    }
    
    private static func insertExampleData(){
        let array = [
            self.createExampleModel(imageName: "poster_example_0"),
            self.createExampleModel(imageName: "poster_example_1"),
            self.createExampleModel(imageName: "poster_example_2")
        ]
        if let jsonString = array.toJSONString() {
            self.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
    }
}

//头像历史记录
class TSPhotoHistory{
    @UserDefault(key: "kPhotoTextPicHistoryListString", defaultValue: "")
    static private var historyString: String
    static var listModelArray: [TSActionInfoModel] = {
        if UserDefaults.standard.string(forKey: "insertPhotoExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertPhotoExampleData")
            UserDefaults.standard.synchronize()
        }
        if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
            return listModelArray
        }
        return []
    }()
    
    static func saveModel(model:TSActionInfoModel){
        self.listModelArray.insert(model, at: 0)
        self.saveHistoryString()
    }
    
    static func removeModel(model:TSActionInfoModel){
        self.listModelArray.removeAll { $0 === model }
        self.saveHistoryString()
    }
    
    static func removeIndex(index:Int){
        self.listModelArray.remove(at: index)
        self.saveHistoryString()
    }
    
    static func saveHistoryString(){
        if let jsonString = listModelArray.toJSONString() {
            self.historyString = jsonString
        }
    }
    
    private static func insertExampleData(){
        let array = [
            self.createExampleModel(imageName: "photo_example_0"),
            self.createExampleModel(imageName: "photo_example_1"),
            self.createExampleModel(imageName: "photo_example_2")
        ]
        if let jsonString = array.toJSONString() {
            self.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 = kTextPicW
        model.response.resultUrl = imageName
        return model
    }
}

//AI铃声历史记录
class TSAIRintoneHistory{
    @UserDefault(key: "kRintoneTextMusicHistoryListString", defaultValue: "")
    static private var historyString: String
    static var listModelArray: [TSActionInfoModel] = {
        
        if UserDefaults.standard.string(forKey: "insertRintoneExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertRintoneExampleData")
            UserDefaults.standard.synchronize()
        }
        
        if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
            return listModelArray
        }
        return []
    }()
    
    static func saveModel(model:TSActionInfoModel){
        self.listModelArray.insert(model, at: 0)
        self.saveHistoryString()
    }
    
    static func removeModel(model:TSActionInfoModel){
        self.listModelArray.removeAll { $0 === model }
        self.saveHistoryString()
    }
    
    static func removeIndex(index:Int){
        self.listModelArray.remove(at: index)
        self.saveHistoryString()
    }
    
    static func saveHistoryString(){
        if let jsonString = listModelArray.toJSONString() {
            self.historyString = jsonString
        }
    }
    

    private static func insertExampleData(){
        let array = [
            self.createExampleModel(),
        ]
        if let jsonString = array.toJSONString() {
            historyString = jsonString
        }
    }
    
    private static func createExampleModel()->TSActionInfoModel{
        let model = TSActionInfoModel()
        model.modelType = .example
        model.request.duration = 30
        model.request.prompt = "Example"
        model.request.promptSort = "Example"
        model.response.coverUrl = "http://d3a93z8fj970a4.cloudfront.net/53337c41-9610-4b92-a8fc-0871a0895880"
        model.response.title = "Glass"
        model.response.musicUrl = "http://d3a93z8fj970a4.cloudfront.net/328324a2-2576-4f55-8c77-2fa16b88e043"
        return model
    }
}


//AI铃声历史记录
class TSMineRintoneHistory{
    @UserDefault(key: "kMineRintoneHistoryListString", defaultValue: "")
    static private var historyString: String
    static var listModelArray: [TSRingModel] = {

        if let listModelArray = Mapper<TSRingModel>().mapArray(JSONString: historyString){
            return listModelArray
        }
        return []
    }()
    
    static func saveModel(model:TSRingModel){
        //若存在,则不保存
        if let _ = self.listModelArray.first(where: { $0.audioUrl == model.audioUrl }) {
            return
        }
//        TSToastShared.showToast(text: "保存成功")
        self.listModelArray.insert(model, at: 0)
        self.saveHistoryString()
        isHaveNew = true
    }
    
    static func removeModel(model:TSRingModel){
        listModelArray.removeAll { $0 === model }
        self.saveHistoryString()
    }
    
    static func removeIndex(index:Int){
        listModelArray.remove(at: index)
        self.saveHistoryString()
    }
    
    static func saveHistoryString(){
        if let jsonString = listModelArray.toJSONString() {
            historyString = jsonString
        }
    }
    
    
    static var isHaveNew:Bool = false
}
