package fr.iutfbleau.desoxyiribonucleique;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.util.prefs.Preferences;

public class MainActivity extends Activity {

    private Button[] boutons;


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

        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String prefCouleurFond = prefs.getString("valeurs_fond", "#FF00CEC8");

        this.findViewById(R.id.main).setBackgroundColor(Color.parseColor(prefCouleurFond));



        Button[] boutons = {
                this.findViewById(R.id.a),
                this.findViewById(R.id.t),
                this.findViewById(R.id.c),
                this.findViewById(R.id.g)
        };

        EditText champ = this.findViewById(R.id.champ);

        for (Button bouton : boutons) {
            bouton.setOnClickListener(new ControleurBouton(champ));
        }

        this.boutons = boutons;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu_liste, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.acceder_parametres) {
            Intent i = new Intent(this, SettingsActivity.class);
            this.startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String couleur = prefs.getString("couleur_fond", "#FF00CEC8");
        this.findViewById(R.id.main).setBackgroundColor(Color.parseColor(couleur));

        Boolean[] param_boutons = {
                prefs.getBoolean("param_a", false),
                prefs.getBoolean("param_t", false),
                prefs.getBoolean("param_c", false),
                prefs.getBoolean("param_g", false),
        };

        for (int i = 0; i != this.boutons.length; i++) {
            if (param_boutons[i]) {
                this.boutons[i].setEnabled(false);
            } else {
                this.boutons[i].setEnabled(true);
            }
        }
    }
}