package fr.iutfbleau.dev41.wamster.tp06ex01_desoxyribonucleique;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private Button[] listButton;
    private EditText output;

    public static final int BOUTON_A = 0;
    public static final int BOUTON_C = 1;
    public static final int BOUTON_G = 2;
    public static final int BOUTON_T = 3;

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

        //ligne de fou
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        this.listButton = new Button[4];
        this.listButton[MainActivity.BOUTON_A] = findViewById(R.id.buttonA);
        this.listButton[MainActivity.BOUTON_C] = findViewById(R.id.buttonC);
        this.listButton[MainActivity.BOUTON_G] = findViewById(R.id.buttonG);
        this.listButton[MainActivity.BOUTON_T] = findViewById(R.id.buttonT);
        this.output = findViewById(R.id.output);

        EventButton eventButton = new EventButton(this.listButton, this.output);
        for (Button button : this.listButton) {
            button.setOnClickListener(eventButton);
        }

        SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
        this.listButton[MainActivity.BOUTON_A].setEnabled(preference.getBoolean("buttonA", true));
        this.listButton[MainActivity.BOUTON_C].setEnabled(preference.getBoolean("buttonC", true));
        this.listButton[MainActivity.BOUTON_G].setEnabled(preference.getBoolean("buttonG", true));
        this.listButton[MainActivity.BOUTON_T].setEnabled(preference.getBoolean("buttonT", true));
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();
        if (id == R.id.test){
            Intent intention = new Intent(this, SettingsActivity.class);
            intention.putExtra("idBtnA", this.listButton[MainActivity.BOUTON_A].getId());
            intention.putExtra("idBtnC", this.listButton[MainActivity.BOUTON_C].getId());
            intention.putExtra("idBtnG", this.listButton[MainActivity.BOUTON_G].getId());
            intention.putExtra("idBtnT", this.listButton[MainActivity.BOUTON_T].getId());
            this.startActivity(intention);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}