package com.iq.intelligence.study;

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.iq.intelligence.study.databinding.ItemLevelsBinding;

import java.util.List;

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

    private Context mContext;

    private List<LevelsBean> list;

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

    @NonNull
    @Override
    public LevelsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLevelsBinding levelsBinding = ItemLevelsBinding.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(PrescribedValue.LEVELS_NUMBER, i);
            Intent quiz = new Intent(mContext, GameMainActivity.class);
            quiz.putExtras(bundle);
            mContext.startActivity(quiz);
        });

        LevelsBean 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.64f);
        }



    }

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

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

    public static class LevelsViewHolder extends RecyclerView.ViewHolder {


        private ItemLevelsBinding itemLevelsBinding;

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