package com.androidbegin.buttonclicktutorial;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class infos_repeat extends Activity 
{
	
		TextView txt_id;
		private EditText txtjbt, txttelefon, txtemel;
		String sid, telno,email,name;

		DB_alarm db;
	
		@Override
		public void onCreate(Bundle savedInstanceState) 
		{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.infos_repeat_layout);
	
				db = new DB_alarm(getApplicationContext());
				
				ActionBar actionBar = getActionBar();
		        getActionBar().setHomeButtonEnabled(true);
		        getActionBar().setDisplayHomeAsUpEnabled(true);
		        setTitle("Infos repat");
		        
		        
		        sid = getIntent().getExtras().getString("stafid");
		        txt_id =(TextView) findViewById(R.id.txt_id);
		        txt_id.setText(sid);
		        
		        Button btn_quitter = (Button) findViewById(R.id.btn_exit);
		        btn_quitter.setOnClickListener(new OnClickListener() 
		 	    {
		 				@Override
		 				public void onClick(View arg0) 
		 				{
		 					finish();
		 				}
		 	    });
		        
		        Button btn_delete = (Button) findViewById(R.id.btn_delete);
		        btn_delete.setOnClickListener(new OnClickListener() 
		 	    {
		 				@Override
		 				public void onClick(View arg0) 
		 				{
							Toast.makeText(infos_repeat.this, "Delete repeat...", Toast.LENGTH_LONG).show();
							// arrêter le service et suppression de la database
							repeat_delete();
		 				}
		 	    });
		        
		}
		
		@Override
		protected void onResume() 
		{
			displayData();
			super.onResume();
		}
		
		private void displayData()
		{
			String requete ="SELECT * FROM " + DB_alarm.TABLE_REPEAT  +" WHERE _id ='"+sid+"';";
			Cursor mCursor = db.DB_query(requete);
			int val = mCursor.getCount();
		    Log.i("**************** => ", String.valueOf(val));

			
			//fetch the record
			if (mCursor.moveToFirst()) 
			{
				//fetch each field and transfer to textbox
				//telno=mCursor.getString(mCursor.getColumnIndex(DBHelper.TELEFON));
				//email=mCursor.getString(mCursor.getColumnIndex(DBHelper.EMEL));
				//name=mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA));
				//get data from field and transfer to EditText
				//txtnama.setText(name);
				//txtjbt.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));
				//txttelefon.setText(telno);
				//txtemel.setText(email);
				
				/*
				 + 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" + ")";
		            */
				
				String titre = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_NAME));
				String interval = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_INTERVAL));
				
				
				String coco ="bof";	
			}
			
			else
			{
				//do something here if no record fetched from database
				//txtnama.setText(sql);
				String toto = "blabla";
			}
		}//end displayData
		

		/*****************************************************************************/
		
		public void repeat_delete()
	    {
	    	
								AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

								alertDialogBuilder.setTitle("Confirmation");
				
								alertDialogBuilder
								.setMessage("Click yes to delete !")
								.setCancelable(false)
								.setPositiveButton("Yes",new DialogInterface.OnClickListener()
								{
									public void onClick(DialogInterface dialog,int id)
									{
										// supprimer de la database
										//db.delete_repeat(sid);
										
										Intent cancelThis = new Intent(infos_repeat.this, AlarmService.class);
				 						long indice = Long.parseLong(sid);   
										cancelThis.putExtra("ALARMID", indice );
										//cancelThis.putExtra("TITRE", "");
					 					//cancelThis.putExtra("startTime", "");
										cancelThis.setAction(AlarmService.CANCEL);
										startService(cancelThis);
										
										// retour à l'activity précédente
										finish();
										
									}
								})
								
								.setNegativeButton("No",new DialogInterface.OnClickListener()
								{
									public void onClick(DialogInterface dialog,int id)
									{
										dialog.cancel();
									}
								});

								AlertDialog alertDialog = alertDialogBuilder.create();
								alertDialog.show();
	    }

		
		
		/*****************************************************************************/
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item)
		{
		      switch (item.getItemId())
		      {
	
		      		case android.R.id.home:
			      			this.finish();
			      			return true;
				    
		      		default:
			          return super.onOptionsItemSelected(item);
			 }
		}

	
}
