package com.adpro.pixelshow.event

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adpro.pixelshow.R
import com.adpro.pixelshow.data.Movie
import com.adpro.pixelshow.databinding.ItemClassFillingBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions

class ClassFillingAdapter : RecyclerView.Adapter<ClassFillingAdapter.MainInViewHolder>() {
    private lateinit var context: Context
    private var coverList: List<Movie> = mutableListOf()
    private lateinit var listener: SecFavListener

    class MainInViewHolder(val binding: ItemClassFillingBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainInViewHolder {
        context = parent.context
        val binding = ItemClassFillingBinding.inflate(LayoutInflater.from(parent.context), parent, false);
        return MainInViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return coverList.size
    }

    override fun onBindViewHolder(holder: MainInViewHolder, position: Int) {
        val previewUrl400 = coverList[position].previewUrl400
        val previewUrl200 = coverList[position].previewUrl200
        Log.d("MainInAdapter", "previewUrl400 is $previewUrl400 previewUrl200 is $previewUrl200")
        Glide.with(context).load(previewUrl400).placeholder(R.mipmap.bg_placeholder)
            .error(R.mipmap.bg_placeholder).thumbnail(
                Glide.with(context)
                    .asDrawable()
                    .load(previewUrl200)
                    .apply(
                        RequestOptions()
                            .format(DecodeFormat.PREFER_RGB_565)
                    )
                    .placeholder(R.mipmap.bg_placeholder)
                    .centerCrop()
            ).into(holder.binding.inImage)
        holder.binding.inImage.setOnClickListener {
            listener.inItemClick(coverList[position])
        }
    }

    fun setListener(listener: SecFavListener) {
        this.listener = listener
    }

    fun setCoverList(list: List<Movie>) {
        this.coverList = list
    }
}