package com.adealink.weparty.order.util

import android.graphics.Typeface
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.TypefaceSpan
import androidx.appcompat.widget.AppCompatTextView
import com.adealink.frame.aab.util.getCompatColor
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.ext.sp
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.module.order.data.OrderStatus
import com.adealink.weparty.order.R
import com.adealink.weparty.R as APP_R

fun setUserOrderStatusText(
    textView: AppCompatTextView,
    status: OrderStatus?,
    isRefundApply: Boolean? //是否处于申请退款中
) {
    if (isRefundApply == true) {
        textView.text =
            getCompatString(R.string.user_order_status_refund_under_review_desc)
        textView.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
        return
    }
    when (status) {
        OrderStatus.CREATE_ORDER -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
        }

        OrderStatus.WAIT_FOR_ACCEPT -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FF3FBFBD))
        }

        OrderStatus.PLAYMATE_ACCEPT -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
        }

        OrderStatus.PLAYMATE_IN_SERVICE -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
        }

        OrderStatus.PLAYMATE_END_SERVICE -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
        }

        OrderStatus.PLAYMATE_REFUSE -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
        }

        OrderStatus.COMPLETE -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FF3FBFBD))
        }

        OrderStatus.USER_REFUND -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
        }

        OrderStatus.USER_CANCEL -> {
            textView.text =
                getCompatString(R.string.user_order_status_create_order_desc)
            textView.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
        }

        null -> {
            textView.text = null
        }
    }
}


fun getOrderAllCost(price: Float, count: Int): SpannableStringBuilder {
    val cost = (price * count).toString()
    val text = getCompatString(R.string.order_all_cost, cost)
    return SpannableStringBuilder(text).apply {
        findAndSetSpan(
            CenterImageSpan(getCompatDrawable(APP_R.drawable.common_wallet_coin_32_ic)),
            "[currency]"
        )
        findAndSetSpan(
            AbsoluteSizeSpan(16.sp()),
            cost
        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            findAndSetSpan(
                TypefaceSpan(Typeface.create(DEFAULT_FONT_BOLD, Typeface.BOLD)),
                cost
            )
        }
    }
}