package com.swago.room.audio

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.fragment.app.activityViewModels
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.BaseXDFragment
import com.swago.baseswago.model.live.audio.AudioSeatModel
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.DialogAudioActionBinding
import com.swago.room.vm.RoomVm

class AudioActionDialog : BaseXDFragment<DialogAudioActionBinding>(),IRoomActiveListener  {

    private val roomVm by activityViewModels<RoomVm>()

    init {
        setGravity(Gravity.BOTTOM)
        setCanCancel(true)
        setDimAmount(0f)
    }

    companion object{
        fun newInstance(audioSeatModel: AudioSeatModel): AudioActionDialog {
            val args = Bundle()
            args.putParcelable("model",audioSeatModel)
            val fragment = AudioActionDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        SwagoRoomManager.addListener(this)
        arguments?.let {
            val audioSeatModel = it.getParcelable<AudioSeatModel>("model")
            audioSeatModel?.let {
                if (SwagoRoomManager.roleType == RoleType.anchor){
                    //主播
                    if (audioSeatModel.lock_status==2){
                        binding.llUnlock.visibility = View.VISIBLE
                    }else{
                        if (audioSeatModel.user_id == "0"){
                            binding.llLock.visibility = View.VISIBLE
                        }
                    }
                    if (audioSeatModel.user_id == "0"){
                        if (audioSeatModel.lock_status==1){
                            binding.llYaoQingShangMic.visibility = View.VISIBLE
                        }
                    }else{
                        UserInfo.getUserInfo()?.let { userInfoModel ->
                            if (userInfoModel.id != it.user_id){
                                binding.llDownSeatMic.visibility = View.VISIBLE
                            }
                            if (audioSeatModel.voice_status==2){
                                //闭麦的情况下  如果是被主播闭麦 那么主播端开麦按钮   否则不展示
                                if (userInfoModel.id == audioSeatModel.voice_user_id){
                                    binding.llUnMuteMic.visibility = View.VISIBLE
                                }
                            }else{
                                binding.llMuteMic.visibility = View.VISIBLE
                            }
                        }
                    }
                }else{
                    binding.llLock.visibility = View.GONE
                    binding.llUnlock.visibility = View.GONE
                    UserInfo.getUserInfo()?.let { userInfoModel ->
                        if (userInfoModel.id == it.user_id){
                            binding.llDownSeatMic.visibility = View.VISIBLE
                            if (audioSeatModel.voice_status==2){
                                binding.llUnMuteMic.visibility = View.VISIBLE
                            }else{
                                binding.llMuteMic.visibility = View.VISIBLE
                            }
                        }
                    }
                }
                binding.llLock.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        roomVm.lockAndUnLockMic(it.maiIndex,1)
                        dismissAllowingStateLoss()
                    }
                })
                binding.llUnlock.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        roomVm.lockAndUnLockMic(it.maiIndex,2)
                        dismissAllowingStateLoss()
                    }
                })
                binding.llYaoQingShangMic.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        AudioCanInviteDialog.newInstance(it.maiIndex).show(parentFragmentManager,"AudioCanInviteDialog")
                        dismissAllowingStateLoss()
                    }
                })
                binding.llDownSeatMic.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        roomVm.downMicByAnchor(it.maiIndex,it.user_id?:"")
                        dismissAllowingStateLoss()
                    }
                })
                //闭麦
                binding.llMuteMic.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        roomVm.operateMicByAnchor(it.maiIndex,it.user_id?:"",1)
                        dismissAllowingStateLoss()
                    }
                })
                //开麦
                binding.llUnMuteMic.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        if (it.user_id == it.voice_user_id){
                            //被自己闭麦后开麦
                            roomVm.operateMicByAnchor(it.maiIndex,it.user_id?:"",2)
                            dismissAllowingStateLoss()
                        }else{
                            //被主播闭麦 如果是主播操作
                            if (SwagoRoomManager.roleType == RoleType.anchor){
                                roomVm.operateMicByAnchor(it.maiIndex,it.user_id?:"",2)
                                dismissAllowingStateLoss()
                            }else{
                                Toast.makeText(
                                    AppContext.getContext(),
                                    AppContext.getContext().resources.getString(R.string.you_be_mute_anchor),
                                    Toast.LENGTH_SHORT
                                )
                                    .show()
                            }
                        }

                    }
                })

            }


        }
    }

    override fun initLiveData() {
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

    override fun closeRoomed() {
    }

    override fun onDestroyView() {
        SwagoRoomManager.removeListener(this)
        super.onDestroyView()
    }

    override fun receiveStopLive() {
        dismissAllowingStateLoss()
    }
}