package com.swago.room.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.swago.baseswago.agora.AgoraManager
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.databinding.LayoutAnchorFooterViewBinding
import com.swago.room.inter.IFooter
import com.swago.room.pk.PKStateManager

/**
 *@date 2021/10/9 10:55
 *description:
 */
class AnchorFooterView : ConstraintLayout, IFooter, IRoomActiveListener {

    lateinit var binding: LayoutAnchorFooterViewBinding

    var openSendMsgDialog:(()->Unit)? = null
    var openMessageFun:(()->Unit)? = null
    var openGameFun:(()->Unit)? = null
    var openPKFun:(()->Unit)? = null

    private var isMute = false

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView()
    }

    private fun initView() {
        visibility = View.GONE
        SwagoRoomManager.addListener(this)
        binding = LayoutAnchorFooterViewBinding.inflate(LayoutInflater.from(context), this, true)

        UserInfo.getUserInfo()?.let {
            if (it.user_type == 1&&it.user_agent_id>0){
                binding.ivPK.visibility = View.VISIBLE
            }else{
                binding.ivPK.visibility = View.GONE
            }
        }

        binding.ivSwitchCamera.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                AgoraManager.changeCameraOrientation()
            }
        })
        binding.ivChat.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                openSendMsgDialog?.invoke()
            }
        })

        binding.ivMessage.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                openMessageFun?.invoke()
            }
        })

        binding.ivGame.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                openGameFun?.invoke()
            }
        })

        binding.ivPK.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                if (PKStateManager.roomState == PKStateManager.LIVING){
                    openPKFun?.invoke()
                }
            }
        })

        binding.ivMute.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                if (isMute){
                    AgoraManager.setMute(false)
                    binding.ivMute.setImageResource(R.mipmap.live_unmute)
                }else{
                    AgoraManager.setMute(true)
                    binding.ivMute.setImageResource(R.mipmap.live_mute)
                }
                isMute = !isMute
            }
        })
    }

    override fun getFooterView(): View {
        return this
    }


    override fun setGameIcon(b: Boolean) {
        binding.ivGame.visibility = if (b) View.VISIBLE else View.GONE
    }

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

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        visibility = View.VISIBLE
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

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


}