package com.adealink.weparty.order.util

import android.os.Build
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.TypefaceSpan
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.weparty.commonui.DEFAULT_FONT_BOLD
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.order.R
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.R as APP_R


fun getOrderAllCost(
    price: Float,
    count: Int,
    totalCost: Float?,
    priceTextSize: Int,
    iconSize: Int,
    @StringRes allCostTextId: Int = R.string.order_all_total_cost
): SpannableStringBuilder {
    val cost = formatNumberStr(totalCost ?: (price * count), omitted = false)
    val text = getCompatString(allCostTextId, cost)
    return SpannableStringBuilder(text).apply {
        findAndSetSpan(
            CenterImageSpan(getCompatDrawable(APP_R.drawable.common_wallet_coin_ic).apply {
                setBounds(0, 0, iconSize, iconSize)
            }),
            "[currency]"
        )
        findAndSetSpan(
            AbsoluteSizeSpan(priceTextSize),
            cost
        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            findAndSetSpan(
                TypefaceSpan(DEFAULT_FONT_BOLD),
                cost
            )
        }
    }
}