package com.ux.video.file.filerecovery.utils

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import com.ux.video.file.filerecovery.R

class CustomTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatTextView(context, attrs, defStyleAttr) {

    companion object {
        private var regular: Typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
          var bold: Typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
        private var alimama: Typeface? = null
    }

    private var selectedText: String? = null
    private var normalText: String? = null

    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.CustomTextView)
            val type = typedArray.getInt(R.styleable.CustomTextView_fontType, 0)
            selectedText = typedArray.getString(R.styleable.CustomTextView_selected_text)
            normalText = typedArray.getString(R.styleable.CustomTextView_normal_text)
            typedArray.recycle()
            if (alimama == null) {
                alimama = Typeface.createFromAsset(context.assets, "fonts/alimama.ttf")
            }
            when (type) {
                0 -> {
                    typeface = regular
                }

                1 -> {

                    typeface = bold
                }

                2 -> {
                    typeface = alimama
                }


                else -> typeface = Typeface.DEFAULT
            }
        }
    }

    override fun setSelected(selected: Boolean) {
        super.setSelected(selected)
        updateText()
    }

    private fun updateText() {
//        Common.showLog("--updateText：$isSelected  selectedText=${selectedText} normalText=${normalText} text=${text}")
        text = if (isSelected) selectedText ?: text else normalText ?: text
    }
}
