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

class CollectAdapter(var mContext: Context) :
    RecyclerView.Adapter<CollectAdapter.ResourceViewHolder>() {

    val collectBg =
        intArrayOf(R.mipmap.collect_bg1, R.mipmap.collect_bg2, R.mipmap.collect_bg3, R.mipmap.collect_bg4)
    private   var mData: List<NewBeanChild> = emptyList()

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

    inner class ResourceViewHolder(var adapterCollectBinding: AdapterCollectBinding) :
        RecyclerView.ViewHolder(adapterCollectBinding.root) {

    }

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

    }

    override fun getItemCount(): Int = mData.size

    override fun onBindViewHolder(holder: ResourceViewHolder, position: Int) {
        holder.adapterCollectBinding.rootView.background = ContextCompat.getDrawable(mContext,collectBg[position%4])

        mData[position].run {
            holder.adapterCollectBinding.textViewCollectName.text = title
            Glide.with(mContext).load(preUrl).placeholder(R.mipmap.placeholder).into(holder.adapterCollectBinding.collectIm)

            holder.adapterCollectBinding.rootView.setOnClickListener {
                mContext.startActivity(Intent(mContext, PlayActivity::class.java).apply {
                    putExtra(PlayActivity.INTENT_KEY_SOUND,this@run)
                })
            }
        }

    }
}