package com.ethostream.ethoandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseLoginAdapter
{
	// Database fields
		private static final String LOGIN_TABLE = "Login";
		private static final String LOGIN_ID = "_id";
		private static final String LOGIN_USERNAMME = "Username";
		private static final String LOGIN_PASSWORD = "Password";
		private Context context;
		private SQLiteDatabase database;
		private LocalSQLiteHelper dbHelper;
		
		/**
		 * Constructor. Still need to call open for anything interesting to happen.
		 * @param context
		 */
		public DatabaseLoginAdapter(Context context) {
			this.context = context;
		}
		
		/**
		 * Open parent database in a writable state. This should also create a new
		 * database if necessary, but seems not to :(
		 * 
		 * TODO: Find out the issue here
		 * 
		 * @return
		 * @throws SQLException
		 */
		public DatabaseLoginAdapter open() throws SQLException {
			dbHelper = new LocalSQLiteHelper(context);
			database = dbHelper.getWritableDatabase();
			createLoginTable();
			return this;
		}
		
		/**
		 * Performs a simple database insert, adding a given record to the Login
		 * table.
		 * 
		 * @param rowData (ContentValues) Schema - _id:int(AUTOGENERATED,
		 * 															DO NOT INCLUDE)
		 * 										 - Username:String, not null
		 * 										 - Password:String, not null
		 * @return long - rowId of inserted record
		 */
		public long createLogin(ContentValues rowData) 
		{
			return database.insert(LOGIN_TABLE, null, rowData);
		}
		
		
		
		/**
		 * If the Login table does not exist, this function will create it. If it
		 * does exist, this function will not complain.
		 * 
		 */
		public void createLoginTable()
		{
			database.execSQL("CREATE TABLE IF NOT EXISTS " 
					+ LOGIN_TABLE 
					+ "(" 
					+ LOGIN_ID + " INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL, " 
					+ LOGIN_USERNAMME + " VARCHAR(64) NOT NULL, "
					+ LOGIN_PASSWORD + " VARCHAR(64) NOT NULL);"
					+ ")");
		}
		
		/**
		 * Return a Cursor over the list of all logins in the database
		 * 
		 * @return Cursor over all logins
		 */
		public Cursor getAll() 
		{
			return database.query(LOGIN_TABLE, null, null, null, null, null, null);
		}
		
		/**
		 * Delete a Login with a given rowId.
		 * 
		 * @param rowId
		 * @return boolean - true on success.
		 */
		public boolean deleteProperty(long rowId) {
			return database.delete(LOGIN_TABLE, LOGIN_ID + "=" + rowId, null) > 0;
		}
		
		/**
		 * Returns a cursor over any rows matching both the username and password
		 * passed in as parameters.
		 * 
		 * @param username
		 * @param password
		 * @return Cursor
		 */
		public Cursor verifyLogin(String username, String password) 
		{
			return database.query(LOGIN_TABLE, null, LOGIN_USERNAMME
					+ " = ? AND " + LOGIN_PASSWORD + "= ?",
					new String[]{username, password}, null, null, null);
		}
		
		/**
		 * Drop Login Table
		 */
		public void dropLoginTable()
		{
			database.execSQL("DROP TABLE IF EXISTS " + LOGIN_TABLE);
		}
		
		/**
		 * Cleanly close parent database.
		 */
		public void close() {
			dbHelper.close();
		}
}
