package com.friendvilla.bucksbuddy.adapter;

import static com.friendvilla.bucksbuddy.NumberActivity.AmountNumber;
import static com.friendvilla.bucksbuddy.ScratchActivity.AmountCard;
import static com.friendvilla.bucksbuddy.SlotActivity.Amnt;
import static com.friendvilla.bucksbuddy.SpinActivity.Amount;
import static com.friendvilla.bucksbuddy.TictacActivity.AmountTicTacToe;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.friendvilla.bucksbuddy.R;

public class AmountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private int selected;
    private boolean tick;
    private final int type;
    public AmountAdapter(boolean select,int type) {
        this.tick = select;
        this.type = type;
    }

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

    @SuppressLint({"SetTextI18n", "NotifyDataSetChanged", "RecyclerView"})
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CateView cateView = (CateView) holder;
        int amount = position + 1;
        cateView.title.setText("" + amount);
        if(tick && selected == position) {
            cateView.icon.setVisibility(View.VISIBLE);
        } else {
            cateView.icon.setVisibility(View.GONE);
        }
        cateView.itemView.setOnClickListener(v -> {
            tick = true;
            selected = position;
            if(type == 1) {
                Amount = amount;
            } else if(type == 2) {
                Amnt = amount;
            } else if(type == 3) {
                AmountTicTacToe.setValue(amount);
            } else if(type == 4) {
                AmountCard.setValue(amount);
            } else if(type == 5) {
                AmountNumber.setValue(amount);
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    private static class CateView extends RecyclerView.ViewHolder {
        private final ImageView icon;
        private final TextView title;
        public CateView(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.txt);
            icon = itemView.findViewById(R.id.tick);
        }
    }
}
