package com.zj365.dc.adapter

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ImageSpan
import android.text.style.LeadingMarginSpan
import android.text.style.ReplacementSpan
import android.widget.ImageView
import androidx.annotation.RequiresApi
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.getImageUrl
import com.xty.common.setImage
import com.xty.network.model.ScienceBean
import com.zj365.dc.R

class ScienceAdapter:BaseAdapter<ScienceBean>(R.layout.item_scence) {

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun convert(holder: BaseViewHolder, item: ScienceBean) {
        var text = " "+item.name
        var builder = SpannableStringBuilder()
        if (item.isNew ==1) {
            builder.append(text)
            val bgDrawable: Drawable = context.resources.getDrawable(R.mipmap.ic_icon_new)
            bgDrawable.setBounds(0, 0, bgDrawable.intrinsicWidth, bgDrawable.intrinsicHeight)
            val bgSpan = ImageSpan (bgDrawable,ImageSpan.ALIGN_CENTER)
            builder.setSpan(bgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            val spanStart = 1
            val spanEnd = 2

            val gapWidth = 20
            val indent = 60
            builder.setSpan(LeadingMarginSpan.Standard(indent, gapWidth), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

            val marginRight = 10
            builder.setSpan(EmptySpan(), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            builder.setSpan(MarginSpan(marginRight), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)


        } else {
            builder.append(text)
        }
        holder.setText(R.id.mTitle,builder)
        holder.setText(R.id.mTime, if (item.time.isNullOrEmpty()) "" else item.time)
        holder.setText(R.id.mOrgName, if (item.orgName.isNullOrEmpty()) "" else item.orgName)
        holder.getView<ImageView>(R.id.mImage).apply {
            setImage(context, getImageUrl(item.bannerUrl))
        }
    }
    // 定义一个空的 span
    class EmptySpan : ReplacementSpan() {
        override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
            return 0
        }

        override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
        }
    }

    // 定义一个带右侧 margin 的 span
    class MarginSpan(private val marginRight: Int) : ReplacementSpan() {
        override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
            return paint.measureText(text, start, end).toInt() + marginRight
        }

        override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
            canvas.drawText(text, start, end, x + marginRight, y.toFloat(), paint)
        }
    }

}