package com.braintest.brain.test;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.braintest.brain.test.databinding.ItemLayoutBinding;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.LevelsViewHolder> {

    private Context mContext;

    private List<Data> list;

    public MyAdapter(Context context, List<Data> list) {
        mContext = context;
        this.list = list;
    }

    @NonNull
    @Override
    public LevelsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLayoutBinding levelsBinding = ItemLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), null, false);
        return new LevelsViewHolder(levelsBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull LevelsViewHolder holder, int position) {
        int i = position + 1;
        holder.itemLevelsBinding.levelsBtn.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putInt(ConsValues.LEVELS_NUMBER, i);
            Intent quiz = new Intent(mContext, InfoActivity.class);
            quiz.putExtras(bundle);
            mContext.startActivity(quiz);
        });

        Data levelsBean = list.get(position);
        holder.itemLevelsBinding.levelsBtn.setText(String.valueOf(levelsBean.getLevels()));
        if (levelsBean.isUnlock()) {
            holder.itemLevelsBinding.levelsBtn.setClickable(true);
            holder.itemLevelsBinding.levelsBtn.setAlpha(1f);
        } else {
            holder.itemLevelsBinding.levelsBtn.setClickable(false);
            holder.itemLevelsBinding.levelsBtn.setAlpha(0.44f);
        }



    }

    public void updateUnlock(int pos) {
        notifyItemChanged(pos);
    }

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

    public static class LevelsViewHolder extends RecyclerView.ViewHolder {


        private ItemLayoutBinding itemLevelsBinding;

        public LevelsViewHolder(@NonNull ItemLayoutBinding itemView) {
            super(itemView.getRoot());
            itemLevelsBinding = itemView;
        }
    }
}
