package com.friendvilla.bucksbuddy.adapter;

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.SpinActivity;
import com.friendvilla.bucksbuddy.R;
import com.friendvilla.bucksbuddy.SlotActivity;

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

    private int selected;
    private boolean tick;
    private final int type;
    private final int[] betList;
    private final int[] iconLst = new int[]{R.drawable.apple,R.drawable.pineapple,R.drawable.banana,R.drawable.grape,R.drawable.guava,
            R.drawable.lychee,R.drawable.orange,R.drawable.dates,R.drawable.watermelon,R.drawable.coconut,R.drawable.cherry,
            R.drawable.peach,R.drawable.strawberry,R.drawable.mango};
    public BetAdapter(boolean select,int type) {
        this.tick = select;
        this.type = type;
        this.betList = new int[]{1,2,3,4,5,10};
    }

    @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;
        if(type == 1) {
            cateView.title.setText("X " + betList[position]);
            if (tick && selected == position) {
                cateView.icon.setVisibility(View.VISIBLE);
            } else {
                cateView.icon.setVisibility(View.GONE);
            }
            cateView.itemView.setOnClickListener(v -> {
                tick = true;
                selected = position;
                SpinActivity.Number = betList[position];
                notifyDataSetChanged();
            });
        } else if(type == 2) {
            cateView.title.setText("");
            cateView.title.setBackgroundResource(iconLst[position]);
            cateView.title.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
            if (tick && selected == position) {
                cateView.icon.setVisibility(View.VISIBLE);
            } else {
                cateView.icon.setVisibility(View.GONE);
            }
            cateView.itemView.setOnClickListener(v -> {
                tick = true;
                selected = position;
                SlotActivity.Nmbr = iconLst[position];
                notifyDataSetChanged();
            });
        }
    }

    @Override
    public int getItemCount() {
        if(type == 1) {
            return betList.length;
        } else if(type == 2) {
            return iconLst.length;
        } else {
            return 0;
        }
    }

    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);
        }
    }
}
