package com.bassbooster.soundeffects.equalizer.view.activity.setting.language.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bassbooster.equalizer.R
import com.bassbooster.soundeffects.equalizer.data.model.Nation
import com.bassbooster.soundeffects.equalizer.utils.SharedPreferencesUtil
class LanguageAdapter(
    private val nations: ArrayList<Nation>,
    val mListener: OnItemClickListener,
    var index : Int, var mContext : Context
) : RecyclerView.Adapter<LanguageAdapter.DataViewHolder>() {
    private var sharedPreferencesUtil =  SharedPreferencesUtil(mContext)
    var isSelect =  sharedPreferencesUtil.getInt("isSeletedIndex")

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): DataViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.item_language, parent, false)
        index = isSelect
        return DataViewHolder(view)
    }

    override fun onBindViewHolder(holder: DataViewHolder, position: Int) {
        if (isSelect == position) {
            holder.clBorder.setBackgroundResource(R.drawable.custom_image_nation_red)
            holder.imgVector.visibility = View.VISIBLE
            holder.imgVectorDefault.visibility = View.GONE
        } else {
            holder.clBorder.setBackgroundResource(R.drawable.custom_image_nation)
            holder.imgVector.visibility = View.GONE
            holder.imgVectorDefault.visibility = View.VISIBLE
        }
        holder.clBorder.setOnClickListener {
            isSelect = position
            mListener.onItemClick(position)
        }
        holder.tvName.text = nations[position].name
        holder.imgNation.setImageResource(nations[position].imgAvatar)

    }

    override fun getItemCount(): Int {
        return nations.size

    }

    class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvName: AppCompatTextView = itemView.findViewById(R.id.tvTitleLanguage)
        var imgNation: AppCompatImageView = itemView.findViewById(R.id.imgNation)
        var clBorder: ConstraintLayout = itemView.findViewById(R.id.clBorder)
        var imgVector: AppCompatImageView = itemView.findViewById(R.id.imgVector)
        var imgVectorDefault: AppCompatImageView = itemView.findViewById(R.id.imgVectorDefault)
    }
}
