package edu.berkeley.eecs.cfc_tracker.log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.StreamHandler;

/**
 * Created by shankari on 7/16/15.
 */
public class DatabaseLogHandler extends SQLiteOpenHelper {
    private String TABLE_LOG = "logTable";
    private String KEY_LINE = "log_line";
    private String KEY_ID = "ID";
    private static final int DATABASE_VERSION = 1;

    private Context cachedContext;

    public DatabaseLogHandler(Context context) {
        super(context, "logDB", null, DATABASE_VERSION);
        cachedContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_LOG_TABLE = "CREATE TABLE " + TABLE_LOG +" (" +
                KEY_ID + " INTEGER PRIMARY_KEY, "+ KEY_LINE +" TEXT)";
        System.out.println("CREATE_CLIENT_STATS_TABLE = " + CREATE_LOG_TABLE);
        sqLiteDatabase.execSQL(CREATE_LOG_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_LOG);
        onCreate(sqLiteDatabase);
    }

    public void log(String line) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(KEY_LINE, line);
        db.insert(TABLE_LOG, null, cv);
        db.close();
    }

    public void export() {
        SQLiteDatabase db = this.getReadableDatabase();
        try {
            String ourFileName = Log.getLogBase(cachedContext) + "/dumped_log_file.txt";
            System.out.println("ourFileName = "+ourFileName);
            PrintStream out = new PrintStream(new FileOutputStream(ourFileName, true));
            String queryString = "SELECT * from " + TABLE_LOG;
            Cursor cur = db.rawQuery(queryString, null);
            int resultCount = cur.getCount();
            if (cur.moveToFirst()) {
                for (int i = 0; i < resultCount; i++) {
                    out.println(cur.getString(0));
                }
            }
        } catch (IOException e) {
            Toast.makeText(cachedContext, e.getMessage(), Toast.LENGTH_LONG).show();
            System.err.println();
        }
        db.close();
    }
}

@@ -398,32 +398,136 @@
 io.PrintStream;%0A
+import java.util.logging.Formatter;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.LogRecord;%0A
 import java.util
@@ -550,16 +550,16 @@
 andler;%0A
-
 %0A/**%0A * 
@@ -858,16 +858,41 @@
 Context;
+%0A    Formatter formatter;
 %0A%0A    pu
@@ -1020,24 +1020,65 @@
  = context;%0A
+        formatter = Log.simpleFormatter;%0A
     %7D%0A%0A    @
@@ -1650,15 +1650,129 @@
 ing 
-line) %7B
+message) %7B%0A        LogRecord record = new LogRecord(Level.FINE, message);%0A        String line = formatter.format(record);
 %0A   
@@ -2798,24 +2798,24 @@
 .println();%0A
-
         %7D%0A  
@@ -2837,11 +2837,162 @@
 );%0A    %7D
+%0A%0A    public void clear() %7B%0A        SQLiteDatabase db = this.getWritableDatabase();%0A        db.delete(TABLE_LOG, null, null);%0A        db.close();%0A    %7D
 %0A%7D%0A

Format the lines in the database log to include the time and level, just like normal logs