package com.andela.currencycalculator.model.dbparser;

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

import com.andela.currencycalculator.model.currency.Currency;

import java.util.ArrayList;

/**
 * Created by Spykins on 20/02/2016.
 */
public class DbHandler extends SQLiteOpenHelper implements DbConfig.FeedEntry {


    public DbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_BASE_CURRENCY + " TEXT, " +
                COLUMN_CURRENCY + " TEXT , " +
                COLUMN_EXCHANGE_RATE + " DOUBLE, " +
                COLUMN_DATE + " DATE " +
                ");";

        db.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS  " + TABLE_NAME);
        onCreate(db);
    }

    public void insertCurrencyInDatabase(Currency currency) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_BASE_CURRENCY, currency.getBaseCurrency());
        values.put(COLUMN_CURRENCY, currency.getCurrency());
        values.put(COLUMN_EXCHANGE_RATE, currency.getExchangeRate());
        values.put(COLUMN_DATE, currency.getDateCreated());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();

    }

    public ArrayList<Currency> readCurrencyFromDatabase() {
        ArrayList<Currency> allCurrencyInDataBase = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();


        String query = "SELECT * FROM " + TABLE_NAME;
        Cursor cursorHandle = db.rawQuery(query, null);
        cursorHandle.moveToFirst();
        while (cursorHandle.moveToNext()) {
            String baseCurrency = cursorHandle.getString(cursorHandle.getColumnIndex(COLUMN_BASE_CURRENCY));
            String currency = cursorHandle.getString(cursorHandle.getColumnIndex(COLUMN_CURRENCY));
            double exchangeRate = cursorHandle.getDouble(cursorHandle.getColumnIndex(COLUMN_EXCHANGE_RATE));
            allCurrencyInDataBase.add(new Currency(baseCurrency, exchangeRate, currency));
        }
        cursorHandle.close();
        db.close();
        return allCurrencyInDataBase;
    }

    public boolean hasDataBase(Context context) {
        SQLiteDatabase checkDB = null;
        try {
            String db_full_path = context.getDatabasePath(DATABASE_NAME).getPath();
            checkDB = SQLiteDatabase.openDatabase(db_full_path, null,
                    SQLiteDatabase.OPEN_READONLY);
            checkDB.close();
        } catch (SQLiteException e) {
            // database doesn't exist yet.
        }
        return checkDB != null;
    }

    public void updateDatabase(String currencyCode, Double newValue){

        SQLiteDatabase sq = getWritableDatabase();
        String selection = COLUMN_CURRENCY + " like ? ";
        String args[] = {currencyCode};
        ContentValues values = new ContentValues();
        values.put(COLUMN_CURRENCY,newValue);
        sq.update(TABLE_NAME,values,selection,args);

    }
}

@@ -3078,51 +3078,8 @@
 ) %7B%0A
-            // database doesn't exist yet.%0A
     
@@ -3191,18 +3191,18 @@
 ewValue)
+ 
 %7B
-%0A
 %0A       
@@ -3264,238 +3264,173 @@
 ing 
-selection = COLUMN_CURRENCY + %22 like ? %22;%0A        String args%5B%5D = %7BcurrencyCode%7D;%0A        ContentValues values = new ContentValues();%0A        values.put(COLUMN_CURRENCY,newValue);%0A        sq.update(TABLE_NAME,values,selection,args
+query = %22UPDATE %22 + TABLE_NAME + %22 %22 +%0A                %22SET %22 + COLUMN_EXCHANGE_RATE + %22 = %22 + newValue + %22 WHERE %22 + COLUMN_ID + %22 = %22 + 0;%0A        sq.execSQL(query
 );%0A%0A

Rewrite the Update query in Db handler to fix Db update bug