package com.swago.room.lianmai

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.swago.baseswago.agora.AgoraManager
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.RoleType
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.ChoiceDialogFragment
import com.swago.baseswago.model.live.mic.LianMaiIMModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo
import com.swago.loadBurlImage
import com.swago.loadUrl
import com.swago.room.R
import com.swago.room.databinding.ViewLianMaiBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LianMaiView : ConstraintLayout, IRoomActiveListener, ILianMaiListener {

    private var binding: ViewLianMaiBinding? = null
    private var imModel: LianMaiIMModel? = null
    var openCloseLianMaiDialogFun: (() -> Unit)? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    private fun initView(context: Context) {
        binding = ViewLianMaiBinding.inflate(LayoutInflater.from(context), this, true)
        SwagoRoomManager.addListener(this)
        LianMaiManager.addLianMaiListener(this)

        binding?.ivCloseMic?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                //关闭连麦确认弹窗
                openCloseLianMaiDialogFun?.invoke()
            }
        })
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
        visibility = View.INVISIBLE
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

    override fun closeRoomed() {
        SwagoRoomManager.removeListener(this)
        LianMaiManager.removeLianMaiListener(this)
    }

    override fun startLianMai(lianMaiIMModel: LianMaiIMModel) {
        this@LianMaiView.visibility = View.VISIBLE
        lianMaiIMModel.linkMicToken?.let {  token ->
                imModel = lianMaiIMModel
                binding?.let {
                    it.tvName.text = lianMaiIMModel.senderName
                    it.ivAvatar.loadUrl(context, lianMaiIMModel.userAvatar ?: "")
                    it.ivBg.loadBurlImage(context, lianMaiIMModel.userAvatar ?: "")
                    when (LianMaiManager.lianMaiState) {
                        2, 4 -> {
                            //正在观看连麦的用户
                            it.ivCloseMic.visibility = View.INVISIBLE
                        }
                        1 -> {
                            //正在连麦的用户或者主播
                            it.ivCloseMic.visibility = View.VISIBLE
                            UserInfo.getUserInfo()?.let { userInfo ->
                                if (SwagoRoomManager.roleType == RoleType.user && userInfo.id == lianMaiIMModel.senderId) {
                                    //正在连麦的用户
                                    AgoraManager.setVideoAudio(
                                        pullVideo = true,
                                        pullAudio = true,
                                        pushVideo = false,
                                        pushAudio = true
                                    )
                                    AgoraManager.renewToken(token)
                                }
                            }
                        }
                        else -> {}
                    }
            }
        }
    }

    override fun stopLianMai(lianMaiIMModel: LianMaiIMModel) {
        visibility = View.INVISIBLE
        when (LianMaiManager.lianMaiState) {
            0 -> {
                UserInfo.getUserInfo()?.let {
                    if (SwagoRoomManager.roleType == RoleType.user && it.id == lianMaiIMModel.senderId) {
                        //正在连麦用户
                        AgoraManager.setVideoAudio(
                            pullVideo = true,
                            pullAudio = true,
                            pushVideo = false,
                            pushAudio = false
                        )
                        lianMaiIMModel.linkMicToken?.let {
                            AgoraManager.renewToken(it)
                        }

                    }
                }
            }
        }
    }

    fun closeLianMai(isAnchor: Boolean) {
        LianMaiManager.lianMaiState = 0
        visibility = View.INVISIBLE
        if (!isAnchor) {
            //如果不是主播还是把角色切换成用户
            AgoraManager.setVideoAudio(
                pullVideo = true,
                pullAudio = true,
                pushVideo = false,
                pushAudio = false
            )
        }
    }
}