package com.adealink.weparty.call

import android.app.Application
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.media.IMediaOperator
import com.adealink.frame.media.MediaConflictConfig
import com.adealink.frame.media.MediaInfo
import com.adealink.frame.spi.RegisterService
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.call.datasource.local.CallLocalService
import com.adealink.weparty.call.manager.callManager
import com.adealink.weparty.call.widget.float.calling.CallingFloatData
import com.adealink.weparty.call.widget.float.calling.CallingFloatView
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.widget.floatview.data.IFloatData
import com.adealink.weparty.module.call.ICallService
import com.tencent.qcloud.tuikit.tuicallkit.manager.bridge.CallServiceInitializer
import io.trtc.tuikit.atomicxcore.api.CompletionHandler

@RegisterService(ICallService::class)
class CallServiceImpl : ICallService {

    override fun appOnCreateMainTask(application: Application) {
        CallServiceInitializer.createInstance(application)
    }


    override fun call(
        uid: String,
        onSuccess: (() -> Unit)?,
        onFail: ((Int, String?) -> Unit)?
    ) {
        val activity = AppUtil.currentActivity as? BaseActivity
        if (activity == null || activity.isDestroyed || activity.isFinishing) {
            return
        }
        activity.showLoading()
        callManager.call(uid, object : CompletionHandler {
            override fun onSuccess() {
                activity.dismissLoading()
                onSuccess?.invoke()
            }

            override fun onFailure(code: Int, desc: String) {
                activity.dismissLoading()
                onFail?.invoke(code, desc)
            }
        })
    }

//    override fun getIncomingFloatView(data: IFloatData): InComingFloatView {
//        return InComingFloatView(data as InComingFloatData)
//    }

    override fun getCallingFloatView(data: IFloatData): CallingFloatView {
        return CallingFloatView(data as CallingFloatData)
    }

//    override fun getCallViewModel(owner: ViewModelStoreOwner): ICallViewModel {
//        return ViewModelProvider(owner, CallViewModelFactory())[CallViewModel::class.java]
//    }


    override fun getService(): ICallService {
        return this
    }

    override fun getMediaOperator(): IMediaOperator {
        return object : IMediaOperator {

            override suspend fun isMediaIn(): Boolean {
                //当前正在呼叫
                return false
                //return TUICallState.instance.selfUser.get().isCalling()
            }

            override suspend fun leaveMedia(): Rlt<Any> {
//                EngineManager.instance.hangup(null)
                return Rlt.Success(Any())
            }

            override suspend fun confirmExitConflictMedia(
                exitMediaInfo: MediaInfo,
                enterMediaInfo: MediaInfo
            ): Rlt<Any> {
                if (!CallLocalService.mediaConflictRemind) {
                    return Rlt.Success(Any())
                }
                return Rlt.Success(Any())
//                val currentActivity =
//                    AppUtil.currentActivity as? BaseActivity ?: return Rlt.Success(Any())
//                return withContext(Dispatcher.UI) {
//                    suspendCancellableCoroutine { continuation ->
//                        var confirm = false
//                        CallMediaConflictDialog.show(
//                            exitMediaInfo,
//                            enterMediaInfo,
//                            currentActivity.supportFragmentManager,
//                            onPositive = {
//                                confirm = true
//                            },
//                            onNegative = null,
//                            onDismiss = {
//                                if (continuation.isActive) {
//                                    if (confirm) {
//                                        continuation.resume(Rlt.Success(Any()), null)
//                                    } else {
//                                        continuation.resume(Rlt.Failed(IError()), null)
//                                    }
//                                }
//                            }
//                        )
//                    }
//                }
            }

            override fun getConflictConfig(): MediaConflictConfig {
                return MediaConflictConfig(
                    MediaInfo(
                        com.adealink.weparty.media.MediaType.CALL_1V1.type,
                        getCompatString(R.string.call_chat)
                    ), false
                )
            }

            override fun getJoinedRoomId(): Long? {
                return null
            }

            override suspend fun rejoinRoom(): Rlt<Any> {
                //1v1不支持重进房
                return Rlt.Success(Any())
            }

        }
    }


}