package com.xty.common

import android.R
import android.content.Context
import android.text.Editable
import android.text.Html
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.text.style.ImageSpan
import android.view.View
import com.hjq.toast.ToastUtils
import com.xty.common.picture.PictureUtils
import org.xml.sax.XMLReader
import java.util.*


class MyTagHandler(var context: Context) : Html.TagHandler {
    override fun handleTag(opening: Boolean, tag: String, output: Editable?, xmlReader: XMLReader?) {
        // 处理标签<img>
        // 处理标签<img>
        if (tag.toLowerCase(Locale.getDefault()).equals("img")) {
            // 获取长度
            val len: Int = output!!.length
            // 获取图片地址
            val images: Array<ImageSpan> = output!!.getSpans(len - 1, len, ImageSpan::class.java)
            val imgURL: String = images[0].source!!


            // 使图片可点击并监听点击事件
             output!!.setSpan(
                ClickableImage(  imgURL),
                len - 1,
                len,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }

    }

    inner  class ClickableImage( var url:String): ClickableSpan() {

        override fun onClick(widget: View) {
            PictureUtils.httpPreviewImage(context,0, mutableListOf<String>().apply {  this.add(url) })
        }

    }

}