package w.soulofpluto.camera;

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


public class DataBaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 3;
    private static final String DATABASE_NAME = "camera.db";
    private Context mContext;
    private static final String REG_TABLE     = " camera";
    private static final String _ID           = "_id";
    private static final String SKU           = "sku";
    private static final String FILENAME      = "filename";
    private static final String STATUS        = "status";

    private static final String CREATE_CAMERA = "CREATE TABLE" + REG_TABLE + "" +
            "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + SKU + " TEXT NOT NULL,"
            + FILENAME + " TEXT NOT NULL," + STATUS + " INTEGER"  + ")";
    public DataBaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db){
      db.execSQL(CREATE_CAMERA);
    }
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion){
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS camera");
        //db.execSQL("DROP TABLE IF EXISTS quote_table");
        onCreate(db);
    }

    public boolean insertcamera(String sku, String filename, int status){
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put(SKU,      sku);
        contentValues.put(FILENAME, filename);
        contentValues.put(STATUS,   status);
        db.insert(REG_TABLE, null, contentValues);
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        return true;
    }

    public Cursor getData(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from camera ", null );
        Cursor Resultcursor = db.rawQuery("select * from camera " + " ORDER BY " + _ID + " DESC", null);
        /*Cursor c = db.query("select * from camera ", null, null, null, null, null, _ID+" DESC");
        Cursor Resultcursor = sql.rawQuery("select * from camera ORDER by ID DESC,null);*/
        return Resultcursor;
    }

    public Cursor queryLastEvents(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.query("camera", null, null, null, null, null, "sku ASC", "10");
        return res;
    }

    public void deleteData(){
       /* SQLiteDatabase db = this.getReadableDatabase();
        db.delete(REG_TABLE, null, null);
        db.close();*/
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(REG_TABLE, null, null);
        db.close();
    }

    public boolean updatecamera(int id,String sku, String filename, int status){
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put(SKU,      sku);
        contentValues.put(FILENAME, filename);
        contentValues.put(STATUS,   status);
        db.update(REG_TABLE, contentValues, _ID + " = ?", new String[]{"" + id});
        System.out.println("update ====>"+ db.update("camera", contentValues, _ID + " = ?", new String[]{"" + id}));
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        return true;
    }

    public boolean deleteContact(int id){
        SQLiteDatabase db = this.getReadableDatabase();
        db.beginTransaction();
        db.delete(REG_TABLE, _ID	+ "	= ?", new String[] { String.valueOf(id)});
        System.out.println("Delete ===>"+db.delete(REG_TABLE, _ID	+ "	= ?", new String[] { String.valueOf(id)}));
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        return true;
    }

    public int gettotalCount(){
        int _id = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        //Cursor Resultcursor = db.rawQuery("select _id from camera " + " ORDER BY " + _ID + " DESC", "where limit 1");

         //below one index based query
         Cursor cr = db.rawQuery("select _id from camera  ORDER BY "+ _ID  +" DESC LIMIT 1",null);
         if(cr.moveToLast()){
            _id = cr.getInt(0);
         }
        /*long count = DatabaseUtils.queryNumEntries(db, REG_TABLE);*/

        db.close();

        return _id;
    }
}
