package com.androidbegin.buttonclicktutorial;


import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;




import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;
import android.widget.Toast;


public class remind extends ListActivity
{
	TextView rangeText;
	
	Calendar cal = Calendar.getInstance();
	String[] monthArr = new String[]{ "Month","Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"  };
	Date dt = new Date();

	public static final String PREFERENCES_NAME = "MyPrefsFile";
	
	SharedPreferences sp;
	DB_alarm db;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    
    		super.onCreate(savedInstanceState);
    		//requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.reminder_layout);
    		
            ActionBar actionBar = getActionBar();
            getActionBar().setHomeButtonEnabled(true);
            getActionBar().setDisplayHomeAsUpEnabled(true);
            setTitle("Listing des rendez-vous");

    		
    		db = new DB_alarm(getApplicationContext());
    		
	    	PreferenceManager.setDefaultValues(this, R.xml.settings, false);
			sp = PreferenceManager.getDefaultSharedPreferences(this);
    		
    		rangeText = (TextView) findViewById(R.id.range_tv);

        
    		rangeText.setText("sfffffssf");
    		
			int r = Tools.getDateRange();
			
			switch(r)
			{
				case 3: // Yearly
					cal.set(Calendar.MONTH, 0);
					
				case 2: // Monthly
					cal.set(Calendar.DATE, 1);
					
				case 1: // Weekly
					if (r==1)
					{	cal.set(Calendar.DATE, cal.getFirstDayOfWeek());
						//cal.set(Calendar.DATE, 1);
					}
					
				case 0: // Daily
			        cal.set(Calendar.HOUR_OF_DAY, 0);
			    	cal.set(Calendar.MINUTE, 0);
					cal.set(Calendar.SECOND, 0);
					cal.set(Calendar.MILLISECOND, 0);
			}

			registerForContextMenu(getListView());


    }    
    
    
	private String move(int step) 
	{
		switch(Tools.getDateRange()) 
		{
			case 0:
				cal.add(Calendar.DATE, 1*step); // jour
				break;		
			case 1:
				cal.add(Calendar.DATE, 7*step); // semaine = 7 jours
				break;
			case 2:
				cal.add(Calendar.MONTH, 1*step); // mois
				cal.set(Calendar.DATE, 1);  // premier jour du mois   
				
				break;
			case 3:
				cal.add(Calendar.YEAR, 1*step); // année
				break;			
		}
		return "";
	}    

	
	private String getRangeStr()
	{
			int date = cal.get(Calendar.DATE);
			int month = cal.get(Calendar.MONTH);
			int year = cal.get(Calendar.YEAR);
			dt.setTime(System.currentTimeMillis());
			
			switch(Tools.getDateRange()) 
			{
			//Calendar.get(Calendar.YEAR)
			
				case 0: // Daily
					if (date==dt.getDate() && month==dt.getMonth() && year==dt.getYear()+1900) 
					{	return "Today";
					
					}
					else
					{	
						return date + " " + monthArr[month+1] + " " + year ;
					}	
					
				case 1: // Weekly
					
					 
					 String aaa = monthArr[month+1];
					 String bbb =  move(+1);
					 int ccc = cal.get(Calendar.DATE);
					 String ddd = monthArr[cal.get(Calendar.MONTH)+1];
					 String eee	= move(-1); 
					String fff = date + " " + monthArr[month+1] + move(+1) + " - " + cal.get(Calendar.DATE) + " " + monthArr[cal.get(Calendar.MONTH)+1] + move(-1) + " " + year;
					return date + " " + monthArr[month+1] + move(+1) + " - " + cal.get(Calendar.DATE) + " " + monthArr[cal.get(Calendar.MONTH)+1] + move(-1) + " " + year;
					
				case 2: // Monthly
					return monthArr[month+1]+" " + year;
					
				case 3: // Yearly
					return year+"";
			}
			return null;
	}

	
	private Cursor createCursor()
	{
		String TAG = " ****************** ";
		
		String requete = "";
		
		int range = Tools.getDateRange();
		
		// granularite jour
		if(range == 0)
		{
	 		long temps = cal.getTimeInMillis();
			requete = "SELECT * FROM table_appointment WHERE date = " + temps  ;
			//String requete = "SELECT * FROM table_appointment WHERE _id = " + id ;
		 	
		}
		
		else
		{
	 		String max = cal.getTimeInMillis() + move(+1);
	 		String min = cal.getTimeInMillis() + move(-1);
	 		
	 		Calendar cal_tmp = Calendar.getInstance();
 		
	 		long d1 = Long.parseLong(max);
			cal_tmp.setTimeInMillis(d1);
			Date dt1 = cal_tmp.getTime();
			Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String s1 = formatter.format(dt1);
			
			long d2 = Long.parseLong(min);
			cal_tmp.setTimeInMillis(d2);
			Date dt2 = cal_tmp.getTime();
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String s2 = formatter.format(dt2);
	
			Log.i(TAG, s1);
			Log.i(TAG, s2);
			
			requete = "SELECT * FROM table_appointment WHERE date >= " + max + " AND date <= " + min + " ORDER BY date ASC" ;
		    
	    
		}
		
		
		Cursor mCursor = db.DB_query(requete);
		int val = mCursor.getCount();
	    Log.i(TAG, String.valueOf(val));
	    return  mCursor;   


		
	}	
	

	
	protected void onResume555() 
	{
			super.onResume();
			
			SharedPreferences pref = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0); // 0 - for private mode
			Editor editor = pref.edit();
			
			String DATE_RANGE_CHANGE = pref.getString("DATE_RANGE_CHANGE", null); 
			Toast.makeText(getApplicationContext(), DATE_RANGE_CHANGE, Toast.LENGTH_LONG).show();
			
			
			SimpleCursorAdapter adapter = new SimpleCursorAdapter(
					this, 
					R.layout.row02, 
					createCursor(), 
					new String[]{ "titre" , "date", "start_time", "end_time", "end_time" }, 
					new int[]{R.id.msg_tv, R.id.year_tv, R.id.month_tv, R.id.date_tv, R.id.time_tv}, 0);

