package com.bc_chat.bc_base.utils

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import com.bc_chat.bc_base.R

/**
 * author：Created by zhaohaoting on 2019/2/21
 * email：526309416@qq.com
 * desc：
 */
object StringUtils {
     fun protocol(context: Context, protocol: CharSequence, textView: TextView, listener: (view: View) -> Unit) {
        textView.movementMethod = LinkMovementMethod.getInstance()
        val stringBuilder = SpannableStringBuilder(protocol)
        val foregroundColorSpan = ForegroundColorSpan(context.resources.getColor(R.color.theme_text_color))
        val startIndex = protocol.indexOf("《")
        val endIndex = protocol.indexOf("》") + 1
        stringBuilder.setSpan(foregroundColorSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        stringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                listener(widget)
            }

            override fun updateDrawState(ds: TextPaint) {
            }
        }, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.text = stringBuilder
    }
}