package mudslingingic.hackic.thanksic

import android.content.Context
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.StyleSpan
import android.view.ViewGroup
import android.widget.TextView
import com.easy.clean.R
import com.easy.clean.databinding.ImmobileicBinding
import heartburnic.Chairic
import tooic.fatalityic.redic.speckledic.hankeringic.Resuscitateic.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class BatteryHead(
    val min: Int,
    val p: Float = 0.50f,
    val temper: String = "36℃",
    val v: String = "3.6V",
    val power: String = "4753mAh",
)

class Laboratoryic : BaseQuickAdapter<BatteryHead, Chairic>() {

    override fun onBindViewHolder(
        holder: Chairic,
        position: Int,
        item: BatteryHead?,
    ) {
        item ?: return
        val binding = ImmobileicBinding.bind(holder.itemView)
        binding.divorceeic.text = "${(item.p * 100).toInt()}%"

        binding.ladleic.setImageLevel((item.p * 100).toInt())

        setText(binding.chowderic, item.min)
        setText(binding.foghornic, (item.min * 0.5).toInt())
        setText(binding.redemptionic, (item.min * 0.8).toInt())
        binding.erectic.text = item.temper
        binding.speakic.text = item.v
        binding.packingic.text = item.power
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int,
    ): Chairic {
        return Chairic(R.layout.immobileic.inflate(parent))
    }

    fun setText(item: TextView, time: Int) {
        var h = time / 60
        var m = time % 60
        val strH = if (h < 10) "0$h" else h.toString()
        val strM = if (m < 10) "0$m" else m.toString()
        val text1 = "$strH h "
        val text2 = "$strM m"
        item.text = buildSpannableString(text1, strH)
        item.append(buildSpannableString(text2, strM))
    }

    fun buildSpannableString(fullString: String, tagString: String): SpannableString {
        val spannableString = SpannableString(fullString)
        val start = fullString.indexOf(tagString)
        val end = start + tagString.length

        spannableString.setSpan(
            StyleSpan(Typeface.BOLD),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spannableString.setSpan(
            AbsoluteSizeSpan(18, true),
            start, end,
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        return spannableString
    }
}