package com.swago.room.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
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.AppContext
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.enum.RoomType
import com.swago.room.inter.IFooter
import com.swago.room.lianmai.LianMaiManager
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
    var openLianMaiFun:(()->Unit)? = null
    var clearJiFen:(()->Unit)? = null
    var openAudioNoticeDialogFun:(()->Unit)? = null
    var openGiftPanelFun:(()->Unit)? = null

    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.ivNotice.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                openAudioNoticeDialogFun?.invoke()
            }
        })

        binding.ivGift.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                openGiftPanelFun?.invoke()
            }
        })


        binding.ivPK.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                if (PKStateManager.roomState == PKStateManager.LIVING){
                    if (LianMaiManager.lianMaiState == 0){
                        openPKFun?.invoke()
                    }else{
                        Toast.makeText(AppContext.getContext(), "您当前正在PK，请先结束PK", Toast.LENGTH_SHORT).show()
                    }

                }
            }
        })

        binding.ivLianMai.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                binding.ivDot.visibility = View.GONE
                openLianMaiFun?.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
            }
        })

        binding.ivClearJifen.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                clearJiFen?.invoke()
            }
        })
    }

    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) {
        if (iRoomInfo.getRoomType() == RoomType.AUDIO.type){
            binding.ivSwitchCamera.visibility = View.GONE
            binding.ivPK.visibility = View.GONE
            binding.ivLianMai.visibility = View.GONE
            binding.ivMute.visibility = View.GONE
            binding.ivClearJifen.visibility = View.VISIBLE
            binding.ivNotice.visibility = View.VISIBLE
            binding.ivGift.visibility = View.VISIBLE
        }
        visibility = View.VISIBLE
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

    override fun receiveStopLive() {

    }

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

    fun showLianMaiDot() {
        binding.ivDot.visibility = View.VISIBLE
    }


}