package com.example.service03;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;


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;
 
// http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

public class DatabaseHelper extends SQLiteOpenHelper 
{
 
    // Logcat tag
    private static final String LOG = DatabaseHelper.class.getName();
 
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "DB03";
    
    public static final String ALARM_TABLE = "alarm";
	public static final String COLUMN_ALARM_ID = "_id";
	public static final String COLUMN_ALARM__CREATEDTIME = "created_time";
	public static final String COLUMN_ALARM__MODIFIEDTIME = "modified_time";
	public static final String COLUMN_ALARM_NAME = "alarm_name"; 
	public static final String COLUMN_ALARM_ACTIVE = "alarm_active";	
	public static final String COLUMN_ALARM_FROMDATE = "from_date";
	public static final String COLUMN_ALARM_TODATE = "to_date";
	public static final String COLUMN_ALARM__RULE = "rule";
	public static final String COLUMN_ALARM__INTERVAL = "interval";
	public static final String COLUMN_ALARM__SOUND = "sound";
	public static final String COLUMN_ALARM_TONE = "alarm_tone";
	public static final String COLUMN_ALARM_VIBRATE = "alarm_vibrate";
	
//	public static final String COLUMN_ALARM_TIME = "alarm_time";
//	public static final String COLUMN_ALARM_DAYS = "alarm_days";
//	public static final String COLUMN_ALARM_DIFFICULTY = "alarm_difficulty";
   
   /* private static final String CREATE_TABLE_ALARM1 = "CREATE TABLE "
										            + ALARM_TABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY," 
										    		+ KEY_TODO + " TEXT," 
										            + KEY_STATUS + " INTEGER,"
										            + KEY_CREATED_AT   + " TEXT" + ")";  // DATETIME
   */
    private static final String CREATE_TABLE_ALARM02 = "CREATE TABLE "
    												+ 	ALARM_TABLE + "(" 
    												+ 	COLUMN_ALARM_ID + " INTEGER PRIMARY KEY," 
    												+ 	COLUMN_ALARM__CREATEDTIME + " INTEGER," 
    												+ 	COLUMN_ALARM__MODIFIEDTIME + " INTEGER," 
													+	COLUMN_ALARM_NAME  + " TEXT," 
													+	COLUMN_ALARM_ACTIVE + " TEXT," 
													+	COLUMN_ALARM_FROMDATE + " DATE," 
													+	COLUMN_ALARM_TODATE + " DATE," 
													+	COLUMN_ALARM__RULE + " TEXT," 
													+	COLUMN_ALARM__INTERVAL + " TEXT," 
													+	COLUMN_ALARM__SOUND + " INTEGER," 
													+	COLUMN_ALARM_TONE + " TEXT," 
													+	COLUMN_ALARM_VIBRATE + " TEXT," 
													+ ")";  // DATETIME
    
    
    private static final String CREATE_TABLE_ALARM = "CREATE TABLE "
            + ALARM_TABLE + "(" 
    		+ COLUMN_ALARM_ID + " INTEGER PRIMARY KEY," 
            + COLUMN_ALARM__CREATEDTIME  + " INTEGER," 
    		+ COLUMN_ALARM__MODIFIEDTIME + " INTEGER," 
            + COLUMN_ALARM_NAME + " TEXT" + ")";
 
    public DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(CREATE_TABLE_ALARM);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + ALARM_TABLE);
        onCreate(db);
    }
 
     /**
     * Creating 
     */
    
/*    
    ALARM_TABLE = "alarm";
	COLUMN_ALARM_ID = "_id";
	COLUMN_ALARM__CREATEDTIME = "created_time";
	COLUMN_ALARM__MODIFIEDTIME = "modified_time";
	COLUMN_ALARM_NAME = "alarm_name"; 
	COLUMN_ALARM_ACTIVE = "alarm_active";	
	COLUMN_ALARM_FROMDATE = "from_date";
	COLUMN_ALARM_TODATE = "to_date";
	COLUMN_ALARM__RULE = "rule";
	COLUMN_ALARM__INTERVAL = "interval";
	COLUMN_ALARM__SOUND = "sound";
	COLUMN_ALARM_TONE = "alarm_tone";
	COLUMN_ALARM_VIBRATE = "alarm_vibrate";
*/	
	
    public long createToDo(Alarm obj) 
    {
	        SQLiteDatabase db = this.getWritableDatabase();
	 
	        ContentValues values = new ContentValues();
	        
	        long now = System.currentTimeMillis();
	        values.put(COLUMN_ALARM__CREATEDTIME, now);
	        values.put(COLUMN_ALARM__MODIFIEDTIME, now);
	        values.put(COLUMN_ALARM_NAME, obj.get_nom());
	        
	        
			//cv.put(COL_NAME, name==null ? "" : name);
			//cv.put(COL_FROMDATE, fromDate);
			//cv.put(COL_TODATE, toDate);
			//cv.put(COL_RULE, rule);
			//cv.put(COL_INTERVAL, interval);
			//cv.put(COL_SOUND, sound ? 1 : 0);
	        //values.put(KEY_TODO, "ghfgd");
	        //values.put(KEY_STATUS, 55);
	        //values.put(KEY_CREATED_AT, "getDateTime");
	 
	        // insert row
	        long id = db.insert(ALARM_TABLE, null, values);
	        return id;
    }
 
   
    public Cursor listNotifications()
    {
		//String selection = "am."+AlarmMsg.COL_STATUS+" != '"+AlarmMsg.CANCELLED+"'";
		//selection += (args!=null && args.length>0 && args[0]!=null) ? " AND am."+AlarmMsg.COL_DATETIME+" >= "+args[0] : "";
		//selection += (args!=null && args.length>1 && args[1]!=null) ? " AND am."+AlarmMsg.COL_DATETIME+" <= "+args[1] : "";		
		/*
		String sql = Util.concat(listSQL,
								" WHERE "+selection,
								" ORDER BY am."+AlarmMsg.COL_DATETIME+" ASC");

		return db.rawQuery(sql, null);*/
		
		
		 String selectQuery = "SELECT  * FROM " + ALARM_TABLE;
		 Log.e(LOG, selectQuery);
		 SQLiteDatabase db = this.getWritableDatabase();
	     Cursor c = db.rawQuery(selectQuery, null);
		 return c;
	}
    
   
    public List<Alarm> getAll() 
    {
        List<Alarm> todos = new ArrayList<Alarm>();
        String selectQuery = "SELECT  * FROM " + ALARM_TABLE;
 
        Log.e(LOG, selectQuery);
 
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                Alarm td = new Alarm();
                Integer aaa = c.getInt(c.getColumnIndex(COLUMN_ALARM__CREATEDTIME));
                String bbb = c.getString(c.getColumnIndex(COLUMN_ALARM_NAME));
                //td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
 
                // adding to todo list
                //todos.add(td);
            } while (c.moveToNext());
        }
 
        return todos;
    }
 
  
 
   
   
 
 
    
}