package ru.forbion.f7cloud.feature.talknative

import android.content.Context
import android.content.Intent
import android.util.Log
import ru.forbion.f7cloud.core.auth.AuthSession
import ru.forbion.f7cloud.core.auth.AuthStore
import ru.forbion.f7cloud.core.push.PushIntents

/**
 * Routes f7push call Accept to native WebRTC (when enabled) or WebView fallback.
 */
object TalkPushBridge {
    private const val TAG = "TalkPushBridge"

    fun launchAcceptedCall(context: Context, acceptUrl: String) {
        val session = AuthStore(context).load()
        if (session == null) {
            Log.w(TAG, "No session for accepted call")
            return
        }
        TalkNativeCallLauncher.launchIncomingCall(context, session, acceptUrl)
    }

    fun intentForAcceptedCall(context: Context, acceptUrl: String): Intent? {
        val launch = context.packageManager.getLaunchIntentForPackage(context.packageName) ?: return null
        return launch.apply {
            action = PushIntents.ACTION_OPEN_CALL
            putExtra(PushIntents.EXTRA_ACCEPT_URL, acceptUrl)
            putExtra(PushIntents.EXTRA_AUTO_ACCEPT, true)
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
    }
}
