package com.swago.user.black

import android.graphics.Rect
import android.view.View
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.HandleVm
import com.swago.baseswago.UserVm
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.model.mine.RoomBlack
import com.swago.baseswago.util.DpPxUtil
import com.swago.user.adapter.RoomBlackAdapter
import com.swago.user.databinding.FragmentBlackBinding

/**
 *@date 2022/2/26 10:35
 *description:主播黑名单列表
 */
class AnchorBlackFragment : BaseListFragment<FragmentBlackBinding, RoomBlack>() {
    private val userVm by viewModels<UserVm>()
    private val handleVm by viewModels<HandleVm>()

    private var position = -1

    override val smartRecyclerView: SwagoRecyclerView<RoomBlack> by lazy {
        binding.swagoRv as SwagoRecyclerView<RoomBlack>
    }
    override val adapter: BaseQuickAdapter<RoomBlack, *> by lazy {
        RoomBlackAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
        userVm.getAnchorBlackList(it+1)
    }

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(VerticalItemDecoration())
        adapter.setOnItemChildClickListener { _, _, position ->
            if (adapter.data.size > position) {
                /* * black_type  用户类型 1拉黑主播 2拉黑用户
                 * type 类型(0取消拉黑1拉黑)*/
                this.position = position
                handleVm.blackUser(adapter.data[position].black_user_id, 1, 0)
            }
        }
    }

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

        handleVm.blackTypeLiveData.observe(this) {
            if (it == 0) {
                adapter.data.removeAt(position)
                adapter.notifyDataSetChanged()
            }
        }
    }
    inner class VerticalItemDecoration : RecyclerView.ItemDecoration() {

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


}