package com.remax.visualnovel.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.annotation.StringRes
import androidx.core.content.withStyledAttributes
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.WidgetPriceViewBinding
import com.remax.visualnovel.extension.setMargin
import com.remax.visualnovel.extension.setSize
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeTextFont
import com.dylanc.viewbinding.nonreflection.inflate

/**
 * Created by HJW on 2023/8/2
 */
@SuppressLint("SetTextI18n")
class PriceView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr) {

    private var binding: WidgetPriceViewBinding? = null

    init {
        binding = inflate(WidgetPriceViewBinding::inflate)
        context.withStyledAttributes(attrs, R.styleable.PriceView) {
            val iconSize = getDimensionPixelOffset(R.styleable.PriceView_priceIconSize, 16.dp)
            val priceIconPadding = getDimensionPixelOffset(R.styleable.PriceView_priceIconPadding, 4.dp)

            binding?.priceIcon?.run {
                setMargin(marginEnd = priceIconPadding)
                setSize(iconSize, iconSize)
            }


            getString(R.styleable.PriceView_priceTextToken)?.let { txtToken ->
                binding?.priceTv?.changeTextFont {
                    textUITextToken = txtToken
                }
            }

            val content = getString(R.styleable.PriceView_priceText)
            binding?.priceTv?.text = content
        }
    }

    fun getContentView() = binding?.priceTv

    fun setSizeType(@StringRes txtToken: Int, iconSize: Int) {
        binding?.run {
            priceTv.changeTextFont {
                textUITextToken = context.getString(txtToken)
            }
            priceIcon.setSize(iconSize, iconSize)
        }
    }

    fun setPrice(content: String?) {
        binding?.priceTv?.text = content
    }
}