// Simple database to save all the profiles.
//
// I'll need to do something like this:
// http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
//
// Instead of a Contacts record, I'll be reading and writing preferences
// to the database so I can save multiple preference sets as "Profiles".

package my.lan.zooty.upwithphp;

import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.util.Log;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.ContentValues;
import android.database.Cursor;
import java.util.List;
import java.util.ArrayList;

public class Profiles extends SQLiteOpenHelper {

   private static final String TAG = "Profiles";
   private static final int DATABASE_VERSION = 1;
   private static final String DATABASE_NAME = "profiles";
   private static final String PROFILE_TABLE_NAME = "profile";
   private static final String PROFILE_TABLE_CREATE =
            "CREATE TABLE " + PROFILE_TABLE_NAME + " (" +
            "ProfileName  TEXT PRIMARY KEY, " +
            "UploadURL    TEXT, " +
            "DowncaseName INT, " +
            "FormData     TEXT " +
            ");";

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

   @Override
   public void onCreate(SQLiteDatabase db) {
      try {
         db.execSQL(PROFILE_TABLE_CREATE);
      } catch (SQLException e) {
         Log.e(TAG, "error creating profiles database: ", e);
      }
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // PUT CODE HERE - do something here when I have implemented more
      // than one database version.
   }

   public void saveProfile(SharedPreferences prefs) {
      try {
         SQLiteDatabase db = this.getWritableDatabase();
         ContentValues values = new ContentValues();
         values.put("ProfileName",
                    prefs.getString("profile_name_edit_key", ""));
         values.put("UploadURL",
                    prefs.getString("upload_host_edit_key", ""));
         values.put("DowncaseName",
                    prefs.getBoolean("downcase_name_checkbox_key", true));
         values.put("FormData",
                    prefs.getString("form_data_edit_key", ""));
         db.insertWithOnConflict(PROFILE_TABLE_NAME, null, values,
                                 SQLiteDatabase.CONFLICT_REPLACE);
         db.close();
      } catch (SQLException e) {
         Log.e(TAG, "error writing profile: ", e);
      }
   }

   public void removeProfile(String profile_name) {
      SQLiteDatabase db = this.getWritableDatabase();
      db.delete(PROFILE_TABLE_NAME, "ProfileName=?",
                new String [] {profile_name});
      db.close();
   }

   public void loadProfile(SharedPreferences prefs, String profile_name) {
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery("SELECT * FROM " + PROFILE_TABLE_NAME +
                                  " WHERE ProfileName = '" +
                                  profile_name + "';", null);
      if (cursor.moveToFirst()) {
         SharedPreferences.Editor ed = prefs.edit();
         ed.putString("profile_name_edit_key", cursor.getString(0));
         ed.putString("upload_host_edit_key", cursor.getString(1));
         ed.putBoolean("downcase_name_checkbox_key", cursor.getInt(2) != 0);
         ed.putString("form_data_edit_key", cursor.getString(3));
         ed.commit();
      }
      cursor = null;
      db.close();
   }

   public List<String> getAllProfiles() {
      List<String> rval = new ArrayList<String>();
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery("SELECT * FROM " + PROFILE_TABLE_NAME +
                                  " ORDER BY ProfileName ASC", null);
      if (cursor.moveToFirst()) {
         do {
            rval.add(cursor.getString(0));
         } while (cursor.moveToNext());
      }
      return rval;
   }
}
