package io.github.sjakthol.stoptimes.db.task;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import io.github.sjakthol.stoptimes.db.StopListContract;
import io.github.sjakthol.stoptimes.db.StopListDatabaseHelper;
import io.github.sjakthol.stoptimes.utils.AsyncTaskResult;
import io.github.sjakthol.stoptimes.utils.Logger;

/**
 * A task for updating favorite status of a stop. Use the non-varargs variant of
 * .execute() to execute the update.
 */
public class UpdateFavoriteStatusTask extends DatabaseTask<Bundle, Void> {
    private static final String TAG = UpdateFavoriteStatusTask.class.getSimpleName();
    private static final String BUNDLE_STOP_ID = "BUNDLE_STOP_ID";
    private static final String BUNDLE_IS_FAVORITE = "BUNDLE_IS_FAVORITE";

    /**
     * Create a DatabaseTask for the given StopListDatabaseHelper.
     *
     * @param dbHelper the database helper instance
     */
    protected UpdateFavoriteStatusTask(StopListDatabaseHelper dbHelper) {
        super(dbHelper);
    }

    /**
     * Set the favorite status of the given stop. Use this method to execute the task.
     *
     * @param stopId the GTFS ID of the stop to modify
     * @param isFavorite the value to set for is_favorite column
     */
    public AsyncTask<Bundle, Void, AsyncTaskResult<Void>> execute(String stopId, boolean isFavorite) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_STOP_ID, stopId);
        bundle.putBoolean(BUNDLE_IS_FAVORITE, isFavorite);
        return this.execute(bundle);
    }

    @Override
    public Void runTask(SQLiteDatabase db, Bundle... params) {
        boolean isFavorite = params[0].getBoolean(BUNDLE_IS_FAVORITE);
        String stopId = params[0].getString(BUNDLE_STOP_ID);

        long changed = -1;
        if (isFavorite) {
            Logger.i(TAG, "Adding %s from favorites", stopId);

            // Prepare data
            ContentValues values = new ContentValues();
            values.put(StopListContract.Stop.COLUMN_NAME_GTFS_ID, stopId);

            // Insert
            changed = db.insert(StopListContract.Stop.FAVORITES_TABLE_NAME, null, values);

        } else {
            Logger.i(TAG, "Removing %s from favorites", stopId);

            // Prepare deletion
            String where = StopListContract.Stop.COLUMN_NAME_GTFS_ID + " = ?";
            String[] whereArgs = {stopId};

            // Delete
            changed = db.delete(StopListContract.Stop.FAVORITES_TABLE_NAME, where, whereArgs);
        }

        if (changed != 1) {
            Logger.w(TAG, "Unexpected number of columns changed %d columns", changed);
        }

        return null;
    }
}

@@ -1826,35 +1826,8 @@
 );%0A%0A
-        long changed = -1;%0A
     
@@ -1881,28 +1881,26 @@
  %22Adding %25s 
-from
+to
  favorites%22,
@@ -2099,34 +2099,24 @@
 %0A           
- changed =
  db.insert(S
@@ -2446,16 +2446,21 @@
         
+long 
 changed 
@@ -2534,27 +2534,21 @@
 eArgs);%0A
-    
+%0A
     
-%7D%0A%0A
         
@@ -2575,24 +2575,28 @@
             
+ 
+   
 Logger.w(TAG
@@ -2623,33 +2623,28 @@
  of 
-columns chang
+rows delet
 ed
+:
  %25d 
-column
+row
 s%22, 
@@ -2649,24 +2649,38 @@
 , changed);%0A
+            %7D%0A
         %7D%0A%0A 

Fix favorite update task logging