//
//  TSChatMessageUIModel.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/21.
//

import ObjectMapper

enum TSChatMessageUIBaseModelStyle:String {
    case base
    case image
    case video

    func getModelClass() -> TSChatMessageUIBaseModel.Type {
        switch self {
        case .base:
            return TSChatMessageUIBaseModel.self
        case .image:
            return TSChatMessageUIImageModel.self // 假设有一个 TSChatMessageUIImageModel 类
        case .video:
            return TSChatMessageVideoModel.self // 假设有一个 TSChatMessageVideoModel 类
        }
    }
}


class TSChatMessageUIBaseModel: TSBaseModel {
    
    weak var msgModel:TSChatMessage?
    
    var selfClass:String = "TSChatMessageUIBaseModel"
    var viewClass:String = "TSChatMsgBaseView"
    
    
    
    lazy var view: TSChatMsgBaseView? = {
        if let view = kGetTSChatMsgBaseView(uiBaseModel: self){
//            view.updataView(uiBaseModel: self)
            return view
        }
        return nil
    }()
    
//    var view:TSChatMsgBaseView?{
//        if let view = kGetTSChatMsgBaseView(uiBaseModel: self){
//            view.updataView(uiBaseModel: self)
//            return view
//        }
//        return nil
//    }
    
    func getRecentDataView() -> TSChatMsgBaseView?{
        updataView()
        return view
    }
    
    func updataView() {
        view?.updataView(uiBaseModel: self)
    }
    
    override func mapping(map: Map) {
        selfClass       <- map["selfClass"]
        viewClass       <- map["viewClass"]
    }
    
}

extension TSChatMessageUIBaseModel {
    
    static func creat(viewClass:String,msgModel:TSChatMessage)->TSChatMessageUIBaseModel{
        let model = TSChatMessageUIBaseModel()
        model.viewClass = viewClass
        model.msgModel = msgModel
        return model
    }
    
    func creat(viewClass:String,msgModel:TSChatMessage)->TSChatMessageUIBaseModel{
        let model = TSChatMessageUIBaseModel()
        model.viewClass = viewClass
        model.msgModel = msgModel
        return model
    }
    
}
func kGetTSChatMessageUIBaseModel(jsonString:String,msgModel:TSChatMessage) -> TSChatMessageUIBaseModel? {
    
    if let jsonDict = jsonString.jsonDict(),
        let selfClass = jsonDict["selfClass"] as? String,
       let baseClass = selfClass.toClass() as? TSBaseModel.Type,
       let baseObj = baseClass.init(json: jsonDict),
       let uiModel = baseObj as? TSChatMessageUIBaseModel
    {
        uiModel.msgModel = msgModel
        return uiModel
    }
    return nil
}

func kGetTSChatMsgBaseView(uiBaseModel:TSChatMessageUIBaseModel) -> TSChatMsgBaseView? {
    
    if let baseClass = uiBaseModel.viewClass.toClass() as? TSChatMsgBaseView.Type
    {
        let baseObj = baseClass.init()
        baseObj.updataView(uiBaseModel: uiBaseModel)
        return baseObj
    }
    return nil
}


class TSChatMessageUIImageModel: TSChatMessageUIBaseModel {
    
    
}
class TSChatMessageVideoModel: TSChatMessageUIBaseModel {
    
    
}
