package com.aiosman.riderpro.entity

import android.content.Context
import android.icu.util.Calendar
import com.aiosman.riderpro.exp.formatChatTime
import com.google.gson.annotations.SerializedName
import com.tencent.imsdk.v2.V2TIMImageElem
import com.tencent.imsdk.v2.V2TIMMessage

data class ChatItem(
    val message: String,
    val avatar: String,
    val time: String,
    val userId: String,
    val nickname: String,
    val timeCategory: String = "",
    val timestamp: Long = 0,
    val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
    val messageType: Int = 0,
    val textDisplay: String = "",
    val msgId: String, // Add this property
    var showTimestamp: Boolean = false,
    var showTimeDivider: Boolean = false
) {
    companion object {
        fun convertToChatItem(message: V2TIMMessage, context: Context): ChatItem? {
//            val avatar = if (message.sender == userProfile?.trtcUserId) {
//                userProfile?.avatar ?: ""
//            } else {
//                myProfile?.avatar ?: ""
//            }
//            val nickname = if (message.sender == userProfile?.trtcUserId) {
//                userProfile?.nickName ?: ""
//            } else {
//                myProfile?.nickName ?: ""
//            }
            val timestamp = message.timestamp
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = timestamp * 1000
            val imageElm = message.imageElem?.imageList
            when (message.elemType) {
                V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
                    val imageElm = message.imageElem?.imageList?.all {
                        it.size == 0
                    }
                    if (imageElm != true) {
                        return ChatItem(
                            message = "Image",
                            avatar = message.faceUrl,
                            time = calendar.time.formatChatTime(context),
                            userId = message.sender,
                            nickname = message.nickName,
                            timestamp = timestamp * 1000,
                            imageList = message.imageElem?.imageList
                                ?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
                            messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE,
                            textDisplay = "Image",
                            msgId = message.msgID // Add this line to include msgId
                        )
                    }
                    return null
                }

                V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
                    return ChatItem(
                        message = message.textElem?.text ?: "Unsupported message type",
                        avatar = message.faceUrl,
                        time = calendar.time.formatChatTime(context),
                        userId = message.sender,
                        nickname = message.nickName,
                        timestamp = timestamp * 1000,
                        imageList = imageElm?.toMutableList()
                            ?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
                        messageType = V2TIMMessage.V2TIM_ELEM_TYPE_TEXT,
                        textDisplay = message.textElem?.text ?: "Unsupported message type",
                        msgId = message.msgID // Add this line to include msgId
                    )
                }

                else -> {
                    return null
                }

            }
        }
    }
}


data class ChatNotification(
    @SerializedName("userId")
    val userId: Int,
    @SerializedName("userTrtcId")
    val userTrtcId: String,
    @SerializedName("targetUserId")
    val targetUserId: Int,
    @SerializedName("targetTrtcId")
    val targetTrtcId: String,
    @SerializedName("strategy")
    val strategy: String
)