package com.braintest.brain.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class LevelsPage extends AppCompatActivity {


    SharedPreferences prefs;
    private RecyclerView recyclerView;
    private List<Data> list;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levels);
        BrainTestApp.setStatusBar(this,true);
        recyclerView = findViewById(R.id.levels_list);
        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        prefs = getSharedPreferences(ConsValues.PRE_FILE, Context.MODE_PRIVATE);


        initList();

    }

    private void initList() {
        SharedPreferences.Editor edit = prefs.edit();
        edit.putBoolean(String.format(getString(R.string.str_LevelValue), String.valueOf(1)), true);
        edit.apply();
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Data levelsBean;
            int levels = i + 1;
            String format = String.format(getString(R.string.str_LevelValue), String.valueOf(levels));
            boolean aBoolean = prefs.getBoolean(format, false);
            levelsBean = new Data(aBoolean, levels);
            list.add(levelsBean);
        }
        myAdapter = new MyAdapter(this, list);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

    }


    @Override
    protected void onStart() {
        super.onStart();

        for (Data bean : list) {
            int index = list.indexOf(bean);
            String format = String.format(getString(R.string.str_LevelValue), String.valueOf(bean.getLevels()));
            boolean aBoolean = prefs.getBoolean(format, false);
            if (aBoolean) {
                bean.setUnlock(aBoolean);
                myAdapter.updateUnlock(index);
            }
        }


    }
}