package com.example.arc_en_ciel;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.app.Activity;
import java.util.Arrays;
import java.util.List;

public class ColorSelectionActivity extends Activity {
    private List<String> colorNames = Arrays.asList("Rouge", "Orange", "Jaune", "Vert", "Bleu", "Indigo", "Violet");
    private CheckBox[] checkBoxes;
    private static final String PREFS_NAME = "ColorPrefs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_selection);

        LinearLayout layout = findViewById(R.id.colorList);
        checkBoxes = new CheckBox[colorNames.size()];

        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

        for (int i = 0; i < colorNames.size(); i++) {
            checkBoxes[i] = new CheckBox(this);
            checkBoxes[i].setText(colorNames.get(i));
            checkBoxes[i].setChecked(prefs.getBoolean("color_" + i, true)); // Toutes cochées par défaut
            layout.addView(checkBoxes[i]);
        }

        Button okButton = findViewById(R.id.okButton);
        okButton.setOnClickListener(v -> {
            savePreferences();
            finish(); // Retourner à MainActivity
        });
    }

    private void savePreferences() {
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        for (int i = 0; i < checkBoxes.length; i++) {
            editor.putBoolean("color_" + i, checkBoxes[i].isChecked());
        }

        editor.apply();
    }
}
