package com.bassbooster.soundeffects.equalizer.view.activity.edgelighting.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bassbooster.equalizer.R
import com.bassbooster.equalizer.databinding.ItemColorBorderBinding
import com.bassbooster.soundeffects.equalizer.data.model.ColorEdgeModel

import com.bassbooster.soundeffects.equalizer.utils.SharedPreferencesUtil

class ColorBorderAdapter(val context: Context, val list: ArrayList<ColorEdgeModel>, val mListener: OnItemClickListener) : RecyclerView.Adapter<ColorBorderAdapter.ViewHolder>() {
    var isSelect = -1
    private var sharedPreferencesUtil = SharedPreferencesUtil(context)
    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (isSelect == position){
            holder.binding.mLlColor.background = ContextCompat.getDrawable(context, R.drawable.custome_border_rectangle_select)
        }else{
            holder.binding.mLlColor.background = ContextCompat.getDrawable(context,R.drawable.custome_border_rectangle)
        }
        if (sharedPreferencesUtil.getInt("positionList") == position) {
            holder.binding.mLlColor.background = ContextCompat.getDrawable(context,R.drawable.custome_border_rectangle_select)
        }
        holder.binding.mImgColor.background = ContextCompat.getDrawable(context,list[position].image)
        holder.binding.mLlColor.setOnClickListener {
            isSelect = position
            mListener.onItemClick(position)
        }
    }

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

    inner class ViewHolder(val binding: ItemColorBorderBinding) : RecyclerView.ViewHolder(binding.root)

}