package com.example.listviewcustom03;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ContactsDbAdapter 
{
		 
		 public static final String KEY_ID = "id";
		 public static final String KEY_NOM = "nom";
		 public static final String KEY_PRENOM = "prenom";
		 public static final String KEY_ADRESSE = "adresse";
		 public static final String KEY_EMAIL = "email";
		 public static final String KEY_PORTABLE = "portable";
		 public static final String KEY_FIXE = "fixe";

		 private static final String TAG = "ContactsDbAdapter";
		 private DatabaseHelper mDbHelper;
		 private SQLiteDatabase mDb;

		 private static final String DATABASE_NAME = "contacts_book";
		 private static final String SQLITE_TABLE = "contacts_perso";
		 private static final int DATABASE_VERSION = 1;

		 private final Context mCtx;
		 
		 private static final String DATABASE_CREATE =
				  "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
				  KEY_ID + " integer PRIMARY KEY autoincrement," +
				  KEY_NOM + "," +
				  KEY_PRENOM + "," +
				  KEY_ADRESSE + "," +
				  KEY_EMAIL + "," +
				  KEY_PORTABLE + "," +
				  KEY_FIXE  + ");";
 
		 private static class DatabaseHelper extends SQLiteOpenHelper
		 {
			 DatabaseHelper(Context context)
			 {
				 super(context, DATABASE_NAME, null, DATABASE_VERSION);
			 }

			 @Override
			 public void onCreate(SQLiteDatabase db)
			 {
				 Log.w(TAG, DATABASE_CREATE);
				 db.execSQL(DATABASE_CREATE);
			 }

			 @Override
			 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
			 {
				 Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
				 db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
				 onCreate(db);
			 }
		 }

		 public ContactsDbAdapter(Context ctx)
		 {
			 this.mCtx = ctx;
		 }

		 public ContactsDbAdapter open() throws SQLException 
		 {
			 mDbHelper = new DatabaseHelper(mCtx);
			 mDb = mDbHelper.getWritableDatabase();
			 return this;
		 }

		 public void close() 
		 {
			 if (mDbHelper != null)
			 {
				 mDbHelper.close();
			 }
		 }

		 
		 public int getContactsCount() 
		 {
		        String countQuery = "SELECT  * FROM " + SQLITE_TABLE;
		      
		        Cursor cursor = mDb.rawQuery(countQuery, null);
		        cursor.close();
		        return cursor.getCount();
		    }
		 
		 public long createContacts(String nom, String prenom, String adresse, String email, String portable,  String fixe) 
		 {
			 ContentValues initialValues = new ContentValues();
			 initialValues.put(KEY_NOM, nom);
			 initialValues.put(KEY_PRENOM, prenom);
			 initialValues.put(KEY_ADRESSE, adresse);
			 initialValues.put(KEY_EMAIL, email);
			 initialValues.put(KEY_PORTABLE, portable);
			 initialValues.put(KEY_FIXE, fixe);
			 
			 return mDb.insert(SQLITE_TABLE, null, initialValues);
		 }

		 public boolean deleteAllContacts()
		 {
			 int doneDelete = 0;
			 doneDelete = mDb.delete(SQLITE_TABLE, null , null);
			 Log.w(TAG, Integer.toString(doneDelete));
			 return doneDelete > 0;
		 }

		 public Cursor fetchContactsByName(String inputText) throws SQLException
		 {
			 Log.w(TAG, inputText);
			 Cursor mCursor = null;
			 if (inputText == null  ||  inputText.length () == 0)  
			 {
				 mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ID, KEY_NOM, KEY_PRENOM, KEY_ADRESSE, KEY_EMAIL, KEY_PORTABLE , KEY_FIXE}, null, null, null, null, null, null);
			 }
			 else
			 {
				 mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ID, KEY_NOM, KEY_PRENOM, KEY_ADRESSE, KEY_EMAIL, KEY_PORTABLE , KEY_FIXE}, 
						 												KEY_NOM + " like '%" + inputText + "%'", null, null, null, null, null, null);
			 }
			 
			 if (mCursor != null)
			 {
				 mCursor.moveToFirst();
			 }
			 return mCursor;

		 }

		 public Cursor fetchAllContacts()
		 {
		
			 	Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ID, KEY_NOM, KEY_PRENOM, KEY_ADRESSE, KEY_EMAIL, KEY_PORTABLE, KEY_FIXE}, 
			 															null, null, null, null, null, null);
		
			 	if (mCursor != null)
			 	{
			 		mCursor.moveToFirst();
			 	}
			 	return mCursor;
		 }

		 
		 
		 public void insertContacts()
		 {
			 createContacts("Hollande","Francois" ,"Toulouse","mail", "06000000", "05000000");
			 createContacts("Bourne","Jason","Toulouse","mail", "06000000", "05000000");
			 createContacts("Sapin","Michel","Toulouse","mail", "06000000", "05000000");
			 
			 createContacts("TOTO","blase" ,"Realmont","nada", "06000000", "05000000");
			 createContacts("XXX","Jason","Toulouse","mail", "06000000", "05000000");
			 createContacts("BBB","Michel","Toulouse","mail", "06000000", "05000000");
			 
			 createContacts("AGX","Francois" ,"Toulouse","mail", "06000000", "05000000");
			 createContacts("AAF","Jason","Toulouse","mail", "06000000", "05000000");
			 createContacts("Sapin","Michel","Toulouse","mail", "06000000", "05000000");
			 
			 
		 }
		 
		 
		 
		// Update contact datas
		public boolean editContact(int id, String nom, String prenom, String adresse, String email, String portable,  String fixe ) 
		{
		        ContentValues vals = new ContentValues();
		        vals.put(KEY_NOM, nom);
				vals.put(KEY_PRENOM, prenom);
				vals.put(KEY_ADRESSE, adresse);
				vals.put(KEY_EMAIL, email);
				vals.put(KEY_PORTABLE, portable);
				vals.put(KEY_FIXE, fixe);
			
		        // prévoir une photo
		        // updating row
		        //int i = mDb.update(SQLITE_TABLE, vals, KEY_ID + " = ?",  new String[] { String.valueOf(id) });
		        
				
				
		        //int i = mDb.update(SQLITE_TABLE, vals, "KEY_ID=?", Integer.toString(id)); 
				//http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
				//http://www.java2s.com/Code/Android/Database/Createdeleteupdate.htm
		        // 406
				// 415
		        // 424
		        //int i = mDb.update(SQLITE_TABLE, vals, KEY_ID + " = ?", new String[] { String.valueOf(433) });
		        
		        //int i = mDb.update(SQLITE_TABLE, vals, KEY_ID + "=" + id, null);
		        
		        //int i = mDb.update(SQLITE_TABLE, vals, "id="+id, null);
		        int i = 1;
				
		        //mDb.close();
		        
		        if(i != 0)
		        {
		        	return true;
		        }
		        else
		        {
		        	return false;
		        }
				
		}


}