package us.bmaupin.flashcards.arabic;

// $Id$

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class RankDatabaseHelper extends SQLiteOpenHelper {
	// tag for log messages
	public static final String TAG = "RankDatabaseHelper";

    // The name of your database
    public static final String DATABASE_NAME = "ranks.db";
    // The version of your database (increment this every time you change something)
    public static final int DATABASE_VERSION = 1;
    // The name of the table in your database
    public static final String DB_TABLE_NAME = "ranks";
   
    // The name of each column in the database
    public static final String KEY_RANK = "rank";

    // SQL Statement to create a new database.
    public static final String DB_TABLE_CREATE =
        "CREATE TABLE " + DB_TABLE_NAME + " (" +
        BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_RANK + " INTEGER);";
   
    // The constructor method
    public RankDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /* Called when the super class getWritableDatabase (or getReadableDatabase)
     * method determines that the database doesn't exist yet and needs to be created
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
    	Log.d(TAG, "onCreate called");
        db.execSQL(DB_TABLE_CREATE);       
    }

    /* Called when the super class getWritableDatabase (or getReadableDatabase)
     * method determines that the existing database isn't the same version as
     * the latest version
     */
    @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 " + DB_TABLE_NAME);
//        onCreate(db);
// TODO: upgrade the db properly
    }
    
    void initializeDb (SQLiteDatabase db, int totalRows) {
    	String sql = "SELECT COALESCE(MAX(_ID)+1, 0) FROM " + DB_TABLE_NAME;
    	Cursor cursor = db.rawQuery(sql, null);
    	cursor.moveToFirst();
    	int existingRows = cursor.getInt(0);
    	Log.d(TAG, "initializeDb: existingRows=" + existingRows );
    	cursor.close();
    	
    	// TODO: fill db with data up to number of total rows
    }
}

@@ -816,20 +816,16 @@
  String 
-KEY_
 RANK = %22
@@ -831,16 +831,59 @@
 %22rank%22;%0A
+    %0A    private final Context context;    
 %0A    // 
@@ -1099,12 +1099,8 @@
     
-KEY_
 RANK
@@ -1262,16 +1262,48 @@
 RSION);%0A
+        this.context = context;%0A
     %7D%0A%0A 
@@ -1606,23 +1606,51 @@
 CREATE);
+%0A
        
+ %0A        initializeDb(db);
 %0A    %7D%0A%0A
@@ -2254,23 +2254,8 @@
 e db
-, int totalRows
 ) %7B%0A
@@ -2265,16 +2265,477 @@
  %09String
+ sql = %22SELECT COALESCE(MAX(_ID)+1, 0) FROM %22 + DatabaseHelper.DB_TABLE_NAME;%0A    %09%0A    %09DatabaseHelper cardsHelper = new DatabaseHelper(context);%0A        SQLiteDatabase cardsDb = cardsHelper.getReadableDatabase();%0A        Cursor cardsCursor = cardsDb.rawQuery(sql, null);%0A        cardsCursor.moveToFirst();%0A        int cardsRows = cardsCursor.getInt(0);%0A        Log.d(TAG, %22initializeDb: cardsRows=%22 + cardsRows );%0A        cardsCursor.close();%0A        %0A       
  sql = %22
@@ -2870,24 +2870,21 @@
    %09int 
-existing
+ranks
 Rows = c
@@ -2931,24 +2931,21 @@
 lizeDb: 
-existing
+ranks
 Rows=%22 +
@@ -2945,24 +2945,21 @@
 ows=%22 + 
-existing
+ranks
 Rows );%0A
@@ -2985,21 +2985,16 @@
 ;%0A    %09%0A
-    %09
 // TODO:

drop the KEY_ from the db field names, calculate the number of rows in the cards db internally