package com.swago.user.roommanager

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.Route
import com.chad.library.adapter.base.BaseQuickAdapter
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.dialog.ChoiceDialogFragment
import com.swago.baseswago.model.ListBean
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.SwagoLoading
import com.swago.user.R

@Route(path = ARouteConstant.User.roomManager)
class RoomManagerActivity : BaseListActivity<LayoutListBinding, ListBean>() {

    private val userVm by viewModels<UserVm>()

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

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(ListItemDecoration())
        binding.toolbar.tvTitle.text =
            AppContext.getContext().resources.getString(R.string.room_manager)
        setBackView(binding.toolbar.ivBack)

        adapter.setOnItemChildClickListener { _, _, position ->
            if (adapter.data.size>position){
                ChoiceDialogFragment.newInstance(AppContext.getContext().getString(R.string.do_delete_room_admin)).apply {
                    this.sureFun = {
                        SwagoLoading.showLoadingDialog(this@RoomManagerActivity)
                        userVm.setRoomAdmin(adapter.data[position].admin_user_id,2)
                    }
                }.show(supportFragmentManager,"ChoiceDialogFragment")

            }
        }
    }

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

        userVm.setRoomAdminLiveData.observe(this){
            smartRecyclerView.startRefresh()
        }
    }

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