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="Alarm12";
    public static final String TABLE = "table_alarm";
    public static final String TABLE_APPOINTMENT = "table_appointment";
    
    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_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 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_STATUS + " TEXT," 
	            + COL_CREATEDTIME + " INTEGER," 
	            + COL_MODIFIEDTIME + " INTEGER" + ")";
	
/*	 
	 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 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);


    }

    @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);
        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 long  insert_appoitment(String titre, long date, long startTime, long endTime, String phone, String notes, 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_CREATEDTIME, now );
			vals.put(COL_MODIFIEDTIME, now );
			vals.put(COL_STATUS, status);
			long id = db.insert(TABLE_APPOINTMENT, 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 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 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();
    }
    */
}


