package com.example.patrice.preferences;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
{

    TextView textView;

    public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
    public static final String PREF_MIN_MAG = "PREF_MIN_MAG";
    public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";
    private static final int SHOW_PREFERENCES = 1;
    private static final int SHOW_PREFERENCES2 = 2;

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

        textView = (TextView) findViewById(R.id.textView);

        displaySharedPreferences();
    }


    private void displaySharedPreferences()
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
/*
        String username = prefs.getString("username", "Default NickName");
        String passw = prefs.getString("password", "Default Password");
        boolean checkBox = prefs.getBoolean("checkBox", false);
        String listPrefs = prefs.getString("listpref", "Default list prefs");
*/

        StringBuilder builder = new StringBuilder();
    //    builder.append("Username: " + username + "\n");
    //    builder.append("Password: " + passw + "\n");
    //    builder.append("Keep me logged in: " + String.valueOf(checkBox) + "\n");
    //    builder.append("List preference: " + listPrefs);
    //    textView.setText(builder.toString());

        SharedPreferences aaprefs;

        Context context = getApplicationContext();
        aaprefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean autoUpChecked = aaprefs.getBoolean(PREF_AUTO_UPDATE, false);

        builder.append("toto: " + autoUpChecked + "\n");
        textView.setText(builder.toString());

        //    boolean autoUpChecked = aaprefs.getBoolean(PREF_AUTO_UPDATE, false);

        //SharedPreferences test = getSharedPreferences("Preferences6", Context.MODE_PRIVATE);
        //int spinnerValue = test.getInt("spinner6",-1);

       // int updateFreqIndex = aaprefs.getInt(PREF_UPDATE_FREQ, -1);
        //int prefsInt = prefs.getInt(KEY_SavedSel, -1);
        //if(prefsInt != -1){
        //    spinner.setSelection(prefsInt);
        //}

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //ajoute les entrées de menu_test à l'ActionBar
        getMenuInflater().inflate(R.menu.menu_settings, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        Intent intent;

        switch (item.getItemId())
        {
            case R.id.action_settings1:
                Toast.makeText(this, "Item1...", Toast.LENGTH_LONG).show();
                intent = new Intent(MainActivity.this, preferences_mode1.class);
                //startActivity(intent);
                startActivityForResult(intent, SHOW_PREFERENCES);
                return true;



            case R.id.action_settings2:
                Toast.makeText(this, "Item2... ", Toast.LENGTH_LONG).show();
                intent = new Intent(MainActivity.this, preferences_mode2.class);
                //startActivity(intent);
                startActivityForResult(intent, SHOW_PREFERENCES2);

                /*super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == 1)
                {
                    if(resultCode == RESULT_OK){
                        String result=data.getStringExtra("result");
                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                    }
                */

                return true;


            case R.id.activity_result:
                Toast.makeText(this, "Item3 result...", Toast.LENGTH_LONG).show();
                intent = new Intent(MainActivity.this, activity_for_result.class);
                startActivity(intent);
                return true;


            case R.id.activity_send:
                Toast.makeText(this, "Item4 send...", Toast.LENGTH_LONG).show();
                intent = new Intent(MainActivity.this, activity_send_data.class);
                startActivity(intent);
                return true;
        }




        return super.onOptionsItemSelected(item);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SHOW_PREFERENCES)
        {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(this, "TRACE", Toast.LENGTH_LONG).show();
                displaySharedPreferences();

                //updateFromPreferences();
                //refreshEarthquakes();
            }
        }

        if (requestCode == SHOW_PREFERENCES2)
        {
            if (resultCode == Activity.RESULT_OK)
            {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                String username = prefs.getString("username", "");
                Toast.makeText(this, username, Toast.LENGTH_LONG).show();
            }
        }


    }



    private void updateFromPreferences()
    {
        SharedPreferences xxprefs;
        Context context = getApplicationContext();
        xxprefs = PreferenceManager.getDefaultSharedPreferences(context);
        // stockage
        ///data/data/com.example.patrice.preferences/shared_prefs/com.example.patrice.preferences_preferences.xml

        boolean autoUpChecked = xxprefs.getBoolean(PREF_AUTO_UPDATE, false);

/*
        int minMagIndex = prefs.getInt(PreferencesActivity.PREF_MIN_MAG_INDEX, 0);
        if (minMagIndex < 0)
            minMagIndex = 0;

        int freqIndex = prefs.getInt(PreferencesActivity.PREF_UPDATE_FREQ_INDEX, 0);
        if (freqIndex < 0)
            freqIndex = 0;
*/
        boolean autoUpdateChecked = xxprefs.getBoolean(preferences_mode1.PREF_AUTO_UPDATE, false);


    }

}


/*
public class MainActivity extends AppCompatActivity
{
    CheckBox autoUpdate;
    Spinner updateFreqSpinner;
    Spinner magnitudeSpinner;

    public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
    public static final String PREF_MIN_MAG = "PREF_MIN_MAG";
    public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";
    SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences_view);

        updateFreqSpinner = (Spinner)findViewById(R.id.spinner_update_freq);
        magnitudeSpinner = (Spinner)findViewById(R.id.spinner_quake_mag);
        autoUpdate = (CheckBox)findViewById(R.id.checkbox_auto_update);
        populateSpinners();
        Context context = getApplicationContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(context);

        updateUIFromPreferences();



        Button okButton = (Button) findViewById(R.id.okButton);
        okButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                savePreferences();
                MainActivity.this.setResult(RESULT_OK);
                finish();
            }
        });

        Button cancelButton = (Button) findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                MainActivity.this.setResult(RESULT_CANCELED);
                finish();
            }
        });
    }

    private void populateSpinners()
    {
        // Populate the update frequency spinner
        ArrayAdapter<CharSequence> fAdapter;
        fAdapter = ArrayAdapter.createFromResource(this, R.array.update_freq_options,
                android.R.layout.simple_spinner_item);
        int spinner_dd_item = android.R.layout.simple_spinner_dropdown_item;
        fAdapter.setDropDownViewResource(spinner_dd_item);
        updateFreqSpinner.setAdapter(fAdapter);

        // Populate the minimum magnitude spinner
        ArrayAdapter<CharSequence> mAdapter;
        mAdapter = ArrayAdapter.createFromResource(this,
                R.array.magnitude_options,
                android.R.layout.simple_spinner_item);
        mAdapter.setDropDownViewResource(spinner_dd_item);
        magnitudeSpinner.setAdapter(mAdapter);

    }

    private void updateUIFromPreferences()
    {
        boolean autoUpChecked = prefs.getBoolean(PREF_AUTO_UPDATE, false);
        int updateFreqIndex = prefs.getInt(PREF_UPDATE_FREQ, 2);
        int minMagIndex = prefs.getInt(PREF_MIN_MAG, 0);
        updateFreqSpinner.setSelection(updateFreqIndex);
        magnitudeSpinner.setSelection(minMagIndex);
        autoUpdate.setChecked(autoUpChecked);
    }

    private void savePreferences() {
        int updateIndex = updateFreqSpinner.getSelectedItemPosition();
        int minMagIndex = magnitudeSpinner.getSelectedItemPosition();
        boolean autoUpdateChecked = autoUpdate.isChecked();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(PREF_AUTO_UPDATE, autoUpdateChecked);
        editor.putInt(PREF_UPDATE_FREQ, updateIndex);
        editor.putInt(PREF_MIN_MAG, minMagIndex);
        editor.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //ajoute les entrées de menu_test à l'ActionBar
        getMenuInflater().inflate(R.menu.menu_settings, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            Intent intent = new Intent(this, preferences_mode2.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
*/