package com.example.db01;


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

public class DB_alarm extends SQLiteOpenHelper 
{
	//define all the constants
    public static final String DATABASE_NAME="Alarm02";
    public static final String TABLE = "table_alarm";
    public static final int DATABASE_VERSION = 1;
    
    public static final String KEY_ID = "_id";
    public static final String COL_NAME = "name";
    public static final String COL_FROMDATE = "from_date";
	public static final String COL_TODATE = "to_date";
	public static final String COL_DATETIME = "datetime";
	public static final String COL_STATUS = "status";
	public static final String COL_CREATEDTIME = "created_time";
	public static final String COL_MODIFIEDTIME = "modified_time";
	public static final String COL_RULE = "rule";
	public static final String COL_INTERVAL = "interval";
	public static final String COL_SOUND = "sound";
	public static final String KEY_CREATED_AT = "created_at";
    	
	public  static final String CREATE_TABLE = "CREATE TABLE "
	            + TABLE + "(" 
	    		+ KEY_ID + " INTEGER PRIMARY KEY,"
	    		+ COL_NAME + " TEXT," 
	    		+ COL_FROMDATE + " DATE," 
	    		+ COL_TODATE + " DATE," 
	    		+ COL_DATETIME + " INTEGER," 
	    		+ COL_STATUS + " TEXT," 
	            + COL_CREATEDTIME + " INTEGER," 
	            + COL_MODIFIEDTIME + " INTEGER,"
	            + KEY_CREATED_AT + " DATETIME" + ")";
	
    public DB_alarm(Context context) 
    {
    	//create the database
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    	//create the table
        db.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
    	//onUpgrade remove the existing table, and recreate and populate new data
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE);
        onCreate(db);
    }

    
    public long  insert_alarm(String NAME, String FROMDATE, String TODATE, long DATETIME, String STATUS, long CREATEDTIME, long MODIFIEDTIME)
    {
    	
    		SQLiteDatabase db = this.getWritableDatabase();
			ContentValues vals = new ContentValues();
			long now = System.currentTimeMillis();
			vals.put(COL_NAME, NAME);
			vals.put(COL_FROMDATE, FROMDATE );
			vals.put(COL_TODATE, TODATE );
			vals.put(COL_DATETIME, DATETIME);
			vals.put(COL_STATUS, STATUS);
			vals.put(COL_CREATEDTIME, CREATEDTIME );
			vals.put(COL_MODIFIEDTIME, MODIFIEDTIME );
			
			long id = db.insert(TABLE, null, vals);
			return id;
    	
    }
    
    public Cursor DB_query(String query) 
    {
    	SQLiteDatabase db = this.getWritableDatabase();
    	
    	// 1481758200000
    	//String req = "SELECT * from " +TABLE_DEBUG;
    	
    	    	
	   	 	Cursor cursor = db.rawQuery(query, null);
	   	 	if (cursor != null)
	   	 	{
	   	 		cursor.moveToFirst();
	   	 	}
	   	 	return cursor;
    }
    
    
    public void update(String query)
    {
    	
    	SQLiteDatabase db = this.getWritableDatabase();
    	
    	/*
    	// OK
    	String id = "6";
    	ContentValues contentValues = new ContentValues();
    	contentValues.put(COL_NAME, "Patoche");
    	db.update(TABLE, contentValues, KEY_ID + " = ?", new String[]{id});
    	*/
    	
    	/* OK */ 
    	db.execSQL(query);
    	    	
	   
    }
    
    public void delete() 
    {
    	
        SQLiteDatabase db = this.getWritableDatabase();
        
        String id = "6";
        
        db.delete(TABLE, KEY_ID + " = ?", new String[] { String.valueOf(6) });
        db.close();
    }
    
    
    
   /* 
    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
     
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
        values.put(KEY_PH_NO, contact.getPhoneNumber());
     
        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }
   */ 
    /*
    public void deleteContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }
    */
}


