package com.swago.room.pk

import android.os.Bundle
import android.view.Gravity
import androidx.core.widget.addTextChangedListener
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.cusview.SwagoRecyclerView
import com.swago.baseswago.dialog.BaseListDialogFragment
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.model.live.RoomUserModel
import com.swago.baseswago.model.live.pk.PKLiverModel
import com.swago.baseswago.util.LogUtil
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.databinding.DialogPkSearchBinding

class PKSearchLisDialog : BaseListDialogFragment<DialogPkSearchBinding,PKLiverModel>() {

    private val pkVm by viewModels<PkVm>()

    var openPKInviteFun:((pkLiverModel:PKLiverModel)->Unit)? =null

    init {
        setGravity(Gravity.BOTTOM)
        setCanCancel(true)
        setDimAmount(0f)
    }

    companion object{
        fun newInstance(): PKSearchLisDialog {
            val args = Bundle()
            val fragment = PKSearchLisDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override val smartRecyclerView: SwagoRecyclerView<PKLiverModel> by lazy {
        binding.rvPKList as SwagoRecyclerView<PKLiverModel>
    }
    override val adapter: BaseQuickAdapter<PKLiverModel, *> by lazy {
        PKListAdapter()
    }

    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
        pkVm.getDefault()
    }

    override fun initViewData() {
        binding.ivSearch.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                val content = binding.etEdit.text.toString()
                pkVm.getDefault(content)
            }
        })

        binding.etEdit.addTextChangedListener {
            it?.let {
                if (it.toString().isEmpty()){
                    pkVm.getDefault()
                }
            }
        }

        adapter.setOnItemChildClickListener { _, _, position ->
            openPKInviteFun?.invoke(adapter.data[position])
            dismissAllowingStateLoss()
        }
    }

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

    override fun loadMoreNeed(): Boolean {
        return false
    }
}