package com.adealink.weparty.call.interceptor

import android.content.Context
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.log.Log
import com.adealink.frame.media.MediaInfo
import com.adealink.frame.router.interceptor.UriInterceptor
import com.adealink.frame.router.request.UriRequest
import com.adealink.weparty.App
import com.adealink.weparty.call.R
import com.adealink.weparty.call.constant.TAG_CALL_ENTER
import com.adealink.weparty.call.manager.callManager
import com.adealink.weparty.media.MediaType
import com.adealink.weparty.module.profile.ProfileModule
import com.tencent.qcloud.tuikit.tuicallkit.common.utils.CallUtils
import com.tencent.qcloud.tuikit.tuicallkit.manager.CallManager
import io.trtc.tuikit.atomicxcore.api.call.CallParticipantStatus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 *
 */
class EnterCallUriInterceptor : UriInterceptor {

    override fun intercept(chain: UriInterceptor.Chain) {
        val request = chain.request()
        val context = request.ctx ?: return

        CoroutineScope(Dispatcher.UI).launch {
            enterCall(context, object : UriInterceptor.Chain {
                override fun abort() {
                    CallManager.instance.reset()
                }

                override fun proceed(request: UriRequest) {
                    chain.proceed(request)
                }

                override fun request(): UriRequest {
                    return chain.request()
                }
            })
        }
    }

    /**
     * -. 检查媒体冲突
     * -. 检查/申请权限
     */
    private suspend fun enterCall(context: Context, chain: UriInterceptor.Chain) {
        val request = chain.request()

        //检查当前通话信息
//        val mediaType = TUICallState.instance.mediaType.get()
//        if (mediaType == null || mediaType == TUICallDefine.MediaType.Unknown) {
//            Log.e(TAG_CALL_ENTER, "enterCall, mediaType($mediaType) is invalid")
//            chain.abort()
//            return
//        }

        //房间媒体冲突处理失败,进房失败
        val conflictRlt = App.instance.mediaManager.conflictHandle(
            MediaInfo(
                MediaType.CALL_1V1.type,
                getCompatString(R.string.call_chat)
            )
        )
        if (conflictRlt is Rlt.Failed) {
            Log.e(TAG_CALL_ENTER, "enterCall fail, for media conflict fail")
            chain.abort()
            return
        }

        val permissionRlt =
            callManager.requestPermission(CallUtils.isCaller(ProfileModule.getMyUid()))
        if (permissionRlt is Rlt.Failed) {
            Log.e(TAG_CALL_ENTER, "enterCall fail, for permission denied")
            chain.abort()
            return
        }

        if (CallUtils.getSelfCallStatus() == CallParticipantStatus.None) {
            Log.i(TAG_CALL_ENTER, "enterCall fail, for self callStatus is None")
            chain.abort()
            return
        }

        chain.proceed(request)
    }


}