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.NewBeanChild
import com.various.funny.sounds.audio.databinding.AdapterSourceBinding
import com.various.funny.sounds.audio.ui.activity.PlayActivity

class SourceAdapter(var mContext: Context) :
    RecyclerView.Adapter<SourceAdapter.SourceViewHolder>() {

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

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

    inner class SourceViewHolder(var adapterSourceBinding: AdapterSourceBinding) :
        RecyclerView.ViewHolder(adapterSourceBinding.root) {

    }

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

    }

    override fun getItemCount(): Int = mData.size

    override fun onBindViewHolder(holder: SourceViewHolder, position: Int) {
        holder.adapterSourceBinding.rootView.background = ContextCompat.getDrawable(mContext,categoryBg[position%4])
        mData[position].run {
            holder.adapterSourceBinding.textViewName.text = title
            Glide.with(mContext).load(preUrl).placeholder(R.mipmap.placeholder).into(holder.adapterSourceBinding.sourceIm)
            holder.adapterSourceBinding.rootView.setOnClickListener {
                mContext.startActivity(Intent(mContext,PlayActivity::class.java).apply {
                    putExtra(PlayActivity.INTENT_KEY_SOUND,this@run)
                })
            }
        }

    }
}