package com.cheng.blzb.ui.fragment.merchant.list

import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.MerchantGoodsEntity
import com.cheng.blzb.common.Constants
import com.example.base.ui.list.LoadMoreAdapter
import com.example.base.utils.DensityUtils

class MerchantGoodsAdapter: LoadMoreAdapter<MerchantGoodsEntity>(R.layout.listitem_merchant_goods) {
    override fun convert(holder: BaseViewHolder, item: MerchantGoodsEntity) {
        holder.getView<TextView>(R.id.tv_price).typeface = Constants.dDIN_PRO_M
        holder.getView<TextView>(R.id.tv_company_avatar).typeface = Constants.almmsht

        if (item.file.isNotEmpty()) {
            holder.getView<ImageView>(R.id.iv_cover).load(item.file[0].url){
                transformations(RoundedCornersTransformation(DensityUtils.dp2px(8f).toFloat()))
            }
        }
        holder.setText(R.id.tv_name, item.goods_name)
        holder.setText(R.id.tv_desc, item.goods_description)
        holder.setText(R.id.tv_price, item.goods_price_show)
        holder.setText(R.id.tv_view_count, "已浏览 ${if (!TextUtils.isEmpty(item.view_count)) item.view_count else 0}")

        holder.setGone(R.id.tv_company_avatar, true)
        holder.setGone(R.id.tv_company_name, true)
        holder.setGone(R.id.tv_location, true)
        if (!TextUtils.isEmpty(item.company.company_name)) {
            if (item.company.company_name!!.length >= 4) {
                holder.setText(R.id.tv_company_avatar, item.company.company_name.take(2) + "\n" + item.company.company_name.substring(2, 4))
            } else if (item.company.company_name.length == 3) {
                holder.setText(R.id.tv_company_avatar, item.company.company_name.take(2) + "\n" + item.company.company_name[2])
            } else {
                holder.setText(R.id.tv_company_avatar, item.company.company_name)
            }
            holder.setText(R.id.tv_company_name, item.company.company_name)
            holder.setVisible(R.id.tv_company_avatar, true)
            holder.setVisible(R.id.tv_company_name, true)
        }
        if (!TextUtils.isEmpty(item.company.city_name)) {
            holder.setText(R.id.tv_location, if (TextUtils.isEmpty(item.city_name)) "全国" else item.city_name)
            holder.setVisible(R.id.tv_location, true)
        }
    }
}