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


import ObjectMapper
class TSActionInfoModel: TSBaseModel {
    
    enum ModelType:Int {
        case normal = 0
        case example
    }
    
    enum ActionStatus:String ,Equatable {
        case success = "success"//成功
        case pending = "pending"//等待
        case running = "running"//运行
        case failed = "failed"//失败
        
        static func from(_ string: String) -> ActionStatus {
            return ActionStatus(rawValue: string) ?? .failed
        }
    }
    var uuid:String = UUID().uuidString
    var modelType:ModelType = .normal
    var id:Int = 0
    var actionType:String = ""
    var comments:String = ""
    var request:TSActionRequestModel = TSActionRequestModel()
    var response:TSActionResponseModel = TSActionResponseModel()
    var createdTimestamp:Int = 0
    var status:String = ""
    var costTime:Int = 0
    var percent:Float = 0.0
    var actionStatus:ActionStatus = .failed


    override func mapping(map: ObjectMapper.Map) {
        modelType           <- map["modelType"]
        id           <- map["id"]
        actionType   <- map["actionType"]
        comments     <- map["comments"]
        request      <- (map["request"],JsonStringTransform<TSActionRequestModel>())
        response           <- (map["response"],JsonStringTransform<TSActionResponseModel>())
        createdTimestamp   <- map["createdTimestamp"]
        status     <- map["status"]
        costTime      <- map["costTime"]
        percent     <- map["percent"]
        actionStatus      <- map["actionStatus"]
        actionStatus = ActionStatus.from(status)

        uuid      <- map["uuid"]
    }
}

extension TSActionInfoModel {
    var isVideo:Bool{
        return false
//        return request.generatorStyle == TSGeneratorStyle.creatVideo
    }

    var upImageURLExpired:Bool{
        if request.imageUrl.count == 0 {
            return true
        }
        
        if request.imageUrlTimestamp <= 0 {
            return true
        }
        //86400=24小时
        if Date.timestampInt - request.imageUrlTimestamp > 86400 {
            return true
        }
        
        return false
    }
}

class TSActionRequestModel : TSBaseModel {
    var prompt:String = ""
    var inputText:String = ""  //用户自己输入的内容
    var width:Int = 0
    var height:Int = 0
    
    var imageUrl:String = ""
    var imageUrlTimestamp:Int = 0
    var generatorStyle:TSGeneratorStyle = .ptp
    var model:String = ""   //决定生图的模型
    var maskUrl:String = ""
    var imageUrls:[String] = []

    override func mapping(map: ObjectMapper.Map) {
        prompt              <- map["prompt"]
        inputText           <- map["inputText"]
        width               <- map["width"]
        height              <- map["height"]
        imageUrls            <- map["imageUrls"]
        imageUrl            <- map["imageUrl"]
        imageUrlTimestamp   <- map["imageUrlTimestamp"]
        generatorStyle      <- map["generatorStyle"]
        model               <- map["model"]
        maskUrl             <- map["maskUrl"]
    }
}

class TSActionResponseModel : TSBaseModel {
    var resultUrl:String = ""
    var previewUrl:String = ""
    var videoDocument:String = ""
    
    
    var code:Int = 0
    var vip:Bool = false
    
    override func mapping(map: ObjectMapper.Map) {
        resultUrl           <- map["resultUrl"]
        previewUrl           <- map["previewUrl"]
        videoDocument           <- map["videoDocument"]
   
        vip                 <- map["vip"]
        code                <- map["code"]
    }
    
    //获取生成错误 code 对应的文案
    var codeErrorMsg:String {
        TSNetWorkCode.errorMsg(code: code)
    }
    //敏感错误
    var sensitiveError:Bool {
        return TSNetWorkCode.sensitiveError(code: code)
    }
    
//    var videoURL: URL {
//        return videoDocument.fillDocumentURL
//    }
    
}

