package es.craftsmanship.toledo.katangapp.db;

import es.craftsmanship.toledo.katangapp.db.model.Favorite;

import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Manuel de la Peña
 */
public class FavoriteDAO {

    public FavoriteDAO(Context context) {
        dbHelper = new FavoritesContract(context);
    }

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

    public Favorite createFavorite(String busStopId) {
        ContentValues values = new ContentValues();

        values.put(FavoritesContract.Favorite.COLUMN_NAME_BUS_STOP_ID, busStopId);

        long insertId = database.insert(FavoritesContract.Favorite.TABLE_NAME, null, values);

        Cursor cursor = database.query(
            FavoritesContract.Favorite.TABLE_NAME, allColumns,
            FavoritesContract.Favorite._ID + " = " + insertId, null, null, null, null);

        cursor.moveToFirst();

        Favorite favorite = cursorToFavorite(cursor);

        cursor.close();

        return favorite;
    }

    public void deleteFavorite(Favorite favorite) {
        String[] whereArgs = { favorite.getBusStopId() };

        database.delete(
            FavoritesContract.Favorite.TABLE_NAME,
            FavoritesContract.Favorite.COLUMN_NAME_BUS_STOP_ID + " = ?", whereArgs);
    }

    public List<Favorite> getAllFavorites() {
        List<Favorite> favorites = new ArrayList<>();

        Cursor cursor = database.query(
            FavoritesContract.Favorite.TABLE_NAME, allColumns, null, null, null, null, null);

        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            Favorite favorite = cursorToFavorite(cursor);

            favorites.add(favorite);

            cursor.moveToNext();
        }

        cursor.close();

        return favorites;
    }

    public Favorite getFavorite(String favoriteId) {
        String selection = FavoritesContract.Favorite.COLUMN_NAME_BUS_STOP_ID + "=?";
        String[] selectionArgs = {favoriteId};

        try (Cursor cursor = database.query(
                FavoritesContract.Favorite.TABLE_NAME, allColumns, selection, selectionArgs,
                null, null, null)) {

            cursor.moveToFirst();

            if (!cursor.isAfterLast()) {
                return cursorToFavorite(cursor);
            }
        }

        return null;
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    private Favorite cursorToFavorite(Cursor cursor) {
        Favorite favorite = new Favorite();

        favorite.setId(cursor.getLong(0));
        favorite.setBusStopId(cursor.getString(1));

        return favorite;
    }

    private String[] allColumns = {
        FavoritesContract.Favorite._ID,
        FavoritesContract.Favorite.COLUMN_NAME_BUS_STOP_ID
    };
    private SQLiteDatabase database;
    private FavoritesContract dbHelper;

}
@@ -853,32 +853,37 @@
 lues);%0A%0A        
+try (
 Cursor cursor = 
@@ -902,32 +902,36 @@
 ry(%0A            
+   
+ 
 FavoritesContrac
@@ -965,16 +965,20 @@
 olumns,%0A
+    
         
@@ -1047,35 +1047,41 @@
 ull, null, null)
-;%0A%0A
+) %7B%0A%0A    
         cursor.m
@@ -1087,32 +1087,36 @@
 moveToFirst();%0A%0A
+    
         Favorite
@@ -1158,37 +1158,16 @@
 );%0A%0A    
-    cursor.close();%0A%0A
         
@@ -1175,32 +1175,43 @@
 eturn favorite;%0A
+        %7D%0A%0A
     %7D%0A%0A    publi
@@ -1589,16 +1589,21 @@
         
+try (
 Cursor c
@@ -1618,32 +1618,36 @@
 database.query(%0A
+    
             Favo
@@ -1722,17 +1722,19 @@
 l, null)
-;
+) %7B
 %0A%0A      
@@ -1727,32 +1727,36 @@
 ll)) %7B%0A%0A        
+ 
+   
 cursor.moveToFir
@@ -1754,32 +1754,36 @@
 moveToFirst();%0A%0A
+    
         while (!
@@ -1810,32 +1810,36 @@
 ) %7B%0A            
+    
 Favorite favorit
@@ -1873,32 +1873,36 @@
 );%0A%0A            
+  
+  
 favorites.add(fa
@@ -1915,32 +1915,36 @@
 );%0A%0A            
+    
 cursor.moveToNex
@@ -1948,34 +1948,37 @@
 Next();%0A        
+ 
+   
 %7D
-%0A
 %0A        cursor.
@@ -1974,23 +1974,9 @@
     
-cursor.close();
+%7D
 %0A%0A  

Apply try-with-resources to DAO