package com.keyboard.theme.app.keyboard.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.keyboard.theme.app.keyboard.MyAppllication
import com.keyboard.theme.app.keyboard.R
import com.keyboard.theme.app.keyboard.api.OnHomeApi
import com.keyboard.theme.app.keyboard.data.InfoChild
import com.keyboard.theme.app.keyboard.databinding.ItemSingeClassBinding
import com.keyboard.theme.app.keyboard.utils.Common

class SingeClassAdapter(private var mContext: Context, private var data: List<InfoChild> = emptyList()) :
    RecyclerView.Adapter<SingeClassAdapter.ClassHolder>() {

    private lateinit var mListener: OnHomeApi
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClassHolder {
        val inflate = ItemSingeClassBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return ClassHolder(inflate)
    }
    fun setListener(listener: OnHomeApi) {
        mListener = listener

    }
    override fun onBindViewHolder(holder: ClassHolder, position: Int) {
        val infoChild = data[position]
        holder.viewbing.run {
            Glide.with(MyAppllication.instance).load(infoChild.preview)
                .transform(RoundedCorners(Common.dpToUnitPx(10f)))
                .placeholder(ContextCompat.getDrawable(MyAppllication.instance, R.mipmap.placholder))
                .into(preview)

            preview.setOnClickListener {
                mListener.onHomeClick(infoChild)
            }
        }

    }


    override fun getItemCount(): Int = data.size


    inner class ClassHolder(  var viewbing: ItemSingeClassBinding) :
        RecyclerView.ViewHolder(viewbing.root)
}