package kr.co.vividnext.sodalive.live.recommend

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.FrameLayout
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import kr.co.vividnext.sodalive.R

class RecommendLiveAdapter(
    private val context: Context,
    private val itemWidth: Int,
    private val itemHeight: Int,
    private val onClick: (Long) -> Unit
) : BaseBannerAdapter<GetRecommendLiveResponse>() {
    override fun bindData(
        holder: BaseViewHolder<GetRecommendLiveResponse>,
        data: GetRecommendLiveResponse,
        position: Int,
        pageSize: Int
    ) {
        val ivRecommendLive = holder.findViewById<ImageView>(R.id.iv_recommend_live)
        val layoutParams = ivRecommendLive.layoutParams as FrameLayout.LayoutParams

        layoutParams.width = itemWidth
        layoutParams.height = itemHeight

        Glide
            .with(context)
            .asBitmap()
            .load(data.imageUrl)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    ivRecommendLive.setImageBitmap(resource)
                    ivRecommendLive.layoutParams = layoutParams
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })

        ivRecommendLive.setOnClickListener { onClick(data.creatorId) }
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_recommend_live
    }
}
