package com.rosetta.messenger.domain.chats.usecase

import com.rosetta.messenger.di.ProtocolGateway
import com.rosetta.messenger.network.MessageAttachment
import com.rosetta.messenger.network.PacketMessage
import javax.inject.Inject

data class SendTextMessageCommand(
    val fromPublicKey: String,
    val toPublicKey: String,
    val encryptedContent: String,
    val encryptedKey: String,
    val aesChachaKey: String,
    val privateKeyHash: String,
    val messageId: String,
    val timestamp: Long,
    val attachments: List<MessageAttachment> = emptyList(),
    val isSavedMessages: Boolean
)

class SendTextMessageUseCase @Inject constructor(
    private val protocolGateway: ProtocolGateway
) {
    operator fun invoke(command: SendTextMessageCommand): PacketMessage {
        val packet =
            PacketMessage().apply {
                fromPublicKey = command.fromPublicKey
                toPublicKey = command.toPublicKey
                content = command.encryptedContent
                chachaKey = command.encryptedKey
                aesChachaKey = command.aesChachaKey
                privateKey = command.privateKeyHash
                messageId = command.messageId
                timestamp = command.timestamp
                attachments = command.attachments
            }

        if (!command.isSavedMessages) {
            protocolGateway.sendMessageWithRetry(packet)
        }
        return packet
    }

    fun dispatch(packet: PacketMessage, isSavedMessages: Boolean) {
        if (!isSavedMessages) {
            protocolGateway.sendMessageWithRetry(packet)
        }
    }
}
