package com.xty.common.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.text.style.ReplacementSpan
import android.util.TypedValue
import androidx.core.content.ContextCompat
import com.xty.common.R

class IconTextSpan(context: Context, content: String) : ReplacementSpan() {

    private lateinit var mContext: Context
    private var mContent: String = ""
    private var mBgColorResId: Int = 0//背景色资源Id
    private var mBgWidth: Float = 0f
    private var mBgHeight: Float = 0f
    private var mRadius: Float = 0f//背景圆角半径
    private var mRightMargin: Float = 0f//右间距
    private var mTextSize: Float = 0f
    private var mTextColorResId: Int = 0//字体色资源ID


    private lateinit var mBgPaint: Paint//背景画笔
    private lateinit var mTextPaint: Paint//文本画笔

    init {
        if (!content.isNullOrEmpty()){
            initDefaultValue(context, content)

            //计算背景宽度
            mBgWidth = calcBgWidth(content)

            //初始化画笔
            initPaint()
        }
    }

    /**
     * 初始化默认数值
     */
    fun initDefaultValue(context: Context, content: String) {
        mContext = context
        mContent = content
        mBgColorResId = R.color.col_c64
        mTextColorResId = R.color.col_009
        mBgHeight = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            17f,
            mContext.resources.displayMetrics
        )
        mRightMargin = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            2f,
            mContext.resources.displayMetrics
        )
        mRadius = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            2f,
            mContext.resources.displayMetrics
        )
        mTextSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            14f,
            mContext.resources.displayMetrics
        )
    }

    /**
     * 根据文字内容来计算背景宽度
     */
    fun calcBgWidth(text: String): Float {
        if (text.isNullOrEmpty()) return 0f
        else if (text.length == 1) return mBgHeight //如果为单字，为正方形，长宽一致
        else {//宽度=文字宽度+padding宽度
            var rect = Rect()
            var paint = Paint()
            paint.textSize = mTextSize
            paint.getTextBounds(text, 0, text.length, rect)
            var padding = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                4f,
                mContext.resources.displayMetrics
            )
            return rect.width() + padding * 2
        }
    }

    /**
     * 初始化画笔
     */
    fun initPaint() {
        //初始化背景画笔
        mBgPaint = Paint()
        mBgPaint.color = ContextCompat.getColor(mContext, mBgColorResId)
        mBgPaint.style = Paint.Style.FILL
        mBgPaint.isAntiAlias = true //抗锯齿
        //初始化文字画笔
        mTextPaint = Paint()
        mTextPaint.color = ContextCompat.getColor(mContext, mTextColorResId)
        mTextPaint.textSize = mTextSize//文本大小
        mTextPaint.isAntiAlias = true//抗锯齿
        mTextPaint.textAlign = Paint.Align.CENTER//文本居中
    }

    override fun getSize(
        p0: Paint,
        p1: CharSequence?,
        p2: Int,
        p3: Int,
        p4: Paint.FontMetricsInt?
    ): Int {
        return (mBgWidth + mRightMargin) as Int
    }

    override fun draw(
        p0: Canvas,
        p1: CharSequence?,
        p2: Int,
        p3: Int,
        p4: Float,
        p5: Int,
        p6: Int,
        p7: Int,
        p8: Paint
    ) {
        var metrics = mBgPaint.fontMetrics
        var textHeight = metrics.descent - metrics.ascent
        //算出Y坐标
        var bgStartY = p6 + (textHeight - mBgHeight) / 2 + metrics.ascent

        //画背景
        var bgRect = RectF(p4, bgStartY, p4 + mBgWidth, bgStartY + mBgHeight)
        p0.drawRoundRect(bgRect, mRadius, mRadius, mBgPaint)

        //画字体
        var fontMetrics = mTextPaint.fontMetrics
        var textRectHeight = fontMetrics.bottom - fontMetrics.top
        p0.drawText(mContent, p4 + mBgWidth / 2, bgStartY + (mBgHeight - textRectHeight) / 2 - fontMetrics.top, mTextPaint)
    }

}