package com.nbusy.app.data.sqldb;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.nbusy.app.data.DB;
import com.nbusy.app.data.Message;
import com.nbusy.app.data.UserProfile;
import com.nbusy.app.data.callbacks.CreateProfileCallback;
import com.nbusy.app.data.callbacks.GetChatMessagesCallback;
import com.nbusy.app.data.callbacks.GetProfileCallback;
import com.nbusy.app.data.callbacks.UpsertMessagesCallback;

public class SQLDB implements DB {

    private final SQLDBHelper sqldbHelper;
    private final SQLiteDatabase db;

    public SQLDB(Context context) {
        sqldbHelper = new SQLDBHelper(context);
        // todo: call this in a background thread as upgrade might take a long while.. also it might fail on full disk
        db = sqldbHelper.getWritableDatabase();
    }

    @Override
    public void createProfile(UserProfile userProfile, CreateProfileCallback cb) {
        // Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();
        values.put(SQLTables.ProfileTable._ID, userProfile.id);
        values.put(SQLTables.ProfileTable.JWT_TOKEN, userProfile.jwttoken);
        values.put(SQLTables.ProfileTable.NAME, userProfile.name);
        values.put(SQLTables.ProfileTable.EMAIL, userProfile.email);
        values.put(SQLTables.ProfileTable.PICTURE, userProfile.picture);

        // Insert the new row, returning the primary key value of the new row
        long newRowId = db.insert(SQLTables.ProfileTable.TABLE_NAME, null, values);
    }

    @Override
    public void getProfile(GetProfileCallback cb) {

    }

    @Override
    public void getChatMessages(String chatId, GetChatMessagesCallback cb) {

    }

    @Override
    public void getQueuedMessages(GetChatMessagesCallback cb) {

    }

    @Override
    public void upsertMessages(UpsertMessagesCallback cb, Message... msgs) {

    }
}

@@ -98,16 +98,48 @@
 ontext;%0A
+import android.database.Cursor;%0A
 import a
@@ -1691,32 +1691,1274 @@
 eCallback cb) %7B%0A
+        String%5B%5D projection = %7B%0A                SQLTables.ProfileTable._ID,%0A                SQLTables.ProfileTable.JWT_TOKEN,%0A                SQLTables.ProfileTable.NAME,%0A                SQLTables.ProfileTable.EMAIL%0A        %7D;%0A%0A        Cursor c = db.query(%0A                SQLTables.ProfileTable.TABLE_NAME,  // The table to query%0A                projection,                               // The columns to return%0A                %22*%22,                                // The columns for the WHERE clause%0A                null,                            // The values for the WHERE clause%0A                null,                                     // don't group the rows%0A                null,                                     // don't filter by row groups%0A                null                                 // The sort order%0A        );%0A%0A        c.moveToFirst();%0A        String id = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable._ID));%0A        String jwtToken = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.JWT_TOKEN));%0A        String name = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.NAME));%0A        String email = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.EMAIL));%0A%0A//        cb.
 %0A    %7D%0A%0A    @Ove

add getProfile stub