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.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.UserVm
import com.swago.baseswago.activity.BaseListActivity
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.databinding.LayoutListBinding
import com.swago.baseswago.model.BlackListModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.IMUtil
import com.swago.user.adapter.BlackListAdapter

/**
 *@date 2021/8/28 14:26
 *description:
 */
class BlackListActivity:BaseListActivity<LayoutListBinding, BlackListModel.ListBean>() {

    private val userVm by viewModels<UserVm>()

    private var position = -1

    override val smartRecyclerView: SwagoRecyclerView<BlackListModel.ListBean> by lazy {
        binding.swagoRv as SwagoRecyclerView<BlackListModel.ListBean>
    }
    override val adapter: BaseQuickAdapter<BlackListModel.ListBean, *> by lazy {
        BlackListAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(this)
    }
    override val loadData: (page: Int) -> Unit = {
        userVm.getBlackList(it+1)
    }

    override fun initViewData() {
        setBackView(binding.toolbar.ivBack)
        binding.toolbar.tvTitle.text = AppContext.getContext().resources.getString(R.string.blacklist)

        smartRecyclerView.recyclerView?.addItemDecoration(VerticalItemDecoration())
        adapter.setOnItemChildClickListener { _, _, position ->
            this.position = position
            val list = ArrayList<String>()
            list.add(adapter.data[position].user_account)
            IMUtil.deleteBlackList(list){
                userVm.addBlack(adapter.data[position].user_account,1)
            }
        }
    }

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

        userVm.hasBeenBlackLiveData.observe(this) {
            adapter.data.removeAt(position)
            adapter.notifyItemRemoved(position)
        }
    }


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