package com.xty.common.weight

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.xty.common.R

class MyShowValue(context: Context, attributeSet: AttributeSet) :
    ConstraintLayout(context, attributeSet) {

    private var values = ""
    private var unit = ""
    private var valueColor = 0
    private var unitColor = 0
    private var desColor = 0
    private var desc = ""

    private lateinit var mTvValue: TextView
    private lateinit var mTvUnit: TextView
    private lateinit var mTvDesc: TextView

    init {
        var array = context.obtainStyledAttributes(attributeSet, R.styleable.MyShowValue)

        values = array.getString(R.styleable.MyShowValue_num).toString()
        unit = array.getString(R.styleable.MyShowValue_units).toString()
        desc = array.getString(R.styleable.MyShowValue_desc).toString()
        valueColor = array.getColor(
            R.styleable.MyShowValue_values_color,
            ContextCompat.getColor(context, R.color.black)
        )
        unitColor = array.getColor(
            R.styleable.MyShowValue_unt_color,
            ContextCompat.getColor(context, R.color.black)
        )
        desColor = array.getColor(
            R.styleable.MyShowValue_desc_color,
            ContextCompat.getColor(context, R.color.black)
        )
        array.recycle()
        LayoutInflater.from(context).inflate(R.layout.view_my_show, this, true)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mTvValue = findViewById(R.id.mTvValue)
        mTvUnit = findViewById(R.id.mTvUnit)
        mTvDesc = findViewById(R.id.mTvDesc)

        mTvValue.setTextColor(valueColor)
        mTvUnit.setTextColor(unitColor)
        mTvDesc.setTextColor(desColor)
        mTvValue.text = values
        mTvUnit.text = unit
        mTvDesc.text = desc
    }

    fun setValue(values: String) {
        if (!TextUtils.isEmpty(values) && values != "0.0") {
            mTvValue.text = values
        } else {
            mTvValue.text = "--"
        }
    }

    fun setAllInfo(values: String, desc: String, unit: String = "") {
        mTvValue.text = values
        mTvDesc.text = desc
        mTvUnit.text = unit
    }
}