// http://custom-android-dn.blogspot.fr/2013/01/listviewis-view-group-that-displays.html	
// https://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text
			// http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
			
			adapter.setViewBinder(new ViewBinder() 
			{
				public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
				{
					if (view.getId() == R.id.msg_tv) return false;

					TextView tv = (TextView)view;
					long timestamp = cursor.getLong(columnIndex);
					//dt.setTime(timestamp);
					
					Calendar cal_tmp = Calendar.getInstance();
					cal_tmp.setTimeInMillis(timestamp);
					Date dt = cal_tmp.getTime();
					
					int jour = cal_tmp.get(Calendar.DATE);
					String mois = monthArr[cal_tmp.get(Calendar.MONTH) +1 ];
					int annee = cal_tmp.get(Calendar.YEAR);
					
					switch(view.getId()) 
					{
							case R.id.year_tv:
								//tv.setText(String.valueOf(dt.getYear()+1900));
								tv.setText(String.valueOf(annee));
								break;
							case R.id.month_tv:
								//tv.setText(monthArr[dt.getMonth()+1]);
								tv.setText(mois);
								break;
							case R.id.date_tv:
								//tv.setText(String.valueOf(dt.getDate()));
								tv.setText(String.valueOf(jour));
								break;
							case R.id.time_tv:
								//long now = System.currentTimeMillis();
								//String txt = RemindMe.showRemainingTime() ? Util.getRemainingTime(time, now) : Util.getActualTime(dt.getHours(), dt.getMinutes());
								//if (TextUtils.isEmpty(txt)) txt = Util.getActualTime(dt.getHours(), dt.getMinutes());
								//tv.setText(txt);
								
								//RelativeLayout parent = (RelativeLayout)tv.getParent();
								//TextView tv2 = (TextView) parent.findViewById(R.id.msg_tv);
								//if (time < now) tv2.setTextColor(Color.parseColor("#555555"));
								//else tv2.setTextColor(Color.parseColor("#587498"));
								
								String option = "";
								
								Boolean option_alarme = Tools.showRemainingTime();
								
								// delai avant l'alarme
								if(option_alarme)
								{
									long now = System.currentTimeMillis();
									String delai = Tools.getRemainingTime(timestamp, now); 
									option = delai;
								}
								
								// heure de l'alarme
								if(!option_alarme)
								{
									//String heure_alarme = Tools.getActualTime(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
									
									int heure_format24h = cal_tmp.get(Calendar.HOUR_OF_DAY);
									// pas utile  int heure_format12h = cal_tmp.get(Calendar.HOUR);
									int minute = cal_tmp.get(Calendar.MINUTE);
									
									/*  format pour afficher les minutes */
									String str_minute = "";
									if(minute == 0) 
									{
									    str_minute = "00";
									}
									if(minute <= 9)
									{
									    str_minute = "0"  + Integer.toString(minute);
									}
									else
									{
										str_minute =  Integer.toString(minute);
									}
									
									/*  format pour afficher les minutes */
									String str_heure = "";
									if(heure_format24h == 0) 
									{
										str_heure = "00";
									}
									if(heure_format24h <= 9)
									{
										str_heure = "0"  + Integer.toString(heure_format24h);
									}
									else
									{
										str_heure =  Integer.toString(heure_format24h);
									}
									
									option = str_heure +" : " + str_minute;
								}
								
								
								tv.setText(option);
								
								break;
					}
					return true;
				}
			});
			
			setListAdapter(adapter);
			
			rangeText.setText(getRangeStr());

			
					
	}
	
	

	protected void onResume() 
	{
			super.onResume();
			
			SharedPreferences pref = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0); // 0 - for private mode
			Editor editor = pref.edit();
			
			String DATE_RANGE_CHANGE = pref.getString("DATE_RANGE_CHANGE", null); 
			Toast.makeText(getApplicationContext(), DATE_RANGE_CHANGE, Toast.LENGTH_LONG).show();
			
			
			SimpleCursorAdapter adapter = new SimpleCursorAdapter(
					this, 
					R.layout.row05, 
					createCursor(), 
					new String[]{ "titre" , "date", "start_time", "end_time", "end_time" }, 
					new int[]{R.id.msg_tv, R.id.date_tv, R.id.time_tv}, 0);

