package com.adealink.weparty.module.playmate.widget

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.TypefaceSpan
import android.util.AttributeSet
import android.util.TypedValue
import androidx.core.content.withStyledAttributes
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.weparty.R
import com.adealink.weparty.commonui.DEFAULT_FONT_BOLD
import com.adealink.weparty.commonui.DEFAULT_FONT_REGULAR
import com.adealink.weparty.commonui.widget.AutoMarqueeTextView
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.commonui.widget.util.getColorX
import com.adealink.weparty.commonui.widget.util.getDrawableX
import com.adealink.weparty.util.formatNumberStr

@SuppressLint("SetTextI18n")
class PriceMarqueeView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AutoMarqueeTextView(context, attrs, defStyleAttr) {

    companion object {
        const val ICON_SPAN = "[icon]"
    }


    private var iconSize = 12
    private var priceTextSize = 12
    private var priceTextBold = true
    private var unitTextSize = 12

    init {
        initAttr(context, attrs)
        isSingleLine = true
        ellipsize = TextUtils.TruncateAt.MARQUEE
        marqueeRepeatLimit = -1
        includeFontPadding = false
        setTextColor(getColorX(R.color.color_FF8E8E8E))
        setTextSize(TypedValue.COMPLEX_UNIT_PX, unitTextSize.toFloat())

        if (isInEditMode) {
            setPrice(5000f, "hour")
        }
    }

    private fun initAttr(context: Context, attrs: AttributeSet? = null) {
        context.withStyledAttributes(
            attrs,
            R.styleable.PriceView
        ) {
            iconSize = getDimensionPixelSize(
                R.styleable.PriceView_price_icon_size,
                12
            )
            priceTextSize = getDimensionPixelSize(
                R.styleable.PriceView_price_text_size,
                12
            )
            priceTextBold = getBoolean(
                R.styleable.PriceView_price_text_bold,
                priceTextBold
            )
            unitTextSize = getDimensionPixelSize(
                R.styleable.PriceView_unit_text_size,
                12
            )
        }
    }

    fun setPrice(price: Float, unit: String?) {
        val priceStr = if (isInEditMode) {
            price.toString()
        } else {
            formatNumberStr(price, false)
        }
        val unitStr = if (unit.isNullOrEmpty()) {
            ""
        } else {
            "/$unit"
        }
        val text = "$ICON_SPAN $priceStr$unitStr"

        val priceUnitText = SpannableStringBuilder(text).apply {
            //金币图片
            getDrawableX(R.drawable.common_wallet_coin_ic)?.let {
                findAndSetSpan(
                    CenterImageSpan(it.apply {
                        setBounds(0, 0, iconSize, iconSize)
                    }),
                    ICON_SPAN
                )
            }
            //价格字体
            findAndSetSpan(
                AbsoluteSizeSpan(priceTextSize),
                priceStr
            )
            findAndSetSpan(
                ForegroundColorSpan(getColorX(R.color.color_FF0D0D0D)),
                priceStr
            )
            if (priceTextBold) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    findAndSetSpan(
                        TypefaceSpan(DEFAULT_FONT_BOLD),
                        priceStr
                    )
                }
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    findAndSetSpan(
                        TypefaceSpan(DEFAULT_FONT_REGULAR),
                        priceStr
                    )
                }
            }
        }
        setText(priceUnitText)
    }


}