package com.keyboard.craft.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.keyboard.craft.activity.DetailsActivity
import com.keyboard.craft.bean.ItemDataBean
import com.keyboard.craft.databinding.AdapterCategoryDetailsItemBinding

class KeyDetailsDataAdapter(
    private val context: Context,
    private val list: List<ItemDataBean>,
    private val from: String
) :
    RecyclerView.Adapter<KeyDetailsDataAdapter.MainDataViewHolder>() {

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

    override fun onBindViewHolder(holder: MainDataViewHolder, position: Int) {
        val currentItem = list[position]
        holder.bind(currentItem)
        holder.itemView.setOnClickListener {
            val intent = Intent(context, DetailsActivity::class.java)
            intent.putExtra(DetailsActivity.KEY_CRAFT_DETAILS_BEAN, currentItem)
            intent.putExtra(DetailsActivity.KEY_CRAFT_FROM, from)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = list.size

    inner class MainDataViewHolder(private val binding: AdapterCategoryDetailsItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(data: ItemDataBean) {
            var url = data.thumbUrlGif
            if (url.isEmpty()) {
                url = data.thumbUrl
            }
            Glide.with(context)
                .load(url)
                .into(binding.imageView)
        }
    }
}
