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

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import androidx.core.view.updateLayoutParams
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.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.widget.util.getColorX
import com.adealink.weparty.databinding.LayoutPriceViewBinding
import com.adealink.weparty.util.formatNumberStr


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

    companion object {
        private const val TEXT_GRAVITY_CENTER = 0
        private const val TEXT_GRAVITY_BOTTOM = 1
    }

    private val binding = LayoutPriceViewBinding.inflate(LayoutInflater.from(context), this)

    private var showIcon = true
    private var iconSize = 12
    private var priceTextSize = 12
    private var priceTextBold = true
    private var priceTextColor = getColorX(R.color.price_text_color)
    private var priceDiscountTextColor = getColorX(R.color.price_discount_text_color)
    private var unitTextSize = 12
    private var unitTextColor = getColorX(R.color.price_unit_text_color)
    private var priceTextGravity = TEXT_GRAVITY_CENTER

    init {
        if (isInEditMode) {
            binding.tvPrice.text = "1000"
            binding.tvUnit.text = "/h"
        }

        initAttr(context, attrs)
    }

    private fun initAttr(context: Context, attrs: AttributeSet? = null) {
        context.withStyledAttributes(
            attrs,
            R.styleable.PriceView
        ) {
            showIcon = getBoolean(
                R.styleable.PriceView_price_icon_show,
                true
            )
            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
            )
            priceTextColor = getColor(
                R.styleable.PriceView_price_text_color,
                priceTextColor
            )
            priceDiscountTextColor = getColor(
                R.styleable.PriceView_price_discount_text_color,
                priceDiscountTextColor
            )

            unitTextSize = getDimensionPixelSize(
                R.styleable.PriceView_unit_text_size,
                12
            )
            unitTextColor = getColor(
                R.styleable.PriceView_unit_text_color,
                unitTextColor
            )
            priceTextGravity = getInt(
                R.styleable.PriceView_price_text_gravity,
                TEXT_GRAVITY_CENTER
            )
        }

        if (priceTextGravity == TEXT_GRAVITY_BOTTOM) {
            binding.tvUnit.updateLayoutParams<LayoutParams> {
                baselineToBaseline = R.id.tv_price
                bottomToBottom = LayoutParams.PARENT_ID
            }
        } else {
            binding.tvUnit.updateLayoutParams<LayoutParams> {
                baselineToBaseline = LayoutParams.PARENT_ID
                bottomToBottom = LayoutParams.PARENT_ID
            }
        }

        if (showIcon) {
            binding.ivPrice.show()
            binding.ivPrice.updateLayoutParams<LayoutParams> {
                width = iconSize
                height = iconSize
            }
        } else {
            binding.ivPrice.gone()
        }
        if (priceTextBold) {
            binding.tvPrice.typeface = DEFAULT_FONT_BOLD
        } else {
            binding.tvPrice.typeface = DEFAULT_FONT_REGULAR
        }
        binding.tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_PX, priceTextSize.toFloat())
        binding.tvPrice.setTextColor(priceTextColor)
        binding.tvUnit.setTextSize(TypedValue.COMPLEX_UNIT_PX, unitTextSize.toFloat())
        binding.tvUnit.setTextColor(unitTextColor)
    }

    fun setPrice(price: Float, unit: String?) {
        binding.tvPrice.setTextColor(priceTextColor)
        setPriceInner(price, unit)
    }

    private fun setPriceInner(price: Float, unit: String?) {
        binding.tvPrice.text = formatNumberStr(price, false)
        if (unit.isNullOrEmpty()) {
            binding.tvUnit.text = null
        } else {
            binding.tvUnit.text = "/$unit"
        }
    }

    /**
     * 设置折扣价格
     */
    fun setDiscount(price: Float, unit: String?) {
        binding.tvPrice.setTextColor(priceDiscountTextColor)
        setPriceInner(price, unit)
    }


}