package com.adealink.weparty.im.session

import androidx.fragment.app.FragmentActivity
import com.adealink.frame.log.Log
import com.adealink.frame.router.interceptor.UriInterceptor
import com.adealink.weparty.im.stat.StartChatEvent
import com.adealink.weparty.module.im.IM
import com.adealink.weparty.module.im.data.TAG_IM_SESSION

class SessionInterceptor : UriInterceptor {

    override fun intercept(chain: UriInterceptor.Chain) {
        val request = chain.request()
        val activity = request.ctx as? FragmentActivity
        if (activity == null) {
            Log.e(TAG_IM_SESSION, "SessionInterceptor, activity is null")
            chain.abort()
            return
        }
        val targetUid = request.intent.getStringExtra(IM.Session.EXTRA_CHAT_ID)
        if (targetUid.isNullOrEmpty()) {
            chain.abort()
            return
        }

        StartChatEvent().apply {
            userId to targetUid
        }.send()

        chain.proceed(request)
    }
}