package com.bassbooster.soundeffects.equalizer.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bassbooster.equalizer.R
import com.bassbooster.soundeffects.equalizer.data.model.VisualizerModel
import com.bassbooster.soundeffects.equalizer.view.fragment.PlayFragment

class VisualizerAdapter(val context: Context, val listImage: ArrayList<VisualizerModel>, val mListener: OnItemClickListener) : RecyclerView.Adapter<VisualizerAdapter.VisualizerViewHolder>() {
    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VisualizerViewHolder {
        val inflater = LayoutInflater.from(parent.getContext())
        val view = inflater.inflate(R.layout.adapter_visualizer, parent, false)
        return VisualizerViewHolder(view)
    }

    override fun onBindViewHolder(holder: VisualizerViewHolder, position: Int) {
        if (PlayFragment.positionType == position){
            holder.isSelected.visibility = View.VISIBLE
        }else{
            holder.isSelected.visibility = View.GONE
        }
        holder.mImgVisual.setImageDrawable(listImage[position].image)
        holder.mRlView.setOnClickListener {
            PlayFragment.positionType = position
            mListener.onItemClick(position)
        }
    }

    override fun getItemCount(): Int {
        return listImage.size
    }

    class VisualizerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var mRlView : CardView = view.findViewById(R.id.mRlView)
        var isSelected : ConstraintLayout = view.findViewById(R.id.isSelectView)
        var mImgVisual : ImageView = view.findViewById(R.id.mImgVisual)
    }

}
