package com.androidbegin.buttonclicktutorial;


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="Alarm31";
    public static final int DATABASE_VERSION = 1;

    public static final String TABLE = "table_alarm";
    public static final String TABLE_APPOINTMENT = "table_appointment";
    
    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_ALARM = "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 static final String COL_TITRE = "titre";
	 public static final String COL_DATE = "date";
	 public static final String COL_START_TIME = "start_time";
	 public static final String COL_END_TIME = "end_time";
	 public static final String COL_PHONE = "phone";
	 public static final String COL_NOTES = "notes";
	 //public static final String COL_OPTION_DELAI = "option_delai";
	 public static final String COL_DELAI = "delai";
	 public static final String COL_OPTION_VIBRATE = "option_vibrate";
	 public static final String COL_OPTION_RINGTONE = "option_ringtone";
				
	
	 public  static final String CREATE_TABLE_APPOINTMENT = "CREATE TABLE "
	            + TABLE_APPOINTMENT + "(" 
	    		+ KEY_ID + " INTEGER PRIMARY KEY,"
	    		+ COL_TITRE + " TEXT," 
	    		+ COL_DATE + " INTEGER,"
	    		+ COL_START_TIME + " INTEGER," 
	    		+ COL_END_TIME + " INTEGER," 
	    		+ COL_PHONE + " TEXT," 
	    		+ COL_NOTES + " TEXT," 
	    		+ COL_DELAI + " INTEGER," 
	    		+ COL_OPTION_VIBRATE + " TEXT," 
	    		+ COL_OPTION_RINGTONE + " TEXT," 
	    		+ COL_STATUS + " TEXT," 
	            + COL_CREATEDTIME + " INTEGER," 
	            + COL_MODIFIEDTIME + " INTEGER" + ")";
	 
	 //  	    		+ COL_OPTION_DELAI + " TEXT," 

	
/*	 
	 public  static final String CREATE_TABLE_APPOINTMENT = "CREATE TABLE "
	            + TABLE_APPOINTMENT + "(" 
	    		+ KEY_ID + " INTEGER PRIMARY KEY,"
	    		+ COL_TITRE + " TEXT," 
	    		+ COL_STARTDATE + " INTEGER," 
	    		+ COL_ENDTIME + " INTEGER," 
	    		+ COL_STATUS + " TEXT," 
	            + COL_CREATEDTIME + " INTEGER," 
	            + COL_MODIFIEDTIME + " INTEGER,"
	            + KEY_CREATED_AT + " DATETIME" + ")";
*/	
	 
		public static final String ACTIVE = "A";
		public static final String EXPIRED = "E";
		public static final String CANCELLED = "cancel";
		public static final String DEFERRED = "D";
 
		
		public static final String TABLE_REPEAT = "table_repeat";
		public static final String COLUMN_REPEAT_ID = "_id";
		public static final String COLUMN_REPEAT_NAME = "repeat_name";	 
		public static final String COLUMN_REPEAT_TIME = "repeat_time";
		public static final String COLUMN_REPEAT_DAYS = "repeat_days";
		public static final String COLUMN_REPEAT_INTERVAL = "repeat_interval";
		public static final String COLUMN_REPEAT_ACTIVE = "alarm_active";	
		public static final String COLUMN_REPEAT_VIBRATE = "option_vibrate";
		public static final String COLUMN_REPEAT_RINGTONE = "option_ringtone";
		public static final String COLUMN_REPEAT_CREATED_TIME = "created_time";
		public static final String COLUMN_REPEAT_MODIFIED_TIME = "modified_time";

		
		 public  static final String CREATE_TABLE_REPEAT = "CREATE TABLE "
		            + TABLE_REPEAT + "(" 
		    		+ COLUMN_REPEAT_ID + " INTEGER PRIMARY KEY,"
		    		+ COLUMN_REPEAT_NAME + " TEXT," 
		    		+ COLUMN_REPEAT_TIME + " INTEGER,"
		    		+ COLUMN_REPEAT_DAYS + " TEXT,"
		    		+ COLUMN_REPEAT_INTERVAL + " TEXT," 
		    		+ COLUMN_REPEAT_ACTIVE + " TEXT," 
		    		+ COLUMN_REPEAT_VIBRATE + " TEXT," 
		    		+ COLUMN_REPEAT_RINGTONE + " TEXT," 
		            + COLUMN_REPEAT_CREATED_TIME + " INTEGER," 
		            + COLUMN_REPEAT_MODIFIED_TIME + " INTEGER" + ")";
		
		
	
	 
	
    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_ALARM);
        db.execSQL(CREATE_TABLE_APPOINTMENT);
        db.execSQL(CREATE_TABLE_REPEAT);


    }

    @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);
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_APPOINTMENT);
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_REPEAT);
        
        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;
    	
    }
    
