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.PersonDataDFragment
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.BlackListModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.user.adapter.FollowFanAdapter

/**
 *@date 2021/8/14 13:18
 *description:
 */

@Route(path = ARouteConstant.User.followFan)
class FollowAndFansActivity : BaseListActivity<LayoutListBinding,BlackListModel.ListBean>() {

    /**
     * 0 关注列表
     * 1 粉丝列表
     */
    @Autowired(required = true)
    @JvmField
    var type:Int = 0

    private var position = -1


    private val userVm by viewModels<UserVm>()

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

    override fun initViewData() {
        ARouter.getInstance().inject(this)
        smartRecyclerView.recyclerView?.addItemDecoration(ListItemDecoration())

        setBackView(binding.toolbar.ivBack)

        when(type){
            0 -> {
                binding.toolbar.tvTitle.text = AppContext.getContext().resources.getString(R.string.following)
            }

            1 -> {
                binding.toolbar.tvTitle.text = AppContext.getContext().resources.getString(R.string.followers)
            }
        }

        adapter.setOnItemChildClickListener { _, _, position ->
            this.position = position
            userVm.followUser(adapter.data[position].id,1)
        }

        adapter.setOnItemClickListener { _, _, position ->
            if (adapter.data.size>position){
                PersonDataDFragment.newInstance(adapter.data[position].id,
                    isAnchor = false,
                    inRoom = false
                ).show(supportFragmentManager,"PersonDataDFragment")
            }
        }
    }

    inner class ListItemDecoration : 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)
        }
    }

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

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