package com.photo.keyboard.themes.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.photo.keyboard.themes.App
import com.photo.keyboard.themes.R
import com.photo.keyboard.themes.data.ThemeData
import com.photo.keyboard.themes.databinding.ItemClassBinding
import com.photo.keyboard.themes.listener.MainListener
import com.photo.keyboard.themes.manager.Utils

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

    private lateinit var mListener: MainListener


    fun updateData(newData: List<ThemeData>) {
        data = newData
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClassHolder {
        val inflate = ItemClassBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return ClassHolder(inflate)
    }

    fun setListener(listener: MainListener) {
        mListener = listener

    }

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

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

    }


    override fun getItemCount(): Int = data.size


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