package com.rosetta.messenger.network.connection

import com.rosetta.messenger.network.ConnectionBootstrapContext
import java.util.concurrent.atomic.AtomicLong

class ProtocolLifecycleStateStoreImpl(
    private val lifecycleStateMachine: RuntimeLifecycleStateMachine,
    private val ownProfileFallbackTimerService: OwnProfileFallbackTimerService,
    private val clearLastSubscribedTokenValue: () -> Unit
) : ProtocolLifecycleStateStore {
    private val sessionGeneration = AtomicLong(0L)

    override var bootstrapContext: ConnectionBootstrapContext
        get() = lifecycleStateMachine.bootstrapContext
        set(value) {
            lifecycleStateMachine.bootstrapContext = value
        }

    override fun clearLastSubscribedToken() {
        clearLastSubscribedTokenValue()
    }

    override fun cancelOwnProfileFallbackTimeout() {
        ownProfileFallbackTimerService.cancel()
    }

    override fun scheduleOwnProfileFallbackTimeout(sessionGeneration: Long) {
        ownProfileFallbackTimerService.schedule(sessionGeneration)
    }

    override fun nextSessionGeneration(): Long = sessionGeneration.incrementAndGet()

    override fun currentSessionGeneration(): Long = sessionGeneration.get()
}
