package com.zj365.health.fragment

import android.graphics.Typeface
import android.text.TextUtils
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.getImageUrl
import com.xty.common.setImageUser
import com.xty.common.util.SpannableUtils
import com.zj365.health.R
import com.zj365.health.act.RankListAct
import com.zj365.health.adapter.StepRankAdapter
import com.zj365.health.databinding.FragStepCommunityRankBinding
import com.zj365.health.vm.RankListVm

/**
 * 社区步数排行榜
 */
class CommunityStepRankFrag : BaseVmFrag<RankListVm>() {
    val binding by lazy { FragStepCommunityRankBinding.inflate(layoutInflater) }
    val adapter by lazy { StepRankAdapter() }
    var userId :String?=null
    val healthStr by lazy { resources.getStringArray(R.array.status_array) }
    override fun setLayout() = binding.root
    override fun initView() {
        super.initView()
        initRecycle()
        userId?.let {
            mViewModel.getStepRank(1,it)
        }

        binding.mTvRank.text = String.format(resources.getString(R.string.rank), "-")
//        CommonUtils.getMyUserInfo()?.let {
//            binding.mImage.setImageUser(requireContext(), it.avatarUrl)
//        }
//        binding.mTvRankInfo.text = healthStr[(activity as RankListAct).status]
    }

    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) {
                binding.mTvRank.text =
                    String.format(resources.getString(R.string.rank), if (it.data.userRank == 0) "-" else "${it.data.userRank}")

                val rank = "${it.data.userSumStep}步"
                binding.mTvStepInfo.text = SpannableUtils.setStrSpan(
                    rank,
                    20,
                    ContextCompat.getColor(
                        requireContext(), R.color.col_02c
                    ),
                    Typeface.BOLD,
                    0,
                    rank.length - 1
                )

                binding.mTvRankInfo.text = it.data.userContent
                adapter.setNewInstance(it.data.all)
            } else {
                adapter.setEmptyView(getNoneView())
            }
            activity.let {  act ->
                if (act is RankListAct){
                    if (!TextUtils.isEmpty(it.data.community)) {
                        act.changeSubTitle(it.data.community!!)
                    }
                }
            }
            it.data.userImages?.let {
                binding.mImage.setImageUser(requireContext(), getImageUrl(it))
            }
        }
    }

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