package ch.threema.app.processors.reflectedoutgoingmessage

import ch.threema.app.managers.ServiceManager
import ch.threema.app.utils.MessageUtil
import ch.threema.base.utils.getThreemaLogger
import ch.threema.domain.models.MessageId
import ch.threema.domain.protocol.csp.messages.GroupDeliveryReceiptMessage
import ch.threema.protobuf.common.CspE2eMessageType
import ch.threema.protobuf.d2d.OutgoingMessage
import ch.threema.storage.models.MessageState
import ch.threema.storage.models.group.GroupMessageModel
import java.time.Instant

private val logger = getThreemaLogger("ReflectedOutgoingGroupDeliveryReceiptTask")

internal class ReflectedOutgoingGroupDeliveryReceiptTask(
    outgoingMessage: OutgoingMessage,
    serviceManager: ServiceManager,
) : ReflectedOutgoingContactMessageTask<GroupDeliveryReceiptMessage>(
    outgoingMessage = outgoingMessage,
    message = GroupDeliveryReceiptMessage.fromReflected(outgoingMessage),
    type = CspE2eMessageType.GROUP_DELIVERY_RECEIPT,
    serviceManager = serviceManager,
) {
    private val messageService by lazy { serviceManager.messageService }
    private val myIdentity by lazy { serviceManager.identityStore.getIdentityString()!! }

    override fun processOutgoingMessage() {
        logger.info(
            "Processing message {}: reflected outgoing group delivery receipt",
            outgoingMessage.messageId,
        )

        val messageState: MessageState? =
            MessageUtil.receiptTypeToMessageState(message.receiptType)
        if (messageState == null || !MessageUtil.isReaction(messageState)) {
            logger.warn(
                "Message {} error: unknown or unsupported delivery receipt type: {}",
                message.messageId,
                message.receiptType,
            )
            return
        }

        for (receiptMessageId: MessageId in message.receiptMessageIds) {
            logger.info(
                "Processing message {}: group delivery receipt for {} (state = {})",
                outgoingMessage.messageId,
                receiptMessageId,
                messageState,
            )
            val groupMessageModel: GroupMessageModel? = messageService.getGroupMessageModel(
                receiptMessageId,
                message.groupCreator,
                message.apiGroupId,
            )
            if (groupMessageModel == null) {
                logger.warn(
                    "Group message model ({}) for reflected outgoing group delivery receipt is null",
                    receiptMessageId,
                )
                continue
            }
            messageService.addMessageReaction(
                groupMessageModel,
                messageState,
                // the identity that reacted (this is us => reflected outgoing message)
                myIdentity,
                Instant.ofEpochMilli(outgoingMessage.createdAt),
            )
        }
    }
}
