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.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.keyboard.journey.JourneyCategoryDetailsActivity
import com.keyboard.journey.JourneyCategoryDetailsActivity.Companion.CATEGORY_DETAILS_BEAN_KEY
import com.keyboard.journey.JourneyDetailsActivity
import com.keyboard.journey.bean.CategoryDataBean
import com.keyboard.journey.bean.ItemDataBean
import com.keyboard.journey.bean.MainDataBean
import com.keyboard.journey.databinding.AdapterCategoryDetailsItemBinding
import com.keyboard.journey.databinding.MainRvItemBinding

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

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

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

    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 = mainDataList[position]
        holder.bind(currentItem)
        holder.itemView.setOnClickListener {
            val intent = Intent(context, JourneyDetailsActivity::class.java)
            intent.putExtra(JourneyDetailsActivity.KEY_JOURNEY_DETAILS_BEAN,currentItem)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = mainDataList.size
}
