package com.tencent.qcloud.tuikit.tuicallkit.common.utils

import io.trtc.tuikit.atomicxcore.api.call.CallParticipantStatus
import io.trtc.tuikit.atomicxcore.api.call.CallStore

object CallUtils {

    fun isSelfCaller(): Boolean {
        val selfInfo = CallStore.shared.observerState.selfInfo.value
        return isCaller(selfInfo.id)
    }

    fun isCaller(userId: String): Boolean {
        val callerId = CallStore.shared.observerState.activeCall.value.inviterId
        return callerId == userId
    }

    fun getCaller(): String? {
        return CallStore.shared.observerState.activeCall.value.inviterId
    }

    fun isCallee(userId: String): Boolean {
        val calleeList = CallStore.shared.observerState.activeCall.value.inviteeIds
        return calleeList.contains(userId)
    }

    fun getCallee(): String? {
        val calleeList = CallStore.shared.observerState.activeCall.value.inviteeIds
        return calleeList.firstOrNull()
    }

    fun getPeerUid(): String? {
        return if (isSelfCaller()) {
            getCallee()
        } else {
            getCaller()
        }
    }


    fun getSelfCallStatus(): CallParticipantStatus {
        return CallStore.shared.observerState.selfInfo.value.status
    }

    /**
     * 处于呼叫状态
     */
    fun isSelfInCalling(): Boolean {
        val selfInfo = CallStore.shared.observerState.selfInfo.value
        return (isCaller(selfInfo.id) && selfInfo.status == CallParticipantStatus.Waiting)
                || selfInfo.status == CallParticipantStatus.Accept
    }

    /**
     * @return 通话时长(单位:秒)
     */
    fun getCallDuration(): Long {
        val duration = CallStore.shared.observerState.activeCall.value.duration
        return duration
    }
}