package net.testing.databasemanipulation.feature;

//import android.database.sqlite.SQLiteOpenHelper;

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

import org.json.JSONArray;
import org.json.JSONObject;

public class DBManipulate extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "elevi.db";
    private static final String TABLE_NAME = "tblElevi";


    public DBManipulate(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String _query = "CREATE TABLE IF NOT EXISTS tblElevi(ID INTEGER PRIMARY KEY, Nume TEXT, Materie TEXT, Nota REAL);";

        db.execSQL(_query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String _query = "CREATE TABLE IF NOT EXISTS tblElevi(ID INTEGER PRIMARY KEY, Nume TEXT, Materie TEXT, Nota REAL);";

        db.execSQL(_query);
        onCreate(db);
    }

    public void createTblElevi() {
        SQLiteDatabase db = getWritableDatabase();
        String _query = "CREATE TABLE IF NOT EXISTS tblElevi(ID INTEGER PRIMARY KEY, Nume TEXT, Materie TEXT, Nota REAL);";

        db.execSQL(_query);
        db.close();
        Log.d("createTBL","CREATED");
    }

    public void addElev(String nume, String materie, Float nota) {

        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("Nume",nume);
        contentValues.put("Materie",materie);
        contentValues.put("Nota",nota);

        db.insert(TABLE_NAME,null,contentValues);

        db.close();

    }

    public void updateElev(Long id, String nume, String materie, Float nota) {

        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("Nume",nume);
        contentValues.put("Materie",materie);
        contentValues.put("Nota",nota);

        db.update("tblElevi", contentValues, "ID="+id, null);

        db.close();

    }

    public void deleteElev(Long id) {

        SQLiteDatabase db = getWritableDatabase();

        String table = "tblElevi";
        String whereClause = "ID=?";
        String[] whereArgs = new String[] { String.valueOf(id) };
        db.delete(table, whereClause, whereArgs);

        db.close();

    }

    public JSONArray getElevi()
    {

        SQLiteDatabase myDataBase = getWritableDatabase();

        String searchQuery = "SELECT  * FROM tblElevi;";
        Cursor cursor = myDataBase.rawQuery(searchQuery, null );

        JSONArray resultSet     = new JSONArray();

        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {

            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();

            for( int i=0 ;  i< totalColumn ; i++ )
            {
                if( cursor.getColumnName(i) != null )
                {
                    try
                    {
                        if( cursor.getString(i) != null )
                        {
                            Log.d("TAG_NAME", cursor.getString(i) );
                            rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                        }
                        else
                        {
                            rowObject.put( cursor.getColumnName(i) ,  "" );
                        }
                    }
                    catch( Exception e )
                    {
                        Log.d("TAG_NAME", e.getMessage()  );
                    }
                }
            }
            resultSet.put(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
        Log.d("TAG_NAME", resultSet.toString() );
        return resultSet;
    }

}
