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.keyboard.journey.JourneyCategoryDetailsActivity
import com.keyboard.journey.JourneyCategoryDetailsActivity.Companion.CATEGORY_DETAILS_BEAN_KEY
import com.keyboard.journey.bean.MainDataBean
import com.keyboard.journey.databinding.MainRvItemBinding

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

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

        fun bind(mainData: MainDataBean) {
            binding.apply {
                titleTextView.text = mainData.title
                val adapter = CategoryItemDataAdapter(context, mainData.items!!)
                itemRv.layoutManager =
                    LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                itemRv.adapter = adapter

                moreData.setOnClickListener {
                    val intent = Intent(context, JourneyCategoryDetailsActivity::class.java)
                    intent.putExtra(CATEGORY_DETAILS_BEAN_KEY, mainData)
                    context.startActivity(intent)
                }
            }
        }
    }

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

    override fun onBindViewHolder(holder: MainDataViewHolder, position: Int) {
        val currentItem = mainDataList[position]
        holder.bind(currentItem)
    }

    override fun getItemCount() = mainDataList.size
}
