package ch.threema.domain.protocol.csp.messages.voip

import ch.threema.base.utils.getThreemaLogger
import ch.threema.domain.protocol.csp.ProtocolDefines
import ch.threema.domain.protocol.csp.messages.BadMessageException
import ch.threema.domain.protocol.csp.messages.voip.VoipCallAnswerMessage.Companion.fromByteArray
import ch.threema.domain.protocol.csp.messages.voip.VoipCallAnswerMessage.Companion.fromReflected
import ch.threema.protobuf.d2d.IncomingMessage
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets

private val logger = getThreemaLogger("VoipCallAnswerMessage")

class VoipCallAnswerMessage : VoipMessage() {
    var data: VoipCallAnswerData? = null

    override fun getBody(): ByteArray? {
        return try {
            val bos = ByteArrayOutputStream()
            data!!.write(bos)
            bos.toByteArray()
        } catch (exception: Exception) {
            logger.error(exception.message)
            null
        }
    }

    override fun getType(): Int = ProtocolDefines.MSGTYPE_VOIP_CALL_ANSWER

    override fun allowUserProfileDistribution(): Boolean {
        // True only if we're accepting the call
        val answerData = this.data ?: return false
        val action = answerData.action
        return action != null && action == VoipCallAnswerData.Action.ACCEPT
    }

    override fun exemptFromBlocking(): Boolean = false

    override fun createImplicitlyDirectContact(): Boolean = false

    override fun protectAgainstReplay(): Boolean = true

    override fun reflectIncoming(): Boolean = true

    override fun reflectOutgoing(): Boolean = true

    override fun reflectSentUpdate(): Boolean = false

    override fun sendAutomaticDeliveryReceipt(): Boolean = false

    override fun bumpLastUpdate(): Boolean = false

    companion object {
        /**
         *  When the message bytes come from sync (reflected), they do not contain the one extra byte at the beginning.
         *  So we set the offset in [fromByteArray] to zero.
         *
         *  In addition the common message model properties ([fromIdentity], [messageId] and [date]) get set.
         *
         *  @param message the MdD2D message representing the voip-call-answer message
         *  @return Instance of [VoipCallAnswerMessage]
         *  @see fromByteArray
         */
        @JvmStatic
        fun fromReflected(message: IncomingMessage): VoipCallAnswerMessage {
            val bodyBytes: ByteArray = message.body.toByteArray()
            val voipCallAnswerMessage = fromByteArray(bodyBytes, 0, bodyBytes.size)
            voipCallAnswerMessage.initializeCommonProperties(message)
            return voipCallAnswerMessage
        }

        /**
         * Build an instance of [VoipCallAnswerMessage] from the given [data] bytes. Note that
         * the common message model properties ([fromIdentity], [messageId] and [date]) will **not** be set.
         *
         * The [data] byte array consists of:
         *  - body json bytes of [VoipCallAnswerData]
         *
         * @param data   the data that represents the voip-call-answer message
         * @param offset the offset where the actual data starts (inclusive)
         * @param length the length of the data (needed to ignore the padding)
         * @return Instance of [VoipCallAnswerMessage]
         * @throws BadMessageException if the length or the offset is invalid
         * @see fromReflected
         */
        @JvmStatic
        @Throws(BadMessageException::class)
        fun fromByteArray(data: ByteArray, offset: Int, length: Int): VoipCallAnswerMessage {
            if (length < 1) {
                throw BadMessageException("Bad length ($length) for voip-call-answer message")
            } else if (offset < 0) {
                throw BadMessageException("Bad offset ($offset) for voip-call-answer message")
            } else if (data.size < length + offset) {
                throw BadMessageException("Invalid byte array length (${data.size}) for offset $offset and length $length")
            }
            return VoipCallAnswerMessage().apply {
                val json = String(data, offset, length, StandardCharsets.UTF_8)
                this.data = VoipCallAnswerData.parse(json)
            }
        }
    }
}
