package com.example.listviewcustom05;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

// reference 
//http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html

// http://houssem-lahiani.blogspot.fr/2014/09/android-tuto-afficher-des-donnees-dune.html

// https://examples.javacodegeeks.com/android/core/provider/android-contacts-example/     read contacts   a voir au plus vite ...

// http://stackandroid.com/tutorial/android-reading-phone-contacts-example/

public class MainActivity extends Activity
{

		private DbAdapter dbHelper;
		private SimpleCursorAdapter dataAdapter;

		@Override
		public void onCreate(Bundle savedInstanceState)
		{
			 	super.onCreate(savedInstanceState);
			 	setContentView(R.layout.activity_main);
			 	
				//ActionBar actionBar = getActionBar();
			    //actionBar.show();
		
			 	dbHelper = new DbAdapter(this);
			 	dbHelper.open();
		
			 	//Clean all data
			 	//dbHelper.deleteAllCountries();
			 	//Add some data
			 	//dbHelper.insertSomeCountries();
		
			 	//Generate ListView from SQLite Database
			 	displayListView();
			 
			 	//dbHelper.createContact("nom", "prenom", "adresse", "email", "portable", "fixe", "photograph" ); 
			 	//dbHelper.createContact("zorro", "zorroprenom", "zorroadresse", "zorroemail", "zorroportable", "zorrofixe", "zorrophotograph" ); 
		
		}

		private void displayListView()
	 	{
				Cursor cursor = dbHelper.fetchAllContacts();
/*	
				// The desired columns to be bound
				String[] columns = new String[] {
								  DbAdapter.KEY_CODE,
								  DbAdapter.KEY_NAME,
								  DbAdapter.KEY_CONTINENT,
								  DbAdapter.KEY_REGION
								};
	
				// the XML defined views which the data will be bound to
				int[] to = new int[] { 
		 							R.id.code,
		 							R.id.name,
		 							R.id.continent,
		 							R.id.region,
		 						};
*/				
				// The desired columns to be bound
				String[] col = new String[] {
								  DbAdapter.KEY_NOM,
								  DbAdapter.KEY_PRENOM,
								};
	
				// the XML defined views which the data will be bound to
				int[] to = new int[] { 
		 							R.id.nom,
		 							R.id.prenom,
		 						};
				
	
			 	// create the adapter using the cursor pointing to the desired data 
			 	//as well as the layout information
			 	dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_infosbis, cursor, col, to, 0);
		
			 	ListView listView = (ListView) findViewById(R.id.listView1);
			 
			 	listView.setAdapter(dataAdapter);
	
