package com.yidianling.common.binding

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.PaintDrawable
import android.graphics.drawable.shapes.RectShape
import android.graphics.drawable.shapes.RoundRectShape
import android.view.View
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.ydl.ydl_image.module.GlideApp

@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
    if (url.isNullOrEmpty()) return
    GlideApp.with(imageView).load(url).into(imageView)
}


@BindingAdapter(
    value = [
        "shapeBg",
        "shapeStrokeColor",
        "shapeStrokeWidth",
        "shapeRadius",
        "shape"
    ],
    requireAll = false
)
fun View.setCustomStyle(
    shapeBg: Int = Color.TRANSPARENT,                  // 背景颜色
    shapeStrokeColor: Int = Color.TRANSPARENT,                 // 边框颜色
    shapeStrokeWidth: Float = 0f,                              // 边框宽度
    shapeRadius: Float = 0f,                                    // 圆角宽度
    shape: Int = GradientDrawable.RECTANGLE               // Shape样式 默认矩形
) {
    val gradientDrawable: GradientDrawable = if (background as? GradientDrawable == null) {
        GradientDrawable()
    } else {
        background as GradientDrawable
    }
    gradientDrawable.setColor(shapeBg)
    gradientDrawable.setStroke(
        shapeStrokeWidth.toInt(),
        shapeStrokeColor
    )
    val radius = resources.displayMetrics.density * shapeRadius
    gradientDrawable.cornerRadii = floatArrayOf(
        radius,
        radius,
        radius,
        radius,
        radius,
        radius,
        radius,
        radius
    )
    when (shape) {
        GradientDrawable.RECTANGLE -> gradientDrawable.shape = GradientDrawable.RECTANGLE
        GradientDrawable.OVAL -> gradientDrawable.shape = GradientDrawable.OVAL
        GradientDrawable.LINE -> gradientDrawable.shape = GradientDrawable.LINE
        GradientDrawable.RING -> gradientDrawable.shape = GradientDrawable.RING
        else -> gradientDrawable.shape = GradientDrawable.RECTANGLE
    }
    background = gradientDrawable
}