package com.nicola.monitor_10;

/**
 * Created by nicola on 27/10/16.
 */

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



public class DbManager {

    private DBhelper dbhelper;

    public DbManager(Context ctx)
    {
        dbhelper=new DBhelper(ctx);
    }

    public void save(double lum, double mov, double s, boolean c, boolean l)
    {
        SQLiteDatabase db=dbhelper.getWritableDatabase();

        ContentValues cv=new ContentValues();
        cv.put(DatabaseStrings.FIELD_LIGHT, lum);
        cv.put(DatabaseStrings.FIELD_MOVEMENT, mov);
        cv.put(DatabaseStrings.FIELD_SOUND, s);
        cv.put(DatabaseStrings.FIELD_CHARGING,c);
        cv.put(DatabaseStrings.FIELD_LOCKED,l);
        cv.put(DatabaseStrings.FIELD_TIME,this.getTime());

        try
        {
            db.insert(DatabaseStrings.TBL_NAME, null,cv);
            Log.i("DBMANAGER","Nuovo salvataggio avvenuto con successo");
        }
        catch (SQLiteException sqle)
        {
            sqle.printStackTrace();
        }

    }

    //todo
    public boolean delete(long id)
    {
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        try
        {
            if (db.delete(DatabaseStrings.TBL_NAME, DatabaseStrings.FIELD_ID+"=?", new String[]{Long.toString(id)})>0)
                return true;
            return false;
        }
        catch (SQLiteException sqle)
        {
            return false;
        }

    }

    //todo
    public Cursor query() {
        Cursor crs=null;
        try
        {
            SQLiteDatabase db=dbhelper.getReadableDatabase();
            crs=db.query(DatabaseStrings.TBL_NAME, null, null, null, null, null, null, null);
        }
        catch(SQLiteException sqle)
        {
            return null;
        }
        return crs;
    }

    public void changeState(boolean state){

        SQLiteDatabase db=dbhelper.getWritableDatabase();

        ContentValues cv=new ContentValues();
        cv.put(DatabaseStrings.FIELD_STATE, state);
        cv.put(DatabaseStrings.FIELD_TIME,this.getTime());

        try
        {
            db.insert(DatabaseStrings.TBL_NAME_2, null,cv);
            Log.i("DBMANAGER","Cambio di stato salvato con successo");
        }
        catch (SQLiteException sqle)
        {
            sqle.printStackTrace();
        }
    }

    private String getTime(){
        java.util.Calendar c = java.util.Calendar.getInstance();
        java.util.Date d = c.getTime();
        return d.toString();
    }

}
@@ -1599,35 +1599,25 @@
   %7D%0A%0A    %7D%0A%0A
-    //todo
 %0A
+
     public C
@@ -1757,83 +1757,160 @@
     
-crs=db.query(DatabaseStrings.TBL_NAME, null, null, null, null, null, null, 
+String myQuery = %22SELECT * FROM %22 + DatabaseStrings.TBL_NAME + %22 order by %22 + DatabaseStrings.FIELD_ID  + %22 DESC %22;%0A            crs=db.rawQuery(myQuery,
 null

query in modo decrescente con il metodo rawquery