package com.swago.baseswago.cusview

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.swago.baseswago.R
import com.swago.baseswago.databinding.ViewSwagoLevelBinding
import com.swago.baseswago.util.DpPxUtil

/**
 *@date 2022/1/17 20:54
 *description:
 */
class SwagoLevelView : FrameLayout {

    private var binding : ViewSwagoLevelBinding? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initView(context)
    }

    private fun initView(context: Context) {
        binding = ViewSwagoLevelBinding.inflate(LayoutInflater.from(context),this,true)
    }

    fun setUserLevel(level:Int){
        binding?.let {
            if (level in 0..9){
                it.tvLevel.text = "0$level"
            }else{
                it.tvLevel.text = level.toString()
            }
            when (level) {
                in 0..10 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level10)
                }
                in 11..15 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level15)
                }
                in 16..29 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level29)
                }
                in 30..38 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level38)
                }
                in 39..48 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level48)
                }
                in 49..55 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level55)
                }
                in 56..65 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level65)
                }
                in 66..76 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level76)
                }
                in 77..88 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level88)
                }
                in 89..98 -> {
                    it.ivBg.setBackgroundResource(R.mipmap.level98)
                }
                else ->{
                    it.ivBg.setBackgroundResource(R.mipmap.level100)
                }
            }
        }
    }

}