package com.yidian.shenghuoquan.buserorder.viewmodel

import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.yidian.bcommon.mvvm.BaseViewModel
import com.yidian.shenghuoquan.buserorder.R
import com.yidian.utils.DensityUtil

/**
 * author: yinjiacheng
 * date: 2021/8/23 16:01
 * description: 订单详情商品列表
 */
class OrderDetailCommodityListItemViewModel : BaseViewModel() {

  companion object {
    /**
     * 加载商品图片
     * @param url 图片url
     * @param target 目标ImageView
     */
    @BindingAdapter("commodityImageUrl")
    @JvmStatic
    fun loadCommodityImage(target: ImageView, url: String) {
      if (url.isBlank()) return
      Glide.with(target.context).load(url)
        .apply(
          RequestOptions()
            .transform(CenterCrop(), RoundedCorners(DensityUtil.dp2px(12F)))
            .error(R.drawable.img_order_commodity_placeholder)
            .placeholder(R.drawable.img_order_commodity_placeholder)
        )
        .into(target)
    }

    /**
     * 设置左外边距
     * @param margin 外边距 单位dp
     */
    @JvmStatic
    @BindingAdapter("layout_marginStart")
    fun setMarginStart(view: View, margin: Int) {
      val lp = view.layoutParams as ConstraintLayout.LayoutParams
      lp.marginStart = DensityUtil.dp2px(margin.toFloat())
      view.layoutParams = lp
    }
  }
}
