package com.example.liste;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.app.Activity;

public class MainActivity extends Activity {
    private Fleche figureView;
    private int selectedShape = Fleche.GAUCHE;

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

        figureView = findViewById(R.id.figureView);
        Spinner shapeSpinner = findViewById(R.id.shapeSpinner);

        // Créer un adaptateur pour la liste déroulante
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.shape_options, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shapeSpinner.setAdapter(adapter);

        // Restaurer l'état après rotation
        if (savedInstanceState != null) {
            selectedShape = savedInstanceState.getInt("selectedShape", Fleche.GAUCHE);
        }
        updateFigure(selectedShape);

        // Gestion des changements de sélection dans la liste déroulante
        shapeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedShape = (position == 0) ? Fleche.GAUCHE : Fleche.DROITE;
                updateFigure(selectedShape);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("selectedShape", selectedShape);
    }

    private void updateFigure(int shape) {
        figureView.setShape(shape);
    }
}
