package com.gh.gamecenter.core.utils

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.*
import android.view.View
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.gh.gamecenter.core.HaloApp

class SpanBuilder(content: CharSequence) {
    private var spannableString: SpannableStringBuilder = SpannableStringBuilder(content)

    fun color(context: Context, start: Int, end: Int, colorRes: Int): SpanBuilder {
        val colorSpan = ForegroundColorSpan(ContextCompat.getColor(context, colorRes))
        spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    fun color(start: Int, end: Int, colorHexInString: String): SpanBuilder {
        try {
            val colorSpan = ForegroundColorSpan(Color.parseColor(colorHexInString))
            spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return this
    }

    fun size(start: Int, end: Int, dpSize: Int): SpanBuilder {
        val s = AbsoluteSizeSpan(dpSize, true)
        spannableString.setSpan(s, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    //删除线
    fun strikeThrough(start: Int, end: Int): SpanBuilder {
        val strike = StrikethroughSpan()
        spannableString.setSpan(strike, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    //加粗
    fun bold(start: Int, end: Int): SpanBuilder {
        val styleSpan = StyleSpan(Typeface.BOLD)
        spannableString.setSpan(styleSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    //正常字体
    fun normal(start: Int, end: Int): SpanBuilder {
        val styleSpan = StyleSpan(Typeface.NORMAL)
        spannableString.setSpan(styleSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    //添加图标
    fun image(start: Int, end: Int, @DrawableRes res: Int): SpanBuilder {
        val imageSpan = CenterImageSpan(HaloApp.getInstance(), res)
        spannableString.setSpan(imageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    //添加图标
    fun image(start: Int, end: Int, drawable: Drawable): SpanBuilder {
        val imageSpan = CenterImageSpan(drawable)
        spannableString.setSpan(imageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    fun click(
        context: Context,
        start: Int,
        end: Int,
        colorRes: Int,
        isUnderlineText: Boolean = false,
        onClick: () -> Unit
    ): SpanBuilder {
        val clickSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(context, colorRes)
                ds.isUnderlineText = isUnderlineText
            }

            override fun onClick(widget: View) {
                onClick.invoke()
            }
        }
        spannableString.setSpan(clickSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return this
    }

    fun build(): SpannableStringBuilder {
        return spannableString
    }
}