package com.nbusy.app.data.sqldb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
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) {
        String[] projection = {
                SQLTables.ProfileTable._ID,
                SQLTables.ProfileTable.JWT_TOKEN,
                SQLTables.ProfileTable.NAME,
                SQLTables.ProfileTable.EMAIL
        };

        Cursor c = db.query(
                SQLTables.ProfileTable.TABLE_NAME,  // The table to query
                projection,                               // The columns to return
                "*",                                // The columns for the WHERE clause
                null,                            // The values for the WHERE clause
                null,                                     // don't group the rows
                null,                                     // don't filter by row groups
                null                                 // The sort order
        );

        c.moveToFirst();
        String id = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable._ID));
        String jwtToken = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.JWT_TOKEN));
        String name = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.NAME));
        String email = c.getString(c.getColumnIndexOrThrow(SQLTables.ProfileTable.EMAIL));

//        cb.
    }

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

    }

    @Override
    public void getQueuedMessages(GetChatMessagesCallback cb) {

    }

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

    }
}

@@ -3224,16 +3224,873 @@
  msgs) %7B
+%0A//// New value for one column%0A//        ContentValues values = new ContentValues();%0A//        values.put(FeedEntry.COLUMN_NAME_TITLE, title);%0A//%0A//// Which row to update, based on the ID%0A//        String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + %22 LIKE ?%22;%0A//        String%5B%5D selectionArgs = %7B String.valueOf(rowId) %7D;%0A//%0A//        int count = db.update(%0A//                FeedReaderDbHelper.FeedEntry.TABLE_NAME,%0A//                values,%0A//                selection,%0A//                selectionArgs);%0A    %7D%0A%0A    private void deleteMessages() %7B%0A//        // Define 'where' part of query.%0A//        String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + %22 LIKE ?%22;%0A//// Specify arguments in placeholder order.%0A//        String%5B%5D selectionArgs = %7B String.valueOf(rowId) %7D;%0A//// Issue SQL statement.%0A//        db.delete(table_name, selection, selectionArgs);
 %0A%0A    %7D%0A

add upsertMessages stub