package com.aiosman.ravenow.entity

import android.content.Context
import android.icu.util.Calendar
import com.aiosman.ravenow.ConstVars
import com.aiosman.ravenow.exp.formatChatTime
import com.google.gson.annotations.SerializedName
import io.openim.android.sdk.models.Message
import io.openim.android.sdk.models.PictureElem

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<PictureInfo> = emptyList<PictureInfo>().toMutableList(),
    val messageType: Int = 0,
    val textDisplay: String = "",
    val msgId: String, // Add this property
    var showTimestamp: Boolean = false,
    var showTimeDivider: Boolean = false
) {
    companion object {
        // OpenIM 消息类型常量
        const val MESSAGE_TYPE_TEXT = 101
        const val MESSAGE_TYPE_IMAGE = 102
        const val MESSAGE_TYPE_AUDIO = 103
        const val MESSAGE_TYPE_VIDEO = 104
        const val MESSAGE_TYPE_FILE = 105
        
        fun convertToChatItem(message: Message, context: Context, avatar: String? = null): ChatItem? {
            val timestamp = message.createTime
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = timestamp
            
            var faceAvatar = avatar
            if (faceAvatar == null) {
                faceAvatar = "${ConstVars.BASE_SERVER}${message.senderFaceUrl}"
            }
            
            when (message.contentType) {
                MESSAGE_TYPE_IMAGE -> {
                    val pictureElem = message.pictureElem
                    if (pictureElem != null && !pictureElem.sourcePicture.url.isNullOrEmpty()) {
                        return ChatItem(
                            message = "Image",
                            avatar = faceAvatar,
                            time = calendar.time.formatChatTime(context),
                            userId = message.sendID,
                            nickname = message.senderNickname ?: "",
                            timestamp = timestamp,
                            imageList = listOfNotNull(
                                PictureInfo(
                                    url = pictureElem.sourcePicture.url ?: "",
                                    width = pictureElem.sourcePicture.width,
                                    height = pictureElem.sourcePicture.height,
                                    size = pictureElem.sourcePicture.size
                                )
                            ).toMutableList(),
                            messageType = MESSAGE_TYPE_IMAGE,
                            textDisplay = "Image",
                            msgId = message.clientMsgID
                        )
                    }
                    return null
                }

                MESSAGE_TYPE_TEXT -> {
                    return ChatItem(
                        message = message.textElem?.content ?: "Unsupported message type",
                        avatar = faceAvatar,
                        time = calendar.time.formatChatTime(context),
                        userId = message.sendID,
                        nickname = message.senderNickname ?: "",
                        timestamp = timestamp,
                        imageList = emptyList<PictureInfo>().toMutableList(),
                        messageType = MESSAGE_TYPE_TEXT,
                        textDisplay = message.textElem?.content ?: "Unsupported message type",
                        msgId = message.clientMsgID
                    )
                }

                else -> {
                    return null
                }
            }
        }
    }
}

// OpenIM 图片信息数据类
data class PictureInfo(
    val url: String,
    val width: Int,
    val height: Int,
    val size: Long
)


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
)