package com.zj365.health.fragment

import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.setImageUser
import com.xty.common.util.CommonUtils
import com.zj365.health.R
import com.zj365.health.adapter.RankAdapter
import com.zj365.health.databinding.FragCommunityRankBinding
import com.zj365.health.vm.RankListVm

/**
 * 全国排行榜
 */
class WholeRankFrag: BaseVmFrag<RankListVm>() {
    //val binding by lazy{FragWholeRankBinding.inflate(layoutInflater)}
    val binding by lazy{ FragCommunityRankBinding.inflate(layoutInflater)}
    val adapter by lazy { RankAdapter() }
    override fun setLayout()=binding.root
    override fun initView() {
        super.initView()
        initRecycle()
         mViewModel.getRank(2)
        binding.mTvRankInfo.text=String.format(resources.getString(R.string.rank),"-")
        binding.mImage.setImageUser(requireContext(), CommonUtils.getMyUserInfo()?.avatarUrl)
    }

    fun initRecycle(){
        binding.mRecycle.layoutManager= LinearLayoutManager(requireContext())
        binding.mRecycle.adapter=adapter

    }

    override fun setViewModel()= RankListVm()

    override fun observer() {
        mViewModel.rankLive.observe(this) {
            if (it.data.all != null) {
//                for (all in it.data.all) {
//                    if (!all.content.isNullOrEmpty()) {
//                        binding.mTvRank.text =
//                            String.format(resources.getString(R.string.rank), all.rank)
//                        binding.mTvRankInfo.text = all.content
//                        break
//                    }
//                }
                binding.mTvRank.text =
                    if (it.data.userRank == 0) "我的排名" else  String.format(resources.getString(R.string.rank), it.data.userContent)
                binding.mTvRankInfo.text = if (it.data.userRank == 0) "-" else "第${it.data.userRank}名"
                adapter.setNewInstance(it.data.all)
            }else{
                adapter.setEmptyView(getNoneView())
            }
        }
    }

    private fun getNoneView() =
        LayoutInflater.from(requireContext()).inflate(com.xty.common.R.layout.view_none_data, binding.mRecycle, false)

}