package com.swago.room.lianmai

import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.viewModels
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
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.ApplyLianMaiResultModel
import com.swago.baseswago.model.live.mic.LianMaiUserModel
import com.swago.room.R
import com.swago.room.databinding.DialogAnchorLianMaiBinding


/**
 * 主播端连麦列表
 */
class AnchorLianMaiDialog :  BaseXDFragment<DialogAnchorLianMaiBinding>() {


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

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

    private val lianMaiVm by viewModels<LianMaiVm>()

    private val adapter by lazy {
        AnchorLianMaiListAdapter()
    }

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

        lianMaiVm.cancelSuccessLiveData.observe(this){
            dismissAllowingStateLoss()
        }

        lianMaiVm.acceptSuccessLiveData.observe(this){
            dismissAllowingStateLoss()
        }
    }

    override fun initOther() {
        binding.swagoRv.layoutManager = LinearLayoutManager(context)
        binding.swagoRv.adapter = adapter
        lianMaiVm.getLianMaiList()
        adapter.setOnItemChildClickListener { _, view, position ->
            if (adapter.data.size>position){
                when(view.id){
                    R.id.ivRefuse -> {
                        lianMaiVm.cancelApplyLianMai(adapter.data[position].id)
                    }
                    R.id.ivAccept -> {
                        LianMaiManager.lianMaiId = adapter.data[position].id
                        lianMaiVm.acceptLianMai()
                    }
                }
            }
        }
    }

}