package com.swago.user.level

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.swago.baseswago.UserVm
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.mine.LevelModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.UserInfo
import com.swago.user.R
import com.swago.user.databinding.FragmentAnchorLevelBinding

/**
 *@date 2021/11/21 12:40
 *description:
 */
class AnchorLevelFragment : BaseXFragment<FragmentAnchorLevelBinding>() {

    private val userVm by viewModels<UserVm>()

    private val adapter by lazy {
        LevelAdapter()
    }

    override fun loadData() {
        userVm.getNextLevelExp(2)
    }

    override fun initOther() {
        UserInfo.getUserInfo()?.let {
            binding.tvLevel.text = it.user_level
            binding.tvStart.text = it.user_level
            binding.tvEnd.text = it.next_user_level
            binding.tvNeedToExp.text = AppContext.getContext().resources.getString(R.string.level_up_exp).format(it.next_user_need_jingyan)
            Glide.with(this).load(it.user_head_img_url)
                .placeholder(R.mipmap.default_avatar)
                .error(R.mipmap.default_avatar).into(binding.ivAvatar)
        }

        binding.rv.layoutManager = LinearLayoutManager(context)
        binding.rv.adapter = adapter
    }

    override fun initLiveData() {
        userVm.levelLiveData.observe(this){
            it.add(0, LevelModel("",AppContext.getContext().resources.getString(R.string.anchor_level),AppContext.getContext().resources.getString(R.string.need_exp)))
            adapter.setNewData(it)
        }
    }
}