package com.swago.user

import android.graphics.Rect
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.HandleVm
import com.swago.baseswago.UserVm
import com.swago.baseswago.activity.BaseListActivity
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.databinding.LayoutListBinding
import com.swago.baseswago.model.mine.RoomBlack
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.user.adapter.BlackListAdapter
import com.swago.user.adapter.RoomBlackAdapter

/**
 *@date 2022/2/8 18:15
 *description:
 */
@Route(path = ARouteConstant.User.roomBlacklist)
class RoomBlackListActivity : BaseListActivity<LayoutListBinding, RoomBlack>() {

    @Autowired
    @JvmField
    var blackType: Int = 1


    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(this)
    }
    override val loadData: (page: Int) -> Unit = {
        userVm.getRoomBlackList(blackType, it + 1)
    }

    override fun initViewData() {
        ARouter.getInstance().inject(this)
        setBackView(binding.toolbar.ivBack)

        when (blackType) {
            1 -> {
                binding.toolbar.tvTitle.text =
                    AppContext.getContext().resources.getString(R.string.room_anchor_black_list)
            }
            2 -> {
                binding.toolbar.tvTitle.text =
                    AppContext.getContext().resources.getString(R.string.room_user_black_list)
            }
        }
        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, blackType, 0)
            }
        }
    }

    override fun initLiveData() {
        userVm.roomBlackListLiveData.observe(this) {
            smartRecyclerView.onFetchDataFinish(it, true)
        }

        handleVm.blackTypeLiveData.observe(this) {
            if (it == 0) {
                adapter.data.removeAt(position)
                adapter.notifyItemRemoved(position)
            }
        }
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }


    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)
        }
    }

}