package com.swago.baseswago.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.fragment.app.viewModels
import com.swago.baseswago.HandleVm
import com.swago.baseswago.R
import com.swago.baseswago.UserVm
import com.swago.baseswago.baseroom.RoleType
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.databinding.DialogHandleBinding
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.IMUtil
import com.swago.baseswago.util.NoDoubleClickListener

/**
 *@date 2022/2/7 10:12
 *description:
 */
class HandleDialogFragment : BaseXDFragment<DialogHandleBinding>() {

    private val handleVm by viewModels<HandleVm>()
    private val userVm by viewModels<UserVm>()
    private var userId = ""
    private var account = ""
    private var isForbid = 0
    private var inRoom = false
    private var isAnchor = false


    init {
        setGravity(Gravity.BOTTOM)
        setDimAmount(0.5f)
    }


    companion object{
        fun newInstance(userId:String,account:String,isForbid:Int,isAnchor:Boolean,inRoom:Boolean=false): HandleDialogFragment {
            val args = Bundle()
            args.putString("userId",userId)
            args.putString("account",account)
            args.putInt("isForbid",isForbid)
            args.putBoolean("inRoom",inRoom)
            args.putBoolean("isAnchor",isAnchor)
            val fragment = HandleDialogFragment()
            fragment.arguments = args
            return fragment
        }
    }
    
    override fun initOther() {
        arguments?.let {
            userId = it.getString("userId","")
            account = it.getString("account","")
            isForbid = it.getInt("isForbid",0)
            inRoom = it.getBoolean("inRoom",false)
            isAnchor = it.getBoolean("isAnchor",false)
            if (isForbid == 1){
                binding.tvMute.text = AppContext.getContext().resources.getString(R.string.cancel_forbid_speak)
            }else {
                binding.tvMute.text = AppContext.getContext().resources.getString(R.string.forbid_speak)
            }
            if (inRoom){
                if (isAnchor){
                    binding.tvKick.visibility = View.VISIBLE
                    binding.tvMute.visibility = View.VISIBLE
                }else{
                    binding.tvKick.visibility = View.GONE
                    binding.tvMute.visibility = View.GONE
                }
            }else{
                binding.tvKick.visibility = View.GONE
                binding.tvMute.visibility = View.GONE
            }
            SwagoRoomManager.iRoomInfo?.let { info ->
                handleVm.getUserRoomAuth(userId,info.getRoomId())
                if (inRoom){
                    if (SwagoRoomManager.roleType == RoleType.user){
                        binding.tvNeverWatcherAnchor.visibility = View.VISIBLE
                        binding.tvLiveRoomChatBlack.visibility = View.GONE
                    }else{
                        binding.tvNeverWatcherAnchor.visibility = View.GONE
                        binding.tvLiveRoomChatBlack.visibility = View.VISIBLE
                    }
                }else{
                    binding.tvNeverWatcherAnchor.visibility = View.GONE
                    binding.tvLiveRoomChatBlack.visibility = View.GONE
                }

                if (info.getAnchorCode() == account){
                    //如果是主播还要加一个 不在观看该主播

                }else{

                }
            }
        }

        binding.tvReport.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (account.isNotEmpty()){
                    ReportDialog.newInstance(account)
                        .show(parentFragmentManager, "ReportDialog")
                }
            }
        })


        binding.tvAddPrivateChatBlack.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (account.isNotEmpty()){
                    val list = ArrayList<String>()
                    list.add(account)
                    IMUtil.addToBlackList(list){
                        userVm.addBlack(account,0)
                    }
                }
            }
        })

        binding.tvLiveRoomChatBlack.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                SwagoRoomManager.iRoomInfo?.let {
                    handleVm.blackUser(userId,2,1)
                }
            }
        })


        binding.tvNeverWatcherAnchor.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                handleVm.blackUser(userId,1,1)
            }
        })

        binding.tvKick.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (userId.isNotEmpty()){
                   SwagoRoomManager.iRoomInfo?.let {
                       handleVm.kickOutRoom(userId,it.getRoomId(),1)
                   }
                }
            }
        })

        binding.tvMute.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (userId.isNotEmpty()){
                    SwagoRoomManager.iRoomInfo?.let {
                        if (isForbid==1){
                            handleVm.muteOrCancel(userId,it.getRoomId(),0)
                        }else{
                            handleVm.muteOrCancel(userId,it.getRoomId(),1)
                        }
                    }
                }
            }
        })
    }

    override fun initLiveData() {
        handleVm.muteOrCancelLiveData.observe(this){
            if (it == 1){
                binding.tvMute.text = AppContext.getContext().resources.getString(R.string.cancel_forbid_speak)
            }else{
                binding.tvMute.text = AppContext.getContext().resources.getString(R.string.forbid_speak)
            }
        }

        handleVm.kickSuccessLiveData.observe(this){
            dismissAllowingStateLoss()
        }

        userVm.hasBeenBlackLiveData.observe(this) {
            dismissAllowingStateLoss()
        }

        handleVm.blackTypeLiveData.observe(this) {
            if (it == 1 && inRoom){
                SwagoRoomManager.closeRoom()
            }
        }
    }
}