package com.example.shared01;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{
   EditText ed1,ed2,ed3;
   Button b1,b2,b3;

   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey";
   public static final String Phone = "phoneKey";
   public static final String Email = "emailKey";
   
   SharedPreferences sharedpreferences;
   private SharePreference sharePreference;
   Activity context = this;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.activity_main);
	      
	      ed1=(EditText)findViewById(R.id.editText);
	      ed2=(EditText)findViewById(R.id.editText2);
	      ed3=(EditText)findViewById(R.id.editText3);
	      
	      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
	      sharePreference = new SharePreference();
	      
	      b1=(Button)findViewById(R.id.save);
	      b1.setOnClickListener(new View.OnClickListener()
	      {
		         @Override
		         public void onClick(View v)
		         {
		            String n  = ed1.getText().toString();
		            String ph  = ed2.getText().toString();
		            String e  = ed3.getText().toString();
		            
		            SharedPreferences.Editor editor = sharedpreferences.edit();
		            
		            editor.putString(Name, n);
		            editor.putString(Phone, ph);
		            editor.putString(Email, e);
		            editor.commit();
		            Toast.makeText(MainActivity.this,"Save",Toast.LENGTH_LONG).show();
		            
		         }
	      });
	      
	      b2 = (Button) findViewById(R.id.second);
	      b2.setOnClickListener(new View.OnClickListener()
	      {
	    	  
	            @Override
	            public void onClick(View v)
	            {
	            	
	            	 String n  = ed1.getText().toString();
	                 sharePreference.save(context, n);
	                 Intent intent = new Intent(context, SecondActivity.class);
	                 startActivity(intent);
	            	 Toast.makeText(MainActivity.this,"Transfert",Toast.LENGTH_LONG).show();
	            }
	        });
	      
	      b3 = (Button) findViewById(R.id.load);
	      b3.setOnClickListener(new View.OnClickListener()
	      {
	    	  
	            @Override
	            public void onClick(View v)
	            {
	            	
	            	 // SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
	            	 // String stremail = sharedPreferences.getString("nameKey", "");
	            	 // ed1.setText(stremail);
	            	 //Toast.makeText(MainActivity.this,"Load",Toast.LENGTH_LONG).show();
	            	  
	            	  sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
	            	  String stremail = sharedpreferences.getString("nameKey", "");
	            	  ed1.setText(stremail);
		              Toast.makeText(MainActivity.this,"Load",Toast.LENGTH_LONG).show(); 
	            	  
	            }
	        });
	      
	      
	       
	    
	      
   }
   
   public static void DeleteSingleEntryFromPref(String keyName)
	{
		//SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
		//SharedPreferences.Editor editor = settings.edit();
		//editor.remove(keyName);
		//editor.commit();
	}

	public static void DeleteAllEntriesFromPref()
	{
		//SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
		//SharedPreferences.Editor editor = settings.edit();
		//editor.clear();
		//editor.commit();
	}
   
   
  
}