package ar.edu.unc.famaf.redditreader.backend;

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

/**
 * Created by aguilarjp on 03/11/16.
 */

public class RedditDBHelper extends SQLiteOpenHelper {
    private static final String TAG = "RedditDBHelper";

    public static final String DATABASE_NAME = "redditdb.db";
    public static final int DATABASE_VERSION = 1;

    public static final String POST_TABLE_ID = "_id";
    public static final String POST_TABLE = "post";
    public static final String POST_TABLE_TITLE = "title";
    public static final String POST_TABLE_AUTHOR = "author";
    public static final String POST_TABLE_CREATED_TIME = "created_time";
    public static final String POST_TABLE_COMMENT_NUMBER = "comment_number";
    public static final String POST_TABLE_THUMBNAIL = "thumbnail";
    public static final String POST_TABLE_SUBREDDIT = "subreddit";
    public static final String POST_TABLE_POST_ID = "id";

    public RedditDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createSentence = " create table " + POST_TABLE + " ( "
                + POST_TABLE_ID + " integer primary key autoincrement, "
                + POST_TABLE_TITLE + " text not null, "
                + POST_TABLE_AUTHOR + " text not null, "
                + POST_TABLE_CREATED_TIME + " integer not null, "
                + POST_TABLE_COMMENT_NUMBER + " integer not null, "
                + POST_TABLE_THUMBNAIL + " text not null, "
                + POST_TABLE_SUBREDDIT + " text not null, "
                + POST_TABLE_POST_ID + " text not null"
                + " );";

        Log.d(TAG, createSentence);
        db.execSQL(createSentence);
        Log.d(TAG, "Database created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "Database updated");
        db.execSQL("DROP TABLE IF EXISTS " + POST_TABLE);
        this.onCreate(db);
    }
}

@@ -893,16 +893,20 @@
 HUMBNAIL
+_URL
  = %22thum
@@ -910,16 +910,20 @@
 humbnail
+_url
 %22;%0A    p
@@ -1041,16 +1041,97 @@
  = %22id%22;
+%0A    public static final String POST_TABLE_THUMBNAIL_BITMAP = %22thumbnail_bitmap%22;
 %0A%0A    pu
@@ -1727,16 +1727,20 @@
 HUMBNAIL
+_URL
  + %22 tex
@@ -1867,16 +1867,18 @@
 not null
+, 
 %22%0A      
@@ -1889,18 +1889,74 @@
       + 
+POST_TABLE_THUMBNAIL_BITMAP + %22 blob %22%0A                + 
 %22
- 
 );%22;%0A%0A  

Add thumbnail image to database