package de.frajul.endlessroll.sqlDatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import de.frajul.endlessroll.main.GameLog;

/**
 * Created by Julian on 07.06.2016.
 */
public abstract class DatabaseAdapter extends SQLiteOpenHelper {

    private SQLTable[] tables;
    private SQLiteDatabase database;

    public DatabaseAdapter(Context context, String databaseName, int version) {
        super(context, databaseName, null, version);
        createColumns();
        this.tables = createTables();
    }

    protected abstract void createColumns();

    protected abstract SQLTable[] createTables();

    @Override
    public void onCreate(SQLiteDatabase db) {
        GameLog.i("DATABASE CREATE");
        for (SQLTable table : tables)
            db.execSQL(table.getSqlCreateTable());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (SQLTable table : tables)
            db.execSQL(table.getSqlDeleteTable());
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

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

    protected void deleteTableEntry(SQLTable table, SQLTableColumn c0, int c0Condition, SQLTableColumn c1, int c1Condition) {
        database.execSQL("DELETE FROM " + table.getName() + " WHERE " + c0.getKey() + " = " + c0Condition + " AND " + c1.getKey() + " = " + c1Condition);
    }

    protected void clearTable(SQLTable table) {
        database.execSQL(table.getSqlClearTable());
    }

    protected long insertData(SQLTable table, ContentValues data) {
        return database.insert(table.getName(), null, data);
    }

    protected long update(SQLTable table, ContentValues data, SQLTableColumn c0, int c0Condition, SQLTableColumn c1, int c1Condition) {
        int reached = database.delete(table.getName(), c0.getKey() + "=" + c0Condition + " and " + c1.getKey() + "=" + c1Condition, null);
        return insertData(table, data);
    }

    protected long update(SQLTable table, ContentValues data, SQLTableColumn c0, int c0Condition) {
        int reached = database.delete(table.getName(), c0.getKey() + "=" + c0Condition, null);
        return insertData(table, data);
    }

    protected Cursor getCursor(SQLTable table) {
        return database.rawQuery(table.getSqlGetCursor(), null);
    }

    protected Cursor getCursor(SQLTable table, SQLTableColumn c0, int c0Condition, SQLTableColumn c1, int c1Condition) {
        return database.rawQuery(table.getSqlGetCursor() + " where " + c0.getKey() + "=" + c0Condition + " and " + c1.getKey() + "=" + c1Condition, null);
    }

    protected Cursor getCursor(SQLTable table, SQLTableColumn c, int cCondition) {
        return database.rawQuery(table.getSqlGetCursor() + " where " + c.getKey() + "=" + cCondition, null);
    }


}
