package com.keyboard.journey.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.journey.JourneyDetailsActivity
import com.keyboard.journey.JourneyDetailsActivity.Companion.KEY_JOURNEY_DETAILS_BEAN
import com.keyboard.journey.LogUtil
import com.keyboard.journey.bean.ItemDataBean
import com.keyboard.journey.databinding.HorizontalScrollingRvItemBinding

class CategoryItemDataAdapter(
    private val context: Context,
    private val mainDataList: List<ItemDataBean>
) :
    RecyclerView.Adapter<CategoryItemDataAdapter.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 {
            val intent = Intent(context, JourneyDetailsActivity::class.java)
            intent.putExtra(KEY_JOURNEY_DETAILS_BEAN,currentItem)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = mainDataList.size
}
