package com.example.fueltrack;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class FuelDbAdapter {

  public static final int FUEL_DB_VERSION = 1;
  public static final String FUEL_DB_NAME = "fuel.db";
  public static final String FUEL_TAB_NAME = "fueldata";
  public static final String KEY_ID = "_id";
  public static final String KEY_DATE = "date";
  public static final String KEY_DISTANCE = "distance";
  public static final String KEY_FUEL = "fuel";
  public static final String KEY_COST = "cost";
  private final Context context;
  private SQLiteDatabase db;
  private FuelDbOpenHelper dbHelper;

  public FuelDbAdapter(Context _context) {
    this.context = _context;
    dbHelper = new FuelDbOpenHelper(context, FUEL_DB_NAME, null,
        FUEL_DB_VERSION);
  }

  public void open() throws SQLiteException {
    try {
      db = dbHelper.getWritableDatabase();
    }
    catch (SQLiteException ex) {
      db = dbHelper.getReadableDatabase();
    }
  }

  public void close() {
    db.close();
  }

  public float[] mostRecentMileageFromDb() {
    float prevDistance = 0f;
    float distance = 0f;
    float fuel = 0f;
    String query = "SELECT " + KEY_DISTANCE + ", " + KEY_FUEL + " FROM "
        + FUEL_TAB_NAME + " ORDER BY " + KEY_ID + " DESC LIMIT 2";
    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {
      distance = c.getFloat(c.getColumnIndex(KEY_DISTANCE));
      fuel = c.getFloat(c.getColumnIndex(KEY_FUEL));
      if (c.moveToNext()) { // have at least 2 records
        prevDistance = c.getFloat(c.getColumnIndex(KEY_DISTANCE));
        distance = distance - prevDistance;
      }
      else { // not enough data yet.
        distance = 0f;
        fuel = 0f;
      }
    }
    return new float[] {
        distance, fuel
    };
  }

  public Cursor getMileageList(String[] projection, String selection,
      String[] selectionArgs, String orderBy) {
    if (orderBy == null) {
      orderBy = KEY_DATE + " DESC";
    }
    return db.query(FUEL_TAB_NAME, projection, selection, selectionArgs, null,
        null, orderBy);
  }

  public Cursor getMileageList(String sortKey) {
    String orderBy = sortKey;
    if (orderBy == null) {
      orderBy = KEY_DATE + " DESC";
    }
    return getMileageList(null, null, null, orderBy);
  }

  public Cursor getMileageList() {
    return getMileageList(null);
  }

  public Cursor getMileageRecord(long id) {
    return db.query(FUEL_TAB_NAME, null, KEY_ID + " = ?", new String[] {
      "" + id
    }, null, null, null);
  }

  public long addMileage(String date, float distance, float fuel, float cost) {
    ContentValues newdata = new ContentValues();
    newdata.put(KEY_DATE, date);
    newdata.put(KEY_DISTANCE, distance);
    newdata.put(KEY_FUEL, fuel);
    newdata.put(KEY_COST, cost);
    return db.insert(FUEL_TAB_NAME, null, newdata);
  }

  public long addMileage(ContentValues content) {
    return db.insert(FUEL_TAB_NAME, null, content);
  }

  public long updateMileage(long _rowid, String date, float distance,
      float fuel, float cost) {
    ContentValues newdata = new ContentValues();
    if (date != null) newdata.put(KEY_DATE, date);
    if (distance > 0) newdata.put(KEY_DISTANCE, distance);
    if (fuel > 0) newdata.put(KEY_FUEL, fuel);
    if (cost > 0) newdata.put(KEY_COST, cost);
    return db.update(FUEL_TAB_NAME, newdata, KEY_ID + "= " + _rowid, null);
  }

  public long updateMileage(long _rowid, ContentValues content) {
    return db.update(FUEL_TAB_NAME, content, KEY_ID + "= " + _rowid, null);
  }

  public long updateMileage(ContentValues content, String selection,
      String[] selectionArgs) {
    return db.update(FUEL_TAB_NAME, content, selection, selectionArgs);
  }

  public long deleteMileage(long _rowid) {
    return db.delete(FUEL_TAB_NAME, KEY_ID + "= " + _rowid, null);
  }

  public long deleteMileage(String selection, String[] selectionArgs) {
    return db.delete(FUEL_TAB_NAME, selection, selectionArgs);
  }

  private static class FuelDbOpenHelper extends SQLiteOpenHelper {
    public static final String CREATE_FUEL_TAB = "CREATE TABLE "
        + FUEL_TAB_NAME + " ( " + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_DATE
        + " TEXT, " + KEY_DISTANCE + " REAL, " + KEY_FUEL + " REAL, "
        + KEY_COST + " REAL );";

    public FuelDbOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
      super(context, name, factory, version);
    }

    public FuelDbOpenHelper(Context context) {
      super(context, FUEL_DB_NAME, null, FUEL_DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
      _db.execSQL(CREATE_FUEL_TAB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
    }
  } // class MileageDBOpenHelper

}