package com.swago.room.lianmai

import android.Manifest
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.permissionx.guolindev.PermissionX
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.dialog.BaseListDialogFragment
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.model.live.mic.LianMaiUserModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.R
import com.swago.room.databinding.DialogUserLianMaiBinding
import com.swago.room.user.UserRoomActivity

class UserLianMaiDialog : BaseXDFragment<DialogUserLianMaiBinding>(),IRoomActiveListener {

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


    companion object{
        fun newInstance(): UserLianMaiDialog {
            val args = Bundle()
            val fragment = UserLianMaiDialog()
            fragment.arguments = args
            return fragment
        }
    }

    private val lianMaiVm by viewModels<LianMaiVm>()

     private val adapter by lazy {
        UserLianMaiListAdapter()
    }

    override fun initOther() {
        SwagoRoomManager.addListener(this)
        binding.swagoRv.layoutManager = LinearLayoutManager(context)
        binding.swagoRv.adapter = adapter
        lianMaiVm.getLianMaiList()
        if (LianMaiManager.lianMaiState == 3||LianMaiManager.lianMaiState == 4){
            //申请连麦的用户
            binding.tvApply.text = AppContext.getContext().resources.getString(R.string.cancel_apply_lian_mai)
            binding.tvApply.setBackgroundResource(R.drawable.shape_14ffffff_37)
        }else{
            //其他用户
            binding.tvApply.text = AppContext.getContext().resources.getString(R.string.apply_lian_mai)
            binding.tvApply.setBackgroundResource(R.drawable.shape_ff56b7_37)
        }

        binding.tvApply.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (LianMaiManager.lianMaiState == 3||LianMaiManager.lianMaiState == 4){
                    //申请连麦的用户 取消申请
                    lianMaiVm.cancelApplyLianMai(LianMaiManager.lianMaiId)
                }else{
                    PermissionX.init(this@UserLianMaiDialog)
                        .permissions(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                        .request { allGranted, grantedList, deniedList ->
                            if (allGranted) {
                                //其他用户
                                lianMaiVm.applyLianMai()
                            } else {
                                Toast.makeText(
                                    AppContext.getContext(),
                                    "These permissions are denied: $deniedList",
                                    Toast.LENGTH_LONG
                                ).show()
                            }
                        }
                }
            }
        })
    }

    override fun initLiveData() {
        lianMaiVm.lianMaiListLiveData.observe(this){
            if (it != null){
                adapter.setNewData(it)
            }
        }

        lianMaiVm.applySuccessLiveData.observe(this){
            activity?.let {
                if (it is UserRoomActivity){
                    it.setRvState(false)
                }
            }
            dismissAllowingStateLoss()
        }

        lianMaiVm.cancelSuccessLiveData.observe(this){
            activity?.let {
                if (it is UserRoomActivity){
                    it.setRvState(true)
                }
            }
            dismissAllowingStateLoss()
        }
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {}

    override fun receiveStopLive() {
        dismissAllowingStateLoss()
    }

    override fun closeRoomed() {
    }

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

}