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

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import com.adealink.weparty.R
import com.adealink.weparty.databinding.LayoutPriceViewBinding

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

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

    private var priceTextSize = 12f
    private var unitTextSize = 12f

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

        initAttr(context)
    }

    private fun initAttr(context: Context, attrs: AttributeSet? = null) {
        if (isInEditMode) {
            return
        }
        context.withStyledAttributes(
            attrs,
            R.styleable.PriceView
        ) {
            priceTextSize = getFloat(
                R.styleable.PriceView_price_text_size,
                12f
            )
            unitTextSize = getFloat(
                R.styleable.PriceView_unit_text_size,
                12f
            )
        }

        if (priceTextSize > 12) {
            binding.ivPrice.setImageResource(R.drawable.common_wallet_coin_64_ic)
        } else {
            binding.ivPrice.setImageResource(R.drawable.common_wallet_coin_32_ic)
        }
        binding.tvPrice.textSize = priceTextSize
        binding.tvUnit.textSize = unitTextSize
    }

    fun setPrice(price: Float, unit: String) {
        binding.tvPrice.text = price.toInt().toString()
        binding.tvUnit.text = "/$unit"
    }


}