package se.emilsjolander.sprinkles;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

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

/**
 * Transaction represents a database transaction in sprinkles.
 * Transactions are past as parameters to certain methods in a model such as save() and delete().
 */
public final class Transaction {

	interface OnTransactionCommittedListener {
		void onTransactionCommitted();
        void onTransactionRollback();
	}

	private SQLiteDatabase mDb;
	private boolean mSuccessful;
	private List<OnTransactionCommittedListener> mOnTransactionCommittedListeners = new ArrayList<OnTransactionCommittedListener>();

	public Transaction() {
		mDb = Sprinkles.getDatabase();
		mDb.beginTransaction();
	}

    /**
     * Mark a transaction as successful before calling finish() to commit the transaction.
     * @param successful
     *      Whether or not the transaction was successful
     */
	public void setSuccessful(boolean successful) {
		mSuccessful = successful;
	}

    /**
     * Just a getting for the successful property
     *
     * @return if the transaction is marked as successful or not
     */
	public boolean isSuccessful() {
		return mSuccessful;
	}

    /**
     * Finish the transaction.
     * This will commit or rollback the transaction depending on whether is was marked as successful or not
     */
	public void finish() {
		if (mSuccessful) {
			mDb.setTransactionSuccessful();
		}
		mDb.endTransaction();

		if (mSuccessful) {
			for (OnTransactionCommittedListener listener : mOnTransactionCommittedListeners) {
				listener.onTransactionCommitted();
			}
		}else {
            for (OnTransactionCommittedListener listener : mOnTransactionCommittedListeners) {
                listener.onTransactionRollback();
            }
        }
	}

	long insert(ModelInfo table, ContentValues values) {
        DataResolver.assureTableExist(table);
		return mDb.insert(table.tableName, null, values);
	}

	int update(ModelInfo table, ContentValues values, String where) {
        DataResolver.assureTableExist(table);
		return mDb.update(table.tableName, values, where, null);
	}

	int delete(ModelInfo table, String where) {
		return mDb.delete(table.tableName, where, null);
	}

	void addOnTransactionCommittedListener(OnTransactionCommittedListener listener) {
		mOnTransactionCommittedListeners.add(listener);
	}



}

@@ -1834,16 +1834,23 @@
  %7D%0A%09%7D%0A%0A%09
+public 
 long ins
@@ -1890,24 +1890,24 @@
 s values) %7B%0A
-
         Data
@@ -1993,24 +1993,31 @@
 lues);%0A%09%7D%0A%0A%09
+public 
 int update(M
@@ -2180,16 +2180,23 @@
 );%0A%09%7D%0A%0A%09
+public 
 int dele
@@ -2286,17 +2286,24 @@
 l);%0A%09%7D%0A%0A
-
 %09
+public 
 void add

make some method public for easier extension