package com.keyboard.craft.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
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.activity.DetailsActivity.Companion.KEY_CRAFT_DETAILS_BEAN
import com.keyboard.craft.util.LogUtil
import com.keyboard.craft.bean.ItemDataBean
import com.keyboard.craft.databinding.HorizontalScrollingRvItemBinding

class KeyItemDataAdapter(
    private val context: Context,
    private val mainDataList: List<ItemDataBean>
) :
    RecyclerView.Adapter<KeyItemDataAdapter.MainDataViewHolder>() {

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

        fun bind(bean: ItemDataBean) {
            binding.apply {
                var url = bean.thumbUrlGif
                if (url.isEmpty()) {
                    url = bean.thumbUrl
                }
                LogUtil.logMsgD("url->${url}")
                Glide.with(context)
                    .load(url)
                    .into(hsRvImg)
            }
        }
    }

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

    override fun onBindViewHolder(holder: MainDataViewHolder, position: Int) {
        val currentItem = mainDataList[position]
        holder.bind(currentItem)
        holder.itemView.setOnClickListener {
            Log.d("ocean","currentItem ->$currentItem")
            val intent = Intent(context, DetailsActivity::class.java)
            intent.putExtra(KEY_CRAFT_DETAILS_BEAN,currentItem)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = mainDataList.size
}