//  				        	long id = db.insert_appoitment(titre, date, startTime, endTime, string_phone , string_notes , option_delai , option_delai_valeur ,  option_vibrate , "actif");

    public long  insert_appoitment(String titre, long date, long startTime, long endTime, String phone, String notes, long option_delai_valeur ,  String option_vibrate, String option_ringtone ,String status)
    {
    	
    		SQLiteDatabase db = this.getWritableDatabase();
			ContentValues vals = new ContentValues();
			long now = System.currentTimeMillis();
			vals.put(COL_TITRE, titre);
			vals.put(COL_DATE, date);
			vals.put(COL_START_TIME, startTime );
			vals.put(COL_END_TIME, endTime );
			vals.put(COL_PHONE, phone );
			vals.put(COL_NOTES, notes );
			
			
			//vals.put(COL_OPTION_DELAI, option_delai );
			vals.put(COL_DELAI, option_delai_valeur );
			vals.put(COL_OPTION_VIBRATE, option_vibrate );
			vals.put(COL_OPTION_RINGTONE, option_ringtone );
			
			
			vals.put(COL_CREATEDTIME, now );
			vals.put(COL_MODIFIEDTIME, now );
			vals.put(COL_STATUS, status);
			long id = db.insert(TABLE_APPOINTMENT, null, vals);
			return id;
			
    }
    
    
    public long  insert_repeat(String titre, long time,  String days_list , String inverval, String status, String option_vibrate, String option_ringtone )
    {
    	
    	
   	/*
        + TABLE_REPEAT + "(" 
		+ COLUMN_REPEAT_ID + " INTEGER PRIMARY KEY,"
		+ COLUMN_REPEAT_NAME + " TEXT," 
		+ COLUMN_REPEAT_TIME + " INTEGER,"
		+ COLUMN_REPEAT_DAYS + " TEXT,"
		+ COLUMN_REPEAT_INTERVAL + " TEXT," 
		+ COLUMN_REPEAT_ACTIVE + " TEXT," 
		+ COLUMN_REPEAT_VIBRATE + " TEXT," 
		+ COLUMN_REPEAT_RINGTONE + " TEXT," 
        + COLUMN_REPEAT_CREATED_TIME + " INTEGER," 
        + COLUMN_REPEAT_MODIFIED_TIME + " INTEGER" + ")";

   	 */
		 
		 
   			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues vals = new ContentValues();
			long now = System.currentTimeMillis();
			vals.put(COLUMN_REPEAT_NAME, titre);
			vals.put(COLUMN_REPEAT_TIME, time);
			vals.put(COLUMN_REPEAT_DAYS, days_list );
			vals.put(COLUMN_REPEAT_INTERVAL, inverval );
			vals.put(COLUMN_REPEAT_ACTIVE, status);
			vals.put(COLUMN_REPEAT_VIBRATE,  option_vibrate );
			vals.put(COLUMN_REPEAT_RINGTONE, option_ringtone );
			vals.put(COLUMN_REPEAT_CREATED_TIME, now );
			vals.put(COLUMN_REPEAT_MODIFIED_TIME, now );
			
			long id = db.insert(TABLE_REPEAT, null, vals);
			return id;
			
   }
    
    public long  xxxxxxupdate_appoitment(String titre, long date, long startTime, long endTime, String status)
    {
    	
    		SQLiteDatabase db = this.getWritableDatabase();
			ContentValues vals = new ContentValues();
			long now = System.currentTimeMillis();
			vals.put(COL_TITRE, titre);
			vals.put(COL_DATE, date);
			vals.put(COL_START_TIME, startTime );
			vals.put(COL_END_TIME, endTime );
			vals.put(COL_CREATEDTIME, now );
			vals.put(COL_MODIFIEDTIME, now );
			vals.put(COL_STATUS, status);
			long id = db.insert(TABLE_APPOINTMENT, null, vals);
			return id;
			
    }
    
   
    
    public void update_appoitment(String id, ContentValues contentValues)
    {
    	
       	SQLiteDatabase db = this.getWritableDatabase();
    	db.update(TABLE_APPOINTMENT, contentValues, KEY_ID + " = ?", new String[]{id});
	   
    }
    
    public void update_repeat(String id, ContentValues contentValues)
    {
    	SQLiteDatabase db = this.getWritableDatabase();
    	db.update(TABLE_REPEAT, contentValues, KEY_ID + " = ?", new String[]{id});
    }
    
    
    public void lulu(int index)
    {
    	
    	SQLiteDatabase db = this.getWritableDatabase();

    	Cursor cursor = db.query(DB_alarm.TABLE_APPOINTMENT, null, DB_alarm.KEY_ID+" = ?", new String[]{String.valueOf(index)}, null, null, null);
		try 
		{
			if (cursor.moveToFirst()) 
			{
			}
			
		} 
		finally
		{
			cursor.close();
		}

        
    }
    
    
    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 insertbis(Alarm id)
    {
    	SQLiteDatabase db = this.getWritableDatabase();
    	
    	id.getName();
    	id.getFromDate();
    	id.getToDate();
    	String at = id.getAt();
    	
    	
		
    	
    	/*
    		ContentValues cv = new ContentValues();
    		long now = System.currentTimeMillis();
    		cv.put(COL_CREATEDTIME, now);
    		cv.put(COL_MODIFIEDTIME, now);
    		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);
    		
    		return db.insert(TABLE_NAME, null, cv);
    	*/
    }
    
    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(String id) 
    {
    	
        SQLiteDatabase db = this.getWritableDatabase();
        
       // String id = "6";
        
//       db.delete(TABLE, KEY_ID + " = ?", new String[] { String.valueOf(6) });
//        db.close();
        
        
        //db.delete("table", "_id = ?", new String[] {String.valueOf(id)});
        
        //String s = "DELETE FROM AddressList WHERE item_id=" + id;
        //db.execSQL(s);
    
//       db.execSQL("DELETE FROM table_alarm WHERE _id ='id'");
        
        db.execSQL("DELETE FROM table_alarm WHERE _id='" + id + "'");
        
        
    }
    
    
    public void delete_appointment(String id) 
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM table_appointment WHERE _id='" + id + "'");
    }
    
    public void delete_repeat(String id) 
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM table_repeat WHERE _id='" + id + "'");
    }
    
    
    
    public void cancelNotification(String id) 
    {
		
        SQLiteDatabase db = this.getWritableDatabase();
    	ContentValues cv = new ContentValues();
		cv.put(COL_STATUS, CANCELLED);
    	db.update(TABLE_APPOINTMENT, cv, KEY_ID + " = ?", new String[]{id});

		
		/*
		return db.update(AlarmMsg.TABLE_NAME, 
							cv, 
							(isAlarmId ? AlarmMsg.COL_ALARMID : AlarmMsg.COL_ID)+" = ?", 
							new String[]{String.valueOf(id)});
		*/					
	}
    
    
    
   /* 
    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();
    }
    */
}


