package com.example.listviewcustom03;


import java.util.ArrayList;
import java.util.List;

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

public class ContactHandler extends SQLiteOpenHelper 
{
	
	// All variables about DB
	// Database name
	private static final String DATABASE_NAME = "patochecontactBook";
	
	// Database version
	private static final int DATABASE_VERSION = 1;
	
	// Contacts table name
	private static final String TABLE_CONTACT = "test_contacts";
	
	// Table Column names
	private static final String COLUMN_ID = "id";
	private static final String COLUMN_NOM = "nom";
	private static final String COLUMN_PRENOM = "prenom";
	private static final String COLUMN_ADRESSE = "adresse";
	private static final String COLUMN_EMAIL = "email";
	private static final String COLUMN_PORTABLE = "portable";
	private static final String COLUMN_FIXE = "fixe";
	private static final String COLUMN_PHOTOGRAPH = "photograph";
	
	private String[] columns= {COLUMN_ID, COLUMN_NOM, COLUMN_PRENOM, COLUMN_ADRESSE, COLUMN_EMAIL, COLUMN_PORTABLE, COLUMN_FIXE, COLUMN_PHOTOGRAPH};
	
	// Create database
	public ContactHandler(Context context) 
	{
					super(context, DATABASE_NAME, null, DATABASE_VERSION);		
	}

	
	// Create table
	@Override
	public void onCreate(SQLiteDatabase db) 
	{		
					String CREATE_TABLE = "CREATE TABLE " + TABLE_CONTACT + "("
			             + COLUMN_ID + " INTEGER PRIMARY KEY," 
							+ COLUMN_NOM + " TEXT,"
							+ COLUMN_PRENOM + " TEXT,"
							+ COLUMN_ADRESSE + " TEXT,"
							+ COLUMN_EMAIL + " TEXT,"
							+ COLUMN_PORTABLE + " TEXT,"
							+ COLUMN_FIXE + " TEXT,"
							+ COLUMN_PHOTOGRAPH + " TEXT"
							+ ")";
					
					db.execSQL(CREATE_TABLE);	
	}
	
	// Drop table if older version exist
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
					db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
				    onCreate(db);
	}
	
	/*
	 * Handling Contact table using sql queries.
	 * */
	
	 public int getContactsCount() 
	 {
					 SQLiteDatabase db = this.getWritableDatabase();
					 //SQLiteDatabase db = this.getReadableDatabase();
				     //String countQuery = "SELECT  * FROM " + TABLE_CONTACT;
				     //Cursor cursor = db.rawQuery(countQuery, null);
					 
					 
					 Cursor c = db.rawQuery("select * from test_contacts",null);
					 Log.i("Number of Records"," :: "+c.getCount());
					 
				     
				     int numRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_CONTACT);
				     ///return numRows;
				     
				     return c.getCount();
				     
				     //cursor.close();
				     //int q = cursor.getCount();
				     //return cursor.getCount();
	}
	
	// Add Contact
	public boolean addContactDetails(Contact contact)
	{
					// Get db writable
					SQLiteDatabase db = this.getWritableDatabase();
					
					// Get the values to insert
					ContentValues vals = new ContentValues();
					vals.put(COLUMN_NOM, contact.getNom());
					vals.put(COLUMN_PRENOM, contact.getPrenom());
					vals.put(COLUMN_ADRESSE, contact.getAdresse());
					vals.put(COLUMN_EMAIL, contact.getEmail());
					vals.put(COLUMN_PORTABLE, contact.getPortable());
					vals.put(COLUMN_FIXE, contact.getFixe());
					vals.put(COLUMN_PHOTOGRAPH, contact.getPhotograph());
					
					// Insert values into table
					long i = db.insert(TABLE_CONTACT, null, vals);
					// Close database
					db.close();
					
					 if(i != 0)
					 {
				       	return true;
				     }
					 else
					 {
				       	return false;
				     }
	}
	
	
	// Reading all contacts
	public List<Contact> readAllContacts()
	{
					// Get db writable
					SQLiteDatabase db = this.getWritableDatabase();
			
					// Define contacts list
					List<Contact> contacts = new ArrayList<Contact>();
			
					Cursor cursor = db.query(TABLE_CONTACT, columns, null, null, null, null, null, null  );
	
				    cursor.moveToFirst();
				    
				    while (!cursor.isAfterLast()) 
				    {
				    	Contact contact = new Contact();
				    	contact.setID(Integer.parseInt(cursor.getString(0)));
				    	contact.setNom(cursor.getString(1));
				    	contact.setPrenom(cursor.getString(2));
				    	contact.setAdresse(cursor.getString(3));
				    	contact.setEmail(cursor.getString(4));
				    	contact.setPortable(cursor.getString(5));
				    	contact.setFixe(cursor.getString(6));
				    	contact.setPhotograph(cursor.getString(7));
				    	contacts.add(contact);
				    	cursor.moveToNext();
				    }
				    
				    // Make sure to close the cursor
				    cursor.close();
				    return contacts;
	}
	
	// Update contact contact
	public boolean editContact(Contact contact)
	{
					SQLiteDatabase db = this.getWritableDatabase();
				    
				    ContentValues vals = new ContentValues();
					vals.put(COLUMN_NOM, contact.getNom());
					vals.put(COLUMN_PRENOM, contact.getPrenom());
					vals.put(COLUMN_ADRESSE, contact.getAdresse());
					vals.put(COLUMN_EMAIL, contact.getEmail());
					vals.put(COLUMN_PORTABLE, contact.getPortable());
					vals.put(COLUMN_FIXE, contact.getFixe());
					vals.put(COLUMN_PHOTOGRAPH, contact.getPhotograph());
		
				    // updating row
				    int i = db.update(TABLE_CONTACT, vals, COLUMN_ID + " = ?",  new String[] { String.valueOf(contact.getID()) });
				     
				    db.close();
				     
				    if(i != 0)
				    {
				     	return true;
				    }
				    else
				    {
				     	return false;
				    }
		
	}
	
	// Deleting contact
	public boolean deleteContact(int id)
	{
					SQLiteDatabase db = this.getWritableDatabase();
					
					int i = db.delete(TABLE_CONTACT, COLUMN_ID + " = ?",  new String[] { String.valueOf(id) });
			     
					db.close();
			     
					if(i != 0)
					{
						return true;
					}
					else
					{
						return false;
					}
	}

}
