package com.xyzshell.myphoneinfo.custom

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.google.android.material.textview.MaterialTextView
import com.xyzshell.myphoneinfo.R
import androidx.core.content.withStyledAttributes


class LabelValueCustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val tvLabel: MaterialTextView
    private val tvValue: MaterialTextView
    private val tvSubTitle: MaterialTextView

    init {
        LayoutInflater.from(context).inflate(R.layout.common_dialog_item, this, true)
        tvValue = findViewById(R.id.tv_value)
        tvLabel = findViewById(R.id.tv_label)
        tvSubTitle = findViewById(R.id.tv_sub_title)

        orientation = HORIZONTAL

        attrs?.let {
            context.withStyledAttributes(it, R.styleable.LabelValueCustomView) {
                val label = getString(R.styleable.LabelValueCustomView_labelText) ?: ""
                val value = getString(R.styleable.LabelValueCustomView_valueText) ?: ""
                val subtitle = getString(R.styleable.LabelValueCustomView_subTitleText)

                tvSubTitle.run {
                    visibility = if (subtitle.isNullOrBlank()) GONE else VISIBLE
                    text = subtitle
                }
                tvLabel.text = label
                tvValue.text = value

            }
        }
    }

    fun setLabel(text: String) {
        tvLabel.text = text
    }

    fun setValue(text: String) {
        tvValue.text = text
    }

    fun getValue(): String = tvValue.text.toString()
}
