package com.rosetta.messenger.network.connection

import com.rosetta.messenger.network.ConnectionEvent
import com.rosetta.messenger.network.Packet
import com.rosetta.messenger.network.ProtocolState

class ConnectionEventRouter(
    private val handleInitializeAccount: (publicKey: String, privateKey: String) -> Unit,
    private val handleConnect: (reason: String) -> Unit,
    private val handleFastReconnect: (reason: String) -> Unit,
    private val handleDisconnect: (reason: String, clearCredentials: Boolean) -> Unit,
    private val handleAuthenticate: (publicKey: String, privateHash: String) -> Unit,
    private val handleProtocolStateChanged: (state: ProtocolState) -> Unit,
    private val handleSendPacket: (packet: Packet) -> Unit,
    private val handleSyncCompleted: (reason: String) -> Unit,
    private val handleOwnProfileResolved: (publicKey: String) -> Unit,
    private val handleOwnProfileFallbackTimeout: (sessionGeneration: Long) -> Unit
) {
    suspend fun route(event: ConnectionEvent) {
        when (event) {
            is ConnectionEvent.InitializeAccount -> {
                handleInitializeAccount(event.publicKey, event.privateKey)
            }
            is ConnectionEvent.Connect -> {
                handleConnect(event.reason)
            }
            is ConnectionEvent.FastReconnect -> {
                handleFastReconnect(event.reason)
            }
            is ConnectionEvent.Disconnect -> {
                handleDisconnect(event.reason, event.clearCredentials)
            }
            is ConnectionEvent.Authenticate -> {
                handleAuthenticate(event.publicKey, event.privateHash)
            }
            is ConnectionEvent.ProtocolStateChanged -> {
                handleProtocolStateChanged(event.state)
            }
            is ConnectionEvent.SendPacket -> {
                handleSendPacket(event.packet)
            }
            is ConnectionEvent.SyncCompleted -> {
                handleSyncCompleted(event.reason)
            }
            is ConnectionEvent.OwnProfileResolved -> {
                handleOwnProfileResolved(event.publicKey)
            }
            is ConnectionEvent.OwnProfileFallbackTimeout -> {
                handleOwnProfileFallbackTimeout(event.sessionGeneration)
            }
        }
    }
}
