package com.adealink.weparty.call.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.util.onClick
import com.adealink.weparty.call.R
import com.adealink.weparty.call.databinding.LayoutCallBottomBarBinding
import com.adealink.weparty.call.manager.callManager
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.hide
import com.adealink.weparty.commonui.ext.show
import com.tencent.qcloud.tuikit.tuicallkit.common.utils.CallUtils
import io.trtc.tuikit.atomicxcore.api.call.CallParticipantInfo
import io.trtc.tuikit.atomicxcore.api.call.CallParticipantStatus
import io.trtc.tuikit.atomicxcore.api.call.CallStore
import io.trtc.tuikit.atomicxcore.api.device.AudioRoute
import io.trtc.tuikit.atomicxcore.api.device.DeviceStatus
import io.trtc.tuikit.atomicxcore.api.device.DeviceStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class CallBottomComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutCallBottomBarBinding
) : BaseCallViewComponent(lifecycleOwner) {

    override fun initViews() {
        super.initViews()
        binding.btnCallingHangup.onClick {
            binding.vCalling.isEnabled = false
            CallStore.shared.reject(null)
        }
        binding.btnCallingAccept.onClick {
            callManager.accept(null)
        }


        binding.btnMic.onClick {
            clickMic()
        }
        updateMicrophoneButtonView()
        binding.btnHangup.onClick {
            binding.vChatting.isEnabled = false
            CallStore.shared.hangup(null)
        }
        binding.btnSpeaker.onClick {
            clickSpeaker()
        }
        val currentAudioRoute = DeviceStore.shared().deviceState.currentAudioRoute.value
        updateAudioRouteButton(currentAudioRoute)
    }

    override suspend fun observeCallState(scope: CoroutineScope) {
        scope.launch {
            DeviceStore.shared().deviceState.microphoneStatus.collect {
                updateMicrophoneButtonView()
            }
        }
        scope.launch {
            DeviceStore.shared().deviceState.currentAudioRoute.collect { audioRoute ->
                updateAudioRouteButton(audioRoute)
            }
        }
    }

    override fun onSelfInfoChanged(selfInfo: CallParticipantInfo) {
        val selfUser = CallStore.shared.observerState.selfInfo.value
        if (CallUtils.isCaller(selfUser.id)) {
            binding.vCalling.gone()
            binding.vChatting.show()
            when (selfUser.status) {
                CallParticipantStatus.None -> {
                    //Ntd.
                }

                CallParticipantStatus.Waiting -> {
                    binding.btnMic.gone()
                    binding.btnSpeaker.gone()
                    binding.tvTimer.hide()
                }

                CallParticipantStatus.Accept -> {
                    binding.btnMic.show()
                    binding.btnSpeaker.show()
                    binding.tvTimer.show()
                }
            }
        } else {
            //被叫
            when (selfUser.status) {
                CallParticipantStatus.None -> {
                    //Ntd.
                }

                CallParticipantStatus.Waiting -> {
                    binding.vCalling.show()
                    binding.vChatting.gone()
                }

                CallParticipantStatus.Accept -> {
                    binding.vCalling.gone()
                    binding.vChatting.show()
                    binding.btnMic.show()
                    binding.btnSpeaker.show()
                    binding.tvTimer.show()
                }
            }
        }
    }

    private fun clickMic() {
        val currentMicrophoneStatus = DeviceStore.shared().deviceState.microphoneStatus.value
        if (currentMicrophoneStatus == DeviceStatus.ON) {
            DeviceStore.shared().closeLocalMicrophone()
        } else {
            DeviceStore.shared().openLocalMicrophone(null)
        }
    }

    private fun clickSpeaker() {
        val currentAudioRoute = DeviceStore.shared().deviceState.currentAudioRoute.value
        if (currentAudioRoute == AudioRoute.SPEAKERPHONE) {
            DeviceStore.shared().setAudioRoute(AudioRoute.EARPIECE)
        } else {
            DeviceStore.shared().setAudioRoute(AudioRoute.SPEAKERPHONE)
        }
    }

    private fun updateMicrophoneButtonView() {
        val microphoneStatus = DeviceStore.shared().deviceState.microphoneStatus.value
        binding.btnMic.setImageResource(
            if (microphoneStatus == DeviceStatus.OFF) {
                R.drawable.call_mic_disable_ic
            } else {
                R.drawable.call_mic_enable_ic
            }
        )
    }

    private fun updateAudioRouteButton(audioRoute: AudioRoute) {
        val isSpeaker = audioRoute == AudioRoute.SPEAKERPHONE
        binding.btnSpeaker.setImageResource(
            if (isSpeaker) {
                R.drawable.call_speaker_enable_ic
            } else {
                R.drawable.call_speaker_disable_ic
            }
        )
    }

}