package com.example.quiz.data.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.quiz.R;
import com.example.quiz.data.model.TypeOfBet;

import java.util.List;

public class TypeOfBetAdapter extends RecyclerView.Adapter<TypeOfBetAdapter.TypeOfBetViewHolder> {
    private List<TypeOfBet> types;
    private onItemClickListener listener;

    private int selectedPosition = -1;

    public interface  onItemClickListener{
        void onItemClick(TypeOfBet type);
    }

    public void setOnItemClickListener(TypeOfBetAdapter.onItemClickListener listener){
        this.listener = listener;
    }

    public TypeOfBetAdapter(List<TypeOfBet> types){
        this.types = types;
    }

    public void setTypes(List<TypeOfBet> types){
        this.types = types;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TypeOfBetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_of_bet_item, parent, false);
        return new TypeOfBetViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull TypeOfBetViewHolder holder, int position) {
        TypeOfBet type = types.get(position);
        holder.type_of_bet_name.setText(type.getLabel());
        if(selectedPosition != position){
            holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg);
        }else{
            holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg_selected);
        }
        holder.itemView.setOnClickListener(v->{
            if(listener != null){
                listener.onItemClick(type);
            }
            int previousPosition = selectedPosition;
            selectedPosition = holder.getAdapterPosition();
            notifyItemChanged(previousPosition);
            notifyItemChanged(selectedPosition);
        });
    }

    @Override
    public int getItemCount() {
        return types.size();
    }

    public class TypeOfBetViewHolder extends RecyclerView.ViewHolder {
        TextView type_of_bet_name;

        public TypeOfBetViewHolder(@NonNull View itemView) {
            super(itemView);
            type_of_bet_name = itemView.findViewById(R.id.type_of_bet_name);
        }
    }
}
