package org.commcare.models.database;

import android.content.ContentValues;

import org.commcare.modern.database.DatabaseIndexingUtils;
import org.commcare.modern.util.Pair;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import java.util.Set;

/**
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class IndexedFixturePathUtils {
    private final static String INDEXED_FIXTURE_INDEX_TABLE = "IndexedFixtureIndex";
    private final static String INDEXED_FIXTURE_INDEX_COL_NAME = "name";
    private final static String INDEXED_FIXTURE_INDEX_COL_BASE = "base";
    private final static String INDEXED_FIXTURE_INDEX_COL_CHILD = "child";

    public static Pair<String, String> lookupIndexedFixturePaths(SQLiteDatabase db,
                                                                 String fixtureName) {
        Cursor c = db.query(INDEXED_FIXTURE_INDEX_TABLE,
                new String[]{INDEXED_FIXTURE_INDEX_COL_BASE, INDEXED_FIXTURE_INDEX_COL_CHILD},
                INDEXED_FIXTURE_INDEX_COL_NAME + "=?", new String[]{fixtureName}, null, null, null);
        try {
            if (c.getCount() == 0) {
                return null;
            } else {
                c.moveToFirst();
                return Pair.create(
                        c.getString(c.getColumnIndexOrThrow(INDEXED_FIXTURE_INDEX_COL_BASE)),
                        c.getString(c.getColumnIndexOrThrow(INDEXED_FIXTURE_INDEX_COL_CHILD)));
            }
        } finally {
            c.close();
        }
    }

    public static void insertIndexedFixturePathBases(SQLiteDatabase db, String fixtureName,
                                                     String baseName, String childName) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(INDEXED_FIXTURE_INDEX_COL_BASE, baseName);
        contentValues.put(INDEXED_FIXTURE_INDEX_COL_CHILD, childName);
        contentValues.put(INDEXED_FIXTURE_INDEX_COL_NAME, fixtureName);

        try {
            db.beginTransaction();

            long ret = db.insertOrThrow(INDEXED_FIXTURE_INDEX_TABLE,
                    INDEXED_FIXTURE_INDEX_COL_BASE,
                    contentValues);

            if (ret > Integer.MAX_VALUE) {
                throw new RuntimeException("Waaaaaaaaaay too many values");
            }

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    public static void createStorageBackedFixtureIndexTable(SQLiteDatabase db) {
        String createStatement = "CREATE TABLE IF NOT EXISTS " +
                INDEXED_FIXTURE_INDEX_TABLE +
                " (" + INDEXED_FIXTURE_INDEX_COL_NAME +
                ", " + INDEXED_FIXTURE_INDEX_COL_BASE +
                ", " + INDEXED_FIXTURE_INDEX_COL_CHILD + ");";
        db.execSQL(createStatement);

        db.execSQL(DatabaseIndexingUtils.indexOnTableCommand("fixture_name_index",
                INDEXED_FIXTURE_INDEX_TABLE, INDEXED_FIXTURE_INDEX_COL_NAME));
    }

    public static void buildFixtureIndices(SQLiteDatabase database,
                                           String tableName,
                                           Set<String> indices) {
        try {
            database.beginTransaction();
            for (String indexStmt : DatabaseIndexingUtils.getIndexStatements(tableName, indices)) {
                database.execSQL(indexStmt);
            }
            database.setTransactionSuccessful();
        } finally {
            database.endTransaction();
        }
    }
}

@@ -130,16 +130,82 @@
 gUtils;%0A
+import org.commcare.modern.database.IndexedFixturePathsConstants;%0A
 import o
@@ -429,314 +429,8 @@
 ls %7B
-%0A    private final static String INDEXED_FIXTURE_INDEX_TABLE = %22IndexedFixtureIndex%22;%0A    private final static String INDEXED_FIXTURE_INDEX_COL_NAME = %22name%22;%0A    private final static String INDEXED_FIXTURE_INDEX_COL_BASE = %22base%22;%0A    private final static String INDEXED_FIXTURE_INDEX_COL_CHILD = %22child%22;
 %0A%0A  
@@ -626,16 +626,45 @@
 b.query(
+IndexedFixturePathsConstants.
 INDEXED_
@@ -713,16 +713,45 @@
 tring%5B%5D%7B
+IndexedFixturePathsConstants.
 INDEXED_
@@ -774,16 +774,45 @@
 L_BASE, 
+IndexedFixturePathsConstants.
 INDEXED_
@@ -845,32 +845,61 @@
                 
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -1189,32 +1189,61 @@
 umnIndexOrThrow(
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -1312,32 +1312,61 @@
 umnIndexOrThrow(
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -1718,32 +1718,61 @@
 ntentValues.put(
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -1816,32 +1816,61 @@
 ntentValues.put(
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -1924,16 +1924,45 @@
 ues.put(
+IndexedFixturePathsConstants.
 INDEXED_
@@ -2090,16 +2090,45 @@
 OrThrow(
+IndexedFixturePathsConstants.
 INDEXED_
@@ -2160,32 +2160,61 @@
                 
+IndexedFixturePathsConstants.
 INDEXED_FIXTURE_
@@ -2606,483 +2606,160 @@
     
-String c
+db.execSQL(IndexedFixtu
 re
+P
 at
-eStatement = %22CREATE TABLE IF NOT EXISTS %22 +%0A                INDEXED_FIXTURE_INDEX_TABLE +%0A                %22 (%22 + INDEXED_FIXTURE_INDEX_COL_NAME +%0A                %22, %22 + INDEXED_FIXTURE_INDEX_COL_BASE +%0A                %22, %22 + INDEXED_FIXTURE_INDEX_COL_CHILD + %22);%22;%0A        db.execSQL(createStatement);%0A%0A        db.execSQL(DatabaseIndexingUtils.indexOnTableCommand(%22fixture_name_index%22,%0A                INDEXED_FIXTURE_INDEX_TABLE, INDEXED_FIXTURE_INDEX_COL_NAME)
+hsConstants.INDEXED_FIXTURE_INDEX_TABLE_STMT);%0A        db.execSQL(IndexedFixturePathsConstants.INDEXED_FIXTURE_INDEXING_STMT
 );%0A 

Consolidate fixture paths table constants