package com.gh.gamecenter.common.view

import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.widget.TextView
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.callback.SimpleCallback
import com.gh.gamecenter.common.utils.TextHelper
import com.lightgame.utils.Utils

/**
 * 默认用 ###高亮内容### 格式来高亮文字并添加点击复制至剪贴板功能的 TextView
 */
class HighlightableTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    TextView(context, attrs) {
    init {
        movementMethod = CustomLinkMovementMethod.getInstance()
        highlightColor = Color.TRANSPARENT
    }

    override fun setText(text: CharSequence?, type: BufferType?) {
        super.setText(
            TextHelper.getHighlightedSpannableStringThatIsWrappedInsideWrapper(
                context,
                text.toString(),
                "###",
                R.color.theme_font,
                object :
                    SimpleCallback<String> {
                    override fun onCallback(arg: String) {
                        val application = HaloApp.getInstance()
                        val cmb = application.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                        cmb.text = arg
                        Utils.toast(application, "已复制：$arg")
                    }
                }), BufferType.SPANNABLE
        )
    }
}