package kr.co.vividnext.sodalive.audio_content.main.banner

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
import kr.co.vividnext.sodalive.audio_content.main.GetAudioContentBannerResponse

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

        layoutParams.width = itemWidth
        layoutParams.height = itemHeight

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

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

        ivBanner.setOnClickListener { onClick(data) }
    }

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