// http://custom-android-dn.blogspot.fr/2013/01/listviewis-view-group-that-displays.html	
// https://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text
			// http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
			//http://www.mysamplecode.com/2013/03/android-simplecursoradapter-alternate-row-color.html
			


//android:layout_toLeftOf
//android:layout_toRightOf
//			android:layout_alignParentRight="true"
//			        android:layout_centerVertical="true"
//			 android:layout_below="@+id/tvCity"
//				        android:layout_alignLeft="@+id/tvCity"
//				        		 " android:layout_toRightOf="@+id/list_image"
//        android:layout_marginLeft="75dip"
       
			// http://www.tutos-android.com/comprendre-relativelayout-android
			
			adapter.setViewBinder(new ViewBinder() 
			{
				public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
				{
					if (view.getId() == R.id.msg_tv) return false;

					TextView tv = (TextView)view;
					long timestamp = cursor.getLong(columnIndex);
					//dt.setTime(timestamp);
					
					Calendar cal_tmp = Calendar.getInstance();
					cal_tmp.setTimeInMillis(timestamp);
					Date dt = cal_tmp.getTime();
					
					int jour = cal_tmp.get(Calendar.DATE);
					String mois = monthArr[cal_tmp.get(Calendar.MONTH) +1 ];
					int annee = cal_tmp.get(Calendar.YEAR);
					
					switch(view.getId()) 
					{
							case R.id.date_tv:
								String sjour = String.valueOf(jour);
								String sannee = String.valueOf(annee);
								String aqw = sjour + " " + mois + " " + sannee;
								tv.setText(aqw);
								break;
								
							case R.id.time_tv:
								//long now = System.currentTimeMillis();
								//String txt = RemindMe.showRemainingTime() ? Util.getRemainingTime(time, now) : Util.getActualTime(dt.getHours(), dt.getMinutes());
								//if (TextUtils.isEmpty(txt)) txt = Util.getActualTime(dt.getHours(), dt.getMinutes());
								//tv.setText(txt);
								
								//RelativeLayout parent = (RelativeLayout)tv.getParent();
								//TextView tv2 = (TextView) parent.findViewById(R.id.msg_tv);
								//if (time < now) tv2.setTextColor(Color.parseColor("#555555"));
								//else tv2.setTextColor(Color.parseColor("#587498"));
								
								String option = "";
								
								Boolean option_alarme = Tools.showRemainingTime();
								
								// delai avant l'alarme
								if(option_alarme)
								{
									long now = System.currentTimeMillis();
									String delai = Tools.getRemainingTime(timestamp, now); 
									option = delai;
								}
								
								// heure de l'alarme
								if(!option_alarme)
								{
									//String heure_alarme = Tools.getActualTime(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
									
									int heure_format24h = cal_tmp.get(Calendar.HOUR_OF_DAY);
									// pas utile  int heure_format12h = cal_tmp.get(Calendar.HOUR);
									int minute = cal_tmp.get(Calendar.MINUTE);
									
									/*  format pour afficher les minutes */
									String str_minute = "";
									if(minute == 0) 
									{
									    str_minute = "00";
									}
									if(minute <= 9)
									{
									    str_minute = "0"  + Integer.toString(minute);
									}
									else
									{
										str_minute =  Integer.toString(minute);
									}
									
									/*  format pour afficher les minutes */
									String str_heure = "";
									if(heure_format24h == 0) 
									{
										str_heure = "00";
									}
									if(heure_format24h <= 9)
									{
										str_heure = "0"  + Integer.toString(heure_format24h);
									}
									else
									{
										str_heure =  Integer.toString(heure_format24h);
									}
									
									option = str_heure +" : " + str_minute;
								}
								
								
								tv.setText(option);
								
								break;
					}
					return true;
				}
			});
			
			setListAdapter(adapter);
			
			rangeText.setText(getRangeStr());

			
					
	}
	
    
	public void onClick(View v) 
	{
		switch (v.getId())
		{
		
			case R.id.preferences:
			
				//SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
				//startActivity(new Intent(this, SettingsActivity.class));
				
      			//Intent Intent02 = new Intent(remind.this, SettingsActivity.class);
      			//startActivity(Intent02);
      			
      			Toast.makeText(getApplicationContext(),"Preferences ...",Toast.LENGTH_SHORT).show();
      			//Intent Intent02 = new Intent(remind.this, agenda02.class);
      			//startActivity(Intent02);
      			//return true;


				
			break;		
		
			case R.id.imageButton3:
				move(-1);
				rangeText.setText(getRangeStr());
				((SimpleCursorAdapter)getListAdapter()).changeCursor(createCursor());
				break;
			case R.id.imageButton4:
				move(+1);
				rangeText.setText(getRangeStr());
				((SimpleCursorAdapter)getListAdapter()).changeCursor(createCursor());
				break;			
		}
	}

    
    
    /********************************************************/
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.remind_menu, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	      switch (item.getItemId())
	      {

	      		case android.R.id.home:
	      			this.finish();
	      			return true;

	      
	      		case R.id.add_event:
	      			Toast.makeText(getApplicationContext(),"Add event",Toast.LENGTH_SHORT).show();
	      			//Intent Intent02 = new Intent(remind.this, agenda02.class);
	      			//startActivity(Intent02);
	      			return true;
		     
	      		case R.id.preferences:
	      			Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
	      			Intent Intent01 = new Intent(remind.this, SettingsActivity.class);
	      			startActivity(Intent01);
	      			return true;
		      
	      			
	      			
	      		//case R.id.menu_settings:
		          // Comportement du bouton "Paramètres"
		        //  return true;
		      
		        /*  
	      		case R.id.menu_gui:
	      			Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
	      			Intent Intent03 = new Intent(MainActivity.this, gui.class);
	      			startActivity(Intent03);
			        return true;
		        */  
		       /*   
		          
	      		case R.id.menu_datepicker:
	      			//Toast.makeText(getApplicationContext(),"Remind",Toast.LENGTH_SHORT).show();
	      			Intent Intent04 = new Intent(remind.this, remind.class);
	      			startActivity(Intent04);
			        return true;
		          
	      		case R.id.menu_about:
	      			Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
	      			//Intent Intent03 = new Intent(MainActivity.this, datepicker.class);
	      			//startActivity(Intent03);
			        return true;

	      		case R.id.menu_quit:
	      			//Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
	      			//Intent Intent03 = new Intent(MainActivity.this, datepicker.class);
	      			//startActivity(Intent03);
	      			finish();
			        return true;    
		          */
	      		default:
		          return super.onOptionsItemSelected(item);
		 }
	}

	
	/*****************************************/
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		//String keyword = (String)this.getListAdapter().getItem(position);
		
		 Object o = this.getListAdapter().getItem(position);
		 
		 String keyword = o.toString();
		
		 Object toto = l.getItemAtPosition(position);
		 String value = toto.toString();
		 
		 
		 Cursor c = (Cursor)l.getItemAtPosition(position);
		 String val = c.getString(c.getColumnIndex("titre"));
		 
		 String sval0 = c.getString(c.getColumnIndex(DB_alarm.KEY_ID));
		 String sval1 = c.getString(c.getColumnIndex(DB_alarm.COL_TITRE));
		 String sval2 = c.getString(c.getColumnIndex(DB_alarm.COL_DATE));
		 String sval3 = c.getString(c.getColumnIndex(DB_alarm.COL_START_TIME));
		 String sval4 = c.getString(c.getColumnIndex(DB_alarm.COL_END_TIME));
		 String sval5 = c.getString(c.getColumnIndex(DB_alarm.COL_PHONE));
		 String sval6 = c.getString(c.getColumnIndex(DB_alarm.COL_NOTES));
		 
		 long val0 = Long.parseLong(sval0);
		 long val2 = Long.parseLong(sval2);
		 long val3 = Long.parseLong(sval3);
		 long val4 = Long.parseLong(sval4);
		 
		 
		 
		 
		 
		 
		 Alarm pipo = new Alarm();
		 pipo.setId(val0);
		 pipo.setTitre(sval1); 
		 pipo.setStart_time(val3);
		 pipo.setEnd_time(val4);
		 pipo.setPhone(sval5);
		 pipo.setNotes(sval6);
		 
		 
		 //toto.hashCode()
		 
	    Toast.makeText(remind.this, val, Toast.LENGTH_LONG).show();	
		
	    
		// 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";

	    
	    
		 //openContextMenu(v);
		
		 //Intent intent = new Intent(MainActivity.this, debug.class);
		 //intent.putExtra(EXTRA_POSITION, String.valueOf(position));
		 //intent.putExtra(EXTRA_ID, String.valueOf(id));
		 //startActivity(intent);
	    
	    
	/*	Calendar cal_actif = Calendar.getInstance();
		tmp = date_selection.split(" ");
	    jour = Integer.parseInt(tmp[0]);
	    mois = Integer.parseInt(tmp[1]);
	    annee = Integer.parseInt(tmp[2]);
	    mois = mois - 1;
		cal_actif.set(annee, mois, jour, 0, 0, 0); 
		cal_actif.set(Calendar.MILLISECOND, 0);
        timestamp = cal_actif.getTimeInMillis();
    */
	    
