package test.pkg;

import android.content.ContentProvider;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.RemoteException;

@SuppressWarnings("UnusedDeclaration")
public class CursorTest {
    public void error1(SQLiteDatabase db, long route_id) {
        Cursor cursor = db.query("TABLE_TRIPS",
                new String[]{"KEY_TRIP_ID"},
                "ROUTE_ID=?",
                new String[]{Long.toString(route_id)},
                null, null, null);
    }

    public int error2(SQLiteDatabase db, long route_id, String table, String whereClause, String id) {
        int total_deletions = 0;
        Cursor cursor = db.query("TABLE_TRIPS",
                new String[]{"KEY_TRIP_ID"},
                "ROUTE_ID=?",
                new String[]{Long.toString(route_id)},
                null, null, null);

        while (cursor.moveToNext()) {
            total_deletions += db.delete(table, whereClause + "=?",
                    new String[]{Long.toString(cursor.getLong(0))});
        }

        // Not closed!
        //cursor.close();

        total_deletions += db.delete(table, id + "=?", new String[]{Long.toString(route_id)});

        return total_deletions;
    }

    public int ok(SQLiteDatabase db, long route_id, String table, String whereClause, String id) {
        int total_deletions = 0;
        Cursor cursor = db.query("TABLE_TRIPS",
                new String[]{
                        "KEY_TRIP_ID"},
                "ROUTE_ID" + "=?",
                new String[]{Long.toString(route_id)},
                null, null, null);

        while (cursor.moveToNext()) {
            total_deletions += db.delete(table, whereClause + "=?",
                    new String[]{Long.toString(cursor.getLong(0))});
        }
        cursor.close();

        return total_deletions;
    }

    public Cursor getCursor(SQLiteDatabase db) {
        @SuppressWarnings("UnnecessaryLocalVariable")
        Cursor cursor = db.query("TABLE_TRIPS",
                new String[]{
                        "KEY_TRIP_ID"},
                "ROUTE_ID" + "=?",
                new String[]{Long.toString(5)},
                null, null, null);

        return cursor;
    }

    void testProviderQueries(Uri uri, ContentProvider provider, ContentResolver resolver,
                             ContentProviderClient client) throws RemoteException {
        Cursor query = provider.query(uri, null, null, null, null);
        Cursor query2 = resolver.query(uri, null, null, null, null);
        Cursor query3 = client.query(uri, null, null, null, null);
    }

    void testProviderQueriesOk(Uri uri, ContentProvider provider, ContentResolver resolver,
                               ContentProviderClient client) throws RemoteException {
        Cursor query = provider.query(uri, null, null, null, null);
        Cursor query2 = resolver.query(uri, null, null, null, null);
        Cursor query3 = client.query(uri, null, null, null, null);
        query.close();
        query2.close();
        query3.close();
    }
}