			 	listView.setOnItemClickListener(new OnItemClickListener() 
				{
						   @Override
						   public void onItemClick(AdapterView<?> listView, View view, int position, long id) 
						   {
							   // Get the cursor, positioned to the corresponding row in the result set
							   Cursor cursor = (Cursor) listView.getItemAtPosition(position);
						
							   //String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("code"));
							   
//							   String code = cursor.getString(cursor.getColumnIndex("code"));
//							   String name = cursor.getString(cursor.getColumnIndex("name"));
//							   String continent = cursor.getString(cursor.getColumnIndex("continent"));
//							   String region = cursor.getString(cursor.getColumnIndex("region"));
							   
							   String nom = cursor.getString(cursor.getColumnIndex("nom"));
							   String prenom = cursor.getString(cursor.getColumnIndex("prenom"));
							   String adresse = cursor.getString(cursor.getColumnIndex("adresse"));
							   String email = cursor.getString(cursor.getColumnIndex("email"));
							   String portable = cursor.getString(cursor.getColumnIndex("portable"));
							   String fixe = cursor.getString(cursor.getColumnIndex("fixe"));
							   String photograph = cursor.getString(cursor.getColumnIndex("photograph"));
						   	
						   	   String[] columnNames = cursor.getColumnNames();
						   	 
				//				for(int i=0;i<cursor.getCount();i++)
				//				{
									//String aaa = cursor.getString(cursor.getColumnIndexOrThrow("code"));
									//String bbb = cursor.getString(cursor.getColumnIndexOrThrow("name"));
									//String ccc = cursor.getString(cursor.getColumnIndexOrThrow("continent"));
									//String ddd = cursor.getString(cursor.getColumnIndexOrThrow("region"));
									
									//String code = cursor.getString(cursor.getColumnIndex("code"));
									//String name = cursor.getString(cursor.getColumnIndex("name"));
									//String continent = cursor.getString(cursor.getColumnIndex("continent"));
									//String region = cursor.getString(cursor.getColumnIndex("region"));
									
									
									 // int item1 = cursor.getInt(cursor.getColumnIndex(DbAdapter.KEY_CODE));
									  //String item2 = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_NAME));
									  //String item3 = cursor.getString(cursor.getColumnIndex(KEY_PRENOM));
				//					  String item4 = cursor.getString(cursor.getColumnIndex(KEY_ADRESSE));
				//					  String item5 = cursor.getString(cursor.getColumnIndex(KEY_EMAIL));
				//					  String item6 = cursor.getString(cursor.getColumnIndex(KEY_PORTABLE));
				//					  String item7 = cursor.getString(cursor.getColumnIndex(KEY_FIXE));
									  // pour l'ordre alphabétique on prend la premiére lettre du nom -> section headers doit être amélioré  
				//					  String firstChar = item2.substring(0, 1);
				//					  items.add(new ListCell(item1, item2, item3, item4, item5, item6, item7 , firstChar));
				//					  cursor.moveToNext();
				//				}
						   	   
						   			Intent intent = new Intent(MainActivity.this, debug02.class); //Contact_details 
									intent.putExtra("id", position);
									intent.putExtra("nom", nom);
									intent.putExtra("prenom", prenom);
									intent.putExtra("adresse", adresse);
									intent.putExtra("email", email);
									intent.putExtra("portable", portable);
									intent.putExtra("fixe", fixe);
									intent.putExtra("photograph", photograph);
									startActivity(intent);
						   }
				});
		
	  
		 		EditText myFilter = (EditText) findViewById(R.id.myFilter);
		 		
		 		myFilter.addTextChangedListener(new TextWatcher() 
		 		{
		 			public void afterTextChanged(Editable s) 
		 			{
		 			}
		 			public void beforeTextChanged(CharSequence s, int start, int count, int after)
		 			{
		 			}
		 			public void onTextChanged(CharSequence s, int start, int before, int count) 
		 			{
		 				dataAdapter.getFilter().filter(s.toString());
		 			}
		 		});
	  
		 		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() 
		 		{
		 			public Cursor runQuery(CharSequence constraint) 
		 			{
		 				return dbHelper.fetchContactByName(constraint.toString());
		 			}
		 		});
	
	 	}
 
		@Override
		public boolean onCreateOptionsMenu(Menu menu) 
		{
		      getMenuInflater().inflate(R.menu.main, menu);
		      return true;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) 
		{
		    switch (item.getItemId())
		    {
			    case R.id.menu_about:
			        // Comportement du bouton "A Propos"
			        return true;
			    case R.id.menu_help:
			        // Comportement du bouton "Aide"
			        return true;
			    case R.id.menu_refresh:
			        // Comportement du bouton "Rafraichir"
			        return true;
			    case R.id.menu_search:
			        // Comportement du bouton "Recherche"
			        return true;
			    case R.id.menu_settings:
			        // Comportement du bouton "Paramètres"
		    		Toast.makeText(getBaseContext(), "Infos", Toast.LENGTH_SHORT).show();
		    		Intent intent5 = new Intent(this, Infos_Db.class);
		            startActivity(intent5); 
		            return true;
			    case R.id.menu_add:
			        // Comportement du bouton "Paramètres"
		    		Toast.makeText(getBaseContext(), "Add", Toast.LENGTH_SHORT).show();
		    		Intent intent_add = new Intent(this, add.class);
		            startActivity(intent_add);     
			    case R.id.menu_debug:
			        // Comportement du bouton "Paramètres"
		    		Toast.makeText(getBaseContext(), "insertion", Toast.LENGTH_SHORT).show();
		    		Intent intent_gestion = new Intent(this, Insertion.class);
		            startActivity(intent_gestion);        
		            
		            
			    default:
			        return super.onOptionsItemSelected(item);
		    }
		}
 
}