//	    Date dt = copy_dayValueInCells.get(backposition);
//      	String date_event = df.format(dt);
      	
//        String sDate = formatter.format(cal.getTime());

	    
		Calendar cal_tmp = Calendar.getInstance();
		cal_tmp.setTimeInMillis(val2);
      	SimpleDateFormat df = new SimpleDateFormat("dd MM yyyy ");
        String ssssDate = df.format(cal_tmp.getTime());


        int i = (int) id;


		
        Intent intent = new Intent(remind.this, infos_appointment.class);
        intent.putExtra("index", i);
        intent.putExtra("date_selection", ssssDate);
        startActivity(intent);

		
		
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		if (v.getId() == android.R.id.list) 
		{
			getMenuInflater().inflate(R.menu.context_menu, menu); 
			menu.setHeaderTitle("Choose an Option");
			menu.setHeaderIcon(R.drawable.ic_dialog_menu_generic);
			
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			//alarmMsg.setId(info.id);
			//alarmMsg.load(db);
			//if (alarmMsg.getDateTime() < System.currentTimeMillis()) 
			//	menu.removeItem(R.id.menu_edit);
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		boolean refresh = false;
		
		switch (item.getItemId())
		{
				case R.id.menu_edit:
					
					long id = info.id;
					
					//alarmMsg.setId(info.id);
					//alarmMsg.load(db);
					//alarm.reset();
					//alarm.setId(alarmMsg.getAlarmId());
					//alarm.load(db);
					//showDialog(R.id.menu_edit);
					
					startActivity(new Intent(remind.this, test.class));
					
		            //Intent intent = new Intent(remind.this, infos_appointment.class);
		            //intent.putExtra("index", index);
		            //intent.putExtra("date_selection", date_selection);
		            //startActivity(intent);

					
					
					break;
					
				case R.id.menu_delete:
					//dbHelper.cancelNotification(info.id, false);
					refresh = true;
					//Intent cancelThis = new Intent(this, AlarmService.class);
					//cancelThis.putExtra(AlarmMsg.COL_ID, String.valueOf(info.id));
					//cancelThis.setAction(AlarmService.CANCEL);
					//startService(cancelThis);
					Toast.makeText(getApplicationContext(), "Simple", Toast.LENGTH_LONG).show();
					break;
					
				case R.id.menu_delete_repeating:
					//alarmMsg.setId(info.id);
					//alarmMsg.load(db);
					//RemindMe.dbHelper.cancelNotification(db, alarmMsg.getAlarmId(), true);
					refresh = true;
					
					//Intent cancelRepeating = new Intent(this, AlarmService.class);
					//cancelRepeating.putExtra(AlarmMsg.COL_ALARMID, String.valueOf(alarmMsg.getAlarmId()));
					//cancelRepeating.setAction(AlarmService.CANCEL);
					//startService(cancelRepeating);
					Toast.makeText(getApplicationContext(), "Repeating", Toast.LENGTH_LONG).show();
					break;
		}
		
		// pour la mise à jour de la listview
		if (refresh)
		{
			SimpleCursorAdapter adapter = (SimpleCursorAdapter) getListAdapter();
	    	adapter.getCursor().requery();
	    	adapter.notifyDataSetChanged();
		}
		
		return true;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) 
	{
		super.onPrepareDialog(id, dialog);
		switch (id)
		{
			case R.id.menu_edit:
				EditText msgEdit = (EditText) dialog.findViewById(R.id.msg_et);
				CheckBox soundCb = (CheckBox) dialog.findViewById(R.id.sound_cb);
				
				//msgEdit.setText(id_alarm.getName());
				//soundCb.setChecked(id_alarm.getSound());
				break;
		}
	}	
	
	@Override
	protected Dialog onCreateDialog(int id) 
	{
		switch (id) 
		{
				case R.id.menu_edit:
					return new AlertDialog.Builder(this)
					   .setTitle("Edit")
					   .setView(getLayoutInflater().inflate(R.layout.edit, null))
				       .setCancelable(false)
				       					       				       
				       .setPositiveButton("Ok", new DialogInterface.OnClickListener()
				       {
					           public void onClick(DialogInterface dialog, int id) 
					           {
						        	   Dialog d = (Dialog) dialog;
						        	   EditText msgEdit = (EditText) d.findViewById(R.id.msg_et);
						        	   CheckBox soundCb = (CheckBox) d.findViewById(R.id.sound_cb);
						        	   
						        	   //id_alarm.setSound(soundCb.isChecked());
						        	   if (!TextUtils.isEmpty(msgEdit.getText())) 
						        	   {
										   //id_alarm.setName(msgEdit.getText().toString());
										   //dbHelper.update(id_alarm );
											 

//										   dbHelper.save_alarm(id_alarm); 
										   //alarm.persist(db);
							        	   
						        		   // pour la mise à jour de la listview
										   SimpleCursorAdapter adapter = (SimpleCursorAdapter) getListAdapter();
										   adapter.getCursor().requery();
										   adapter.notifyDataSetChanged();
							        	   
						        	   }
						        	   else
						        	   {
						        		   Toast.makeText(remind.this, "Enter a message", Toast.LENGTH_SHORT).show();
						        	   }
					           }
				       })
				       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
				       {
				           public void onClick(DialogInterface dialog, int id)
				           {
				                dialog.cancel();
				           }
				       })
				       .create();
		}
		return super.onCreateDialog(id);
	}
	
	
	/*******************/

	

	
	
    
    
	
		
	
}