package com.various.funny.sounds.audio.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.various.funny.sounds.audio.R
import com.various.funny.sounds.audio.data.NewBean
import com.various.funny.sounds.audio.databinding.AdapterCategoryBinding
import com.various.funny.sounds.audio.ui.activity.SourceActivity

class CategoryAdapter(var mContext: Context) :
    RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder>() {

    val categoryBg =
        intArrayOf(R.mipmap.item_bg1, R.mipmap.item_bg2, R.mipmap.item_bg3, R.mipmap.item_bg4)
    private   var mData: List<NewBean> = emptyList()

    fun updateData(data: List<NewBean>) {
        mData = data
        notifyDataSetChanged()
    }

    inner class CategoryViewHolder(var categoryItemViewBing: AdapterCategoryBinding) :
        RecyclerView.ViewHolder(categoryItemViewBing.root) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        return CategoryViewHolder(
            AdapterCategoryBinding.inflate(
                LayoutInflater.from(mContext),
                parent,
                false
            )
        )

    }

    override fun getItemCount(): Int = mData.size

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.categoryItemViewBing.rootView.background =
            ContextCompat.getDrawable(mContext, categoryBg[position % 4])
        mData[position].run {
            holder.categoryItemViewBing.textViewCategoryName.text = categoryName
            Glide.with(mContext).load(categoryUrl).placeholder(R.mipmap.placeholder).into(holder.categoryItemViewBing.categoryIm)

            holder.categoryItemViewBing.rootView.setOnClickListener {
                mContext.startActivity(Intent(mContext, SourceActivity::class.java).apply {
                    putExtra(SourceActivity.INTENT_KEY_ID,categoryId)
                })
            }
        }



    }
}