package com.swago.room.dialog

import android.graphics.Rect
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
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.model.live.RoomUserModel
import com.swago.baseswago.util.DpPxUtil
import com.swago.room.adapter.AudienceAdapter
import com.swago.room.databinding.DialogAudienceListBinding
import com.swago.room.vm.RoomVm

/**
 *@date 2022/3/29 20:17
 *description:
 */
class AudienceListDialog : BaseListDialogFragment<DialogAudienceListBinding,RoomUserModel.ListBean>() {

    private val roomVm by activityViewModels<RoomVm>()

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

    override val smartRecyclerView: SwagoRecyclerView<RoomUserModel.ListBean> by lazy {
        binding.swagoRv as SwagoRecyclerView<RoomUserModel.ListBean>
    }
    override val adapter: BaseQuickAdapter<RoomUserModel.ListBean, *> by lazy {
        AudienceAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
       roomVm.loadUserInDialogData(it+1)
    }

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

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(ListItemDecoration())
    }

    override fun initLiveData() {
       roomVm.roomUserListDialogLiveData.observe(this){
           if (it == null){
               smartRecyclerView.onFetchDataError()
           }else{
               smartRecyclerView.onFetchDataFinish(it.list,true)
           }
       }
    }



    inner class ListItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            val margin = DpPxUtil.dip2px(8f)
            outRect.set(0, 0, 0, margin)
        }
    }
}