//
//  TSUserDefaultData1.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/24.
//


import Foundation
import ObjectMapper

// MARK: - 基类
class HistoryManager<T: Mappable> {
    private let userDefaultsKey: String
    private var historyString: String {
        get {
            return UserDefaults.standard.string(forKey: userDefaultsKey) ?? ""
        }
        set {
            UserDefaults.standard.set(newValue, forKey: userDefaultsKey)
        }
    }
    
    var listModelArray: [T] {
        get {
            if let listModelArray = Mapper<T>().mapArray(JSONString: historyString) {
                return listModelArray
            }
            return []
        }
        set {
            if let jsonString = newValue.toJSONString() {
                historyString = jsonString
            }
        }
    }
    
    init(userDefaultsKey: String) {
        self.userDefaultsKey = userDefaultsKey
    }
    
    func saveModel(model: T) {
        var models = listModelArray
        if let index = models.firstIndex(where: { $0.toJSONString() == model.toJSONString() }) {
            models[index] = model
        } else {
            models.insert(model, at: 0)
        }
        listModelArray = models
    }
    
    func removeModel(model: T) {
        var models = listModelArray
        models.removeAll { $0.toJSONString() == model.toJSONString() }
        listModelArray = models
    }
    
    func removeIndex(index: Int) {
        var models = listModelArray
        models.remove(at: index)
        listModelArray = models
    }
    
    func replaceModel(oldID: Int, newModel: T) {
        var models = listModelArray
        if let index = models.firstIndex(where: { ($0 as? TSActionInfoModel)?.id == oldID }) {
            models[index] = newModel
        } else {
            models.insert(newModel, at: 0)
        }
        listModelArray = models
    }
}

// MARK: - 子类实现
// 海报历史记录
class TSPosterHistory: HistoryManager<TSActionInfoModel> {
    static let shared = TSPosterHistory(userDefaultsKey: "kPosterTextPicHistoryListString")
    
    private override init(userDefaultsKey: String) {
        super.init(userDefaultsKey: userDefaultsKey)
        if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
            UserDefaults.standard.synchronize()
        }
    }
    
    private func insertExampleData() {
        let array = [
            createExampleModel(imageName: "poster_example_0"),
            createExampleModel(imageName: "poster_example_1"),
            createExampleModel(imageName: "poster_example_2")
        ]
        if let jsonString = array.toJSONString() {
            listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: jsonString) ?? []
        }
    }
    
    private 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: HistoryManager<TSActionInfoModel> {
    static let shared = TSPhotoHistory(userDefaultsKey: "kPhotoTextPicHistoryListString")
    
    private override init(userDefaultsKey: String) {
        super.init(userDefaultsKey: userDefaultsKey)
        if UserDefaults.standard.string(forKey: "insertPhotoExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertPhotoExampleData")
            UserDefaults.standard.synchronize()
        }
    }
    
    private func insertExampleData() {
        let array = [
            createExampleModel(imageName: "photo_example_0"),
            createExampleModel(imageName: "photo_example_1"),
            createExampleModel(imageName: "photo_example_2")
        ]
        if let jsonString = array.toJSONString() {
            listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: jsonString) ?? []
        }
    }
    
    private 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: HistoryManager<TSActionInfoModel> {
    static let shared = TSAIRintoneHistory(userDefaultsKey: "kRintoneTextMusicHistoryListString")
    
    private override init(userDefaultsKey: String) {
        super.init(userDefaultsKey: userDefaultsKey)
        if UserDefaults.standard.string(forKey: "insertRintoneExampleData") == nil {
            insertExampleData()
            UserDefaults.standard.set("1", forKey: "insertRintoneExampleData")
            UserDefaults.standard.synchronize()
        }
    }
    
    private func insertExampleData() {
        let array = [
            createExampleModel()
        ]
        if let jsonString = array.toJSONString() {
            listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: jsonString) ?? []
        }
    }
    
    private 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://example.com/cover"
        model.response.title = "Glass"
        model.response.musicUrl = "http://example.com/music"
        return model
    }
}

// 我的下载铃声历史记录
class TSMineRintoneHistory: HistoryManager<TSRingModel> {
    static let shared = TSMineRintoneHistory(userDefaultsKey: "kMineRintoneHistoryListString")
    
    var isHaveNew: Bool = false
}
