//
//  TSDBActionInfoModel.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2025/6/10.
//


import RealmSwift

class TSDBActionInfoModel: Object {
 
    @Persisted(primaryKey: true) var primaryKey: String = UUID().uuidString
    @Persisted var createdAt: Date = Date()
    
    @Persisted var modelType:Int = 0
    @Persisted var id:Int = 0
    @Persisted var actionType:String = ""
    @Persisted var comments:String = ""
    @Persisted var request:TSDBActionRequestModel?
    @Persisted var response:TSDBActionResponseModel?
    @Persisted var createdTimestamp:Int = 0
    @Persisted var status:String = ""   //success,pending,running,failed
    @Persisted var costTime:Int = 0
    @Persisted var percent:Float = 0.0
    
    @Persisted var uuid:String = UUID().uuidString
    
    static func createDBModel(actionInfoModel:TSActionInfoModel) -> TSDBActionInfoModel{
        if let dbModel = TSRMShared.realm.object(ofType: TSDBActionInfoModel.self, forPrimaryKey: actionInfoModel.uuid) {
            TSRMShared.writeThread { realm in
                dbModel.saveData(infoModel: actionInfoModel)
            }
            return dbModel
        }else{
            let dbModel = TSDBActionInfoModel()
            dbModel.primaryKey = actionInfoModel.uuid
            dbModel.saveData(infoModel: actionInfoModel)
            return dbModel
        }
    }
    
    func saveData(infoModel:TSActionInfoModel) {
        self.modelType = infoModel.modelType.rawValue
        self.id = infoModel.id
        self.actionType = infoModel.actionType
        
        self.comments = infoModel.comments

        self.createdTimestamp = infoModel.createdTimestamp
        self.status = infoModel.status
        self.costTime = infoModel.costTime
        self.percent = infoModel.percent
        self.uuid = infoModel.uuid
        
        self.request = TSDBActionRequestModel.createDBModel(requestModel: infoModel.request)
        self.response = TSDBActionResponseModel.createDBModel(responseModel: infoModel.response)
        
    }
    
    func getModel()->TSActionInfoModel{
        let infoModel = TSActionInfoModel()
        
        infoModel.modelType = TSActionInfoModel.ModelType.init(rawValue: self.modelType) ?? .normal
        infoModel.id = self.id
        infoModel.actionType = self.actionType
        infoModel.comments = self.comments
        
        infoModel.createdTimestamp = self.createdTimestamp
        infoModel.status = self.status
        infoModel.costTime = self.costTime
        infoModel.percent = self.percent
        infoModel.uuid = self.uuid

        if let request = self.request {
            infoModel.request = request.getModel()
        }
        
        if let response = self.response {
            infoModel.response = response.getModel()
        }
        
        infoModel.actionStatus = TSActionInfoModel.ActionStatus.from(infoModel.status)
        return infoModel
    }
    
    
    var isResult:Bool {
        if status.count > 0 {
            if status == "pending" ||
               status == "running"
            {
                return false
            }
        }
        return true
    }

    func deleteResources() {
        //退出正在生成的任务
        TSGenerateBasePhotoOperationQueue.shared.cancelOperations(uuid: uuid)
        
        if let response = response {
            TSImageStoreTool.removeImage(urlString:response.resultUrl)
            TSImageStoreTool.removeImage(urlString:response.previewUrl)
            if !response.videoDocument.isEmpty {
//                TSFileManagerTool.removeItem(from: response.videoDocument.fillDocumentURL)
            }
            
        }
        
        if let request = request {
            TSImageStoreTool.removeImage(urlString:request.imageUrl)
        }
        
        
    }
}



class TSDBActionRequestModel : Object {
    
    @Persisted(primaryKey: true) var primaryKey: String = UUID().uuidString
    @Persisted var createdAt: Date = Date()
    
    @Persisted var prompt:String = ""
    @Persisted var inputText:String = ""  //用户自己输入的内容
    @Persisted var width:Int = 0
    @Persisted var height:Int = 0
    
    @Persisted var imageUrl:String = ""
    @Persisted var imageUrlTimestamp:Int = 0
    @Persisted var imageUrls = List<String>()//惰性加载特性
    @Persisted var maskUrl:String = ""
    
    
    @Persisted var model:String = ""
    @Persisted var generatorStyle:String = ""
    
    static func createDBModel(requestModel:TSActionRequestModel) -> TSDBActionRequestModel{
        let dbModel = TSDBActionRequestModel()
        dbModel.saveData(requestModel: requestModel)
        return dbModel
    }
    
    func saveData(requestModel:TSActionRequestModel) {
        self.prompt = requestModel.prompt
        self.inputText = requestModel.inputText
        self.width = requestModel.width
        self.height = requestModel.height
        
        self.imageUrl = requestModel.imageUrl
        self.imageUrlTimestamp = requestModel.imageUrlTimestamp

        self.model = requestModel.model
        self.generatorStyle = requestModel.generatorStyle.rawValue
        
        let list = List<String>()
        list.append(objectsIn: requestModel.imageUrls)
        self.imageUrls = list
        self.maskUrl = requestModel.maskUrl
    }
    
    
    func getModel()->TSActionRequestModel{
        let model = TSActionRequestModel()
        model.prompt = self.prompt
        model.inputText = self.inputText
        model.width = self.width
        model.height = self.height
        
        model.imageUrl = self.imageUrl
        model.imageUrlTimestamp = self.imageUrlTimestamp
        model.imageUrls = Array(self.imageUrls)
        model.maskUrl = self.maskUrl
        model.model = self.model
        model.generatorStyle = TSGeneratorStyle(rawValue: self.generatorStyle) ?? .ptp
        
        return model
    }

}

class TSDBActionResponseModel : Object {
    @Persisted(primaryKey: true) var primaryKey: String = UUID().uuidString
    @Persisted var createdAt: Date = Date()
    
    @Persisted var resultUrl:String = ""
    @Persisted var previewUrl:String = ""
    @Persisted var videoDocument:String = ""
    
    @Persisted var code:Int = 0
    @Persisted var vip:Bool = false
    
    static func createDBModel(responseModel:TSActionResponseModel) -> TSDBActionResponseModel{
        let dbModel = TSDBActionResponseModel()
        dbModel.saveData(responseModel: responseModel)
        return dbModel
    }
    
    func saveData(responseModel:TSActionResponseModel) {
        self.resultUrl = responseModel.resultUrl
        self.previewUrl = responseModel.previewUrl
        self.videoDocument = responseModel.videoDocument
        self.code = responseModel.code
        self.vip = responseModel.vip
    }
    
    
    func getModel()->TSActionResponseModel{
        let model = TSActionResponseModel()
        model.resultUrl = self.resultUrl
        model.previewUrl = self.previewUrl
        model.videoDocument = self.videoDocument
        model.code = self.code
        model.vip = self.vip
        return model
    }
    
}
