package com.zj365.health.fragment

import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.getImageUrl
import com.xty.common.setImage
import com.zj365.health.R
import com.zj365.health.adapter.StepRankAdapter
import com.zj365.health.databinding.FragStepCommunityRankBinding
import com.zj365.health.vm.RankListVm

/**
 * 全国步数排行榜
 */
class WholeStepRankFrag: BaseVmFrag<RankListVm>() {
    //val binding by lazy{FragWholeRankBinding.inflate(layoutInflater)}
    val binding by lazy{ FragStepCommunityRankBinding.inflate(layoutInflater)}
    val adapter by lazy { StepRankAdapter() }
    val healthStr by lazy { resources.getStringArray(R.array.status_array)}
    var userId :String?=null
    override fun setLayout()=binding.root
    override fun initView() {
        super.initView()
        initRecycle()
        userId?.let {
            mViewModel.getStepRank(2,it)
        }
        binding.mTvRank.text=String.format(resources.getString(R.string.rank),"-")
//        binding.mTvRankInfo.text=healthStr[(activity as RankListAct).status]
//        binding.mImage.setImageUser(requireContext(), CommonUtils.getMyUserInfo()?.avatarUrl)
    }

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

    }

    override fun initData() {
        super.initData()
        userId = arguments?.getString("userId")

    }
    override fun setViewModel()= RankListVm()

    override fun observer() {
        mViewModel.stepRankLive.observe(this) {
            if (it.data.all != null) {
                if (it.data.userSumStep == 0) {
                    binding.mTvRank.text = "--步"
                } else {
                    binding.mTvRank.text = "${it.data.userSumStep}步"
                }
//                    String.format(resources.getString(R.string.rank), if (it.data.userRank == 0) "-" else "${it.data.userRank}")
//                val rank = "${it.data.userSumStep}步"
                if (it.data.userRank == 0) {
                    binding.mTvStepInfo.text = "第--名"
                } else {
                    binding.mTvStepInfo.text = "第${it.data.userRank}名"
                }
//                    SpannableUtils.setStrSpan(
//                    rank,
//                    20,
//                    ContextCompat.getColor(
//                        requireContext(), R.color.col_009
//                    ),
//                    Typeface.BOLD,
//                    0,
//                    rank.length - 1
//                )
                binding.mTvRankInfo.text = it.data.userContent
                adapter.setNewInstance(it.data.all)
            }else{
                adapter.setEmptyView(getNoneView())
            }
            binding.mImage.setImage(requireContext(),getImageUrl(it.data.userImages))
        }
    }

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

}