package cn.org.bjca.trust.push.common

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import cn.org.bjca.trust.push.BuildConfig
import cn.org.bjca.trust.push.db.DbHelper
import cn.org.bjca.trust.push.message.ImManager
import cn.org.bjca.trust.push.message.bean.*
import cn.org.bjca.trust.push.message.msg.Message
import cn.org.bjca.trust.push.message.msg.SendMessage
import java.io.File
import java.net.URI

object SendMessageHelper {
    private fun sendMessage(message: Message) {
        val sendMessage = SendMessage()
        sendMessage.messageId = message.messageId ?: CommonHelper.generateMessageId()
        sendMessage.timestamp = System.currentTimeMillis()
        sendMessage.toClientId = message.toClientId
        sendMessage.messageType = message.msgType.value
        sendMessage.message = message.createContentJsonStr()
        ImManager.instance.sendMessage(sendMessage)
    }

    @JvmStatic
    fun sendTextMessage(toUserId: String, msg: String) {
        sendMessage(TextMessage().apply {
            toClientId = toUserId
            text = msg
        })
    }

    @JvmStatic
    fun sendImageMessage(
        toUserId: String,
        uri: URI
    ) {
        sendImageMessage(toUserId, File(uri))
    }

    @JvmStatic
    fun sendImageMessage(
        toUserId: String,
        file: File
    ) {
        if (!FileHelper.isPicture(file)) throw Throwable("不是图片文件")
        val img = GraphicHelper.imageZoom(BitmapFactory.decodeFile(file.absolutePath), 2048.0)
        GraphicHelper.bitmapToBase64(img)?.let {
            sendImageMessage(
                toUserId,
                "dsadsasdsa",
                it,
                img.width.toDouble(),
                img.height.toDouble()
            )
        }
    }
    @JvmStatic
    fun sendImageMessage(
        toUserId: String,
        bitmap: Bitmap
    ) {
        val img = GraphicHelper.imageZoom(bitmap, 200.0)
        GraphicHelper.bitmapToBase64(img)?.let {
            sendImageMessage(
                toUserId,
                "dsadsasdsa",
                it,
                img.width.toDouble(),
                img.height.toDouble()
            )
        }
    }

    @JvmStatic
    fun sendImageMessage(
        toUserId: String,
        imageStorageId: String,
        thumbnail: String,
        thumbnailWidth: Double,
        thumbnailHeight: Double
    ) {
        sendMessage(
            PictureMessage().apply {
                toClientId = toUserId
                this.imageStorageId = imageStorageId
                this.thumbnail = thumbnail
                this.thumbnailWidth = thumbnailWidth
                this.thumbnailHeight = thumbnailHeight
            })
    }

    @JvmStatic
    fun sendAtMessage(
        toUserId: String, text: String, atNos: List<String>
    ) {
        sendMessage(
            AtMessage().apply {
                this.text = text
                this.atNos = atNos
                toClientId = toUserId
            })
    }

    @JvmStatic
    fun sendOtherMessage(
        toUserId: String, msg: String
    ) {
        sendMessage(
            OtherMessage().apply {
                toClientId = toUserId
                this.msg = msg
            })
    }

    @JvmStatic
    fun sendLogMessage(
        msg: String,
    ) {
        val devices = DbHelper.getDataBase().deviceDao().getAll()
        if (devices.isNotEmpty()) {
            sendMessage(
                LogMessage().apply {
                    text = msg
                    versionName = BuildConfig.versionName
                    versionCode = BuildConfig.versionCode
                    device = devices[0]
                }
            )
        }
    }
}