package com.hongyegroup.cpt_parttime.adapter

import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.guadou.cs_cptservices.binding.BaseMultiDataBindingAdapter
import com.guadou.lib_baselib.engine.extLoad
import com.guadou.lib_baselib.ext.dp2px
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.bean.PartJobResult

/**
 * 多布局+DataBinding
 * 兼职工作首页的列表适配器
 */
class PartTimeJobAdapter(br: Int, list: MutableList<PartJobResult>) :
    BaseMultiDataBindingAdapter<PartJobResult>(br, list) {

    init {
        // 绑定 layout 对应的 type
        addItemType(PartJobResult.TYPE_EMPTY, R.layout.item_empty_layout)
        addItemType(PartJobResult.TYPE_ITEM, R.layout.item_parttime_job_view)
        addItemType(PartJobResult.TYPE_BANNER, R.layout.item_parttime_banner)
    }

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        if (viewType != PartJobResult.TYPE_EMPTY) {
            // 绑定databinding
            DataBindingUtil.bind<ViewDataBinding>(viewHolder.itemView)
        }
    }

    override fun convert(holder: BaseViewHolder, item: PartJobResult) {
        if (holder.itemViewType != PartJobResult.TYPE_EMPTY) {
            // 绑定databinding
            holder.getBinding<ViewDataBinding>()?.run {
                setVariable(_br, item)
                executePendingBindings()
            }
        }

        //不方便做DataBinding的操作 ↓
        if (holder.itemViewType == PartJobResult.TYPE_ITEM) {


        } else if (holder.itemViewType == PartJobResult.TYPE_BANNER) {

            val imageUrl = "http://i03piccdn.sogoucdn.com/dbbea331b73acd7d"
            holder.getView<ImageView>(R.id.iv_banner).extLoad(imageUrl, roundRadius = dp2px(8f))
        }

    }

}