package com.swago.room.hongbao

import android.os.Bundle
import android.view.Gravity
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.model.live.RedReceiveModel
import com.swago.baseswago.util.AppContext
import com.swago.loadUrl
import com.swago.room.R
import com.swago.room.databinding.DialogRedDetailListBinding
import com.swago.room.vm.RoomOtherVm

class RedDetailListDialog : BaseListDialogFragment<DialogRedDetailListBinding,RedReceiveModel>() {

    private val roomOtherVm  by viewModels<RoomOtherVm>()

    private var packageId = ""
    private var avatar = ""
    private var name = ""
    private var level = 1

    companion object{
        fun newInstance(packageId:String,avatar:String,name:String,level:Int): RedDetailListDialog {
            val args = Bundle()
            args.putString("packageId",packageId)
            args.putString("avatar",avatar)
            args.putString("name",name)
            args.putInt("level",level)
            val fragment = RedDetailListDialog()
            fragment.arguments = args
            return fragment
        }
    }

    init {
        setGravity(Gravity.BOTTOM)
        setCanCancel(false)
        setDimAmount(0.5f)
    }


    override val smartRecyclerView: SwagoRecyclerView<RedReceiveModel> by lazy {
        binding.swagoRv as SwagoRecyclerView<RedReceiveModel>
    }
    override val adapter: BaseQuickAdapter<RedReceiveModel, *> by lazy {
        RedReceivedAdapter()
    }

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

    override fun initViewData() {
        binding.ivClose.setOnClickListener {
            dismissAllowingStateLoss()
        }
        arguments?.let {
            packageId = it.getString("packageId","")
            avatar = it.getString("avatar","")
            name = it.getString("name","")
            level = it.getInt("level",1)
            activity?.let {
                binding.ivAvatar.loadUrl(it,avatar)
                binding.tvName.text = AppContext.getContext().resources.getString(R.string.from_who_red_envelope).format(name)
                binding.tvLevel.setUserLevel(level)
            }
        }
    }

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

    override fun loadMoreNeed(): Boolean {
        return false
    }

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