package com.androidbegin.buttonclicktutorial;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.widget.Toast;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

//http://programmerguru.com/android-tutorial/android-audio-manager-tutorial/

//http://code4reference.com/2012/07/android-jelly-bean-notification/

public class AlarmReceiver extends BroadcastReceiver 
{

	/*
	@Override
	public void onReceive(Context arg0, Intent arg1) 
	{
		Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

	}
	*/
	@Override
	public void onReceive(Context context, Intent intent) 
	{
	
			//long alarmId = intent.getLongExtra("index", -1);
			
		
			String titre = intent.getStringExtra("titre");
			long keycode = intent.getLongExtra("keycode", -1); // avant long et geIntExtra
			long delai = intent.getLongExtra("delai", -1);
			long time = intent.getLongExtra("time", -1);
			//String option_delai = intent.getStringExtra("option_delai");
			String option_vibrate = intent.getStringExtra("option_vibrate");
			String option_ringtone = intent.getStringExtra("option_ringtone");
			
			/*
			AlarmMsg alarmMsg = new AlarmMsg(alarmMsgId);
			alarmMsg.setStatus(AlarmMsg.EXPIRED);
			alarmMsg.persist(RemindMe.db);
	
			Alarm alarm = new Alarm(alarmId);
			alarm.load(RemindMe.db);
	/*
	 */
			Notification n = new Notification(R.drawable.clock40, titre, System.currentTimeMillis());  //ic_launcher  // ic_alarm
			//PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
			PendingIntent pi = PendingIntent.getActivity(context, (int) keycode, new Intent(), 0);  // avant pas (int)
			
	
			n.setLatestEventInfo(context, "Remind Me", titre, pi);		
			
			/*
			if (RemindMe.isVibrate()) 
			{
				n.defaults |= Notification.DEFAULT_VIBRATE;
			}
			
			*/
			
			boolean sound = false;
			
			if( sound == true)
			{
				String url = android.provider.Settings.System.DEFAULT_NOTIFICATION_URI.toString();
				n.sound = Uri.parse(url);
				//		n.defaults |= Notification.DEFAULT_SOUND;
			}		
			
			
			/*******************************/
			/*
			if (RemindMe.isVibrate())
			{
				n.defaults |= Notification.DEFAULT_VIBRATE;
			}
			if (alarm.getSound()) 
			{
				n.sound = Uri.parse(RemindMe.getRingtone());
//				n.defaults |= Notification.DEFAULT_SOUND;
			}	
			*/
			
			
			/******************************/
			
			
			
			n.flags |= Notification.FLAG_AUTO_CANCEL;		
			NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			nm.notify((int)keycode, n);
			
			//String s = 
			
			Intent myIntent=new Intent(context, ringtone.class);   
			myIntent.putExtra("titre", titre);
			myIntent.putExtra("delai", delai);
			myIntent.putExtra("time", time);
			myIntent.putExtra("option_vibrate", option_vibrate);
			myIntent.putExtra("option_ringtone", option_ringtone);
			
			myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
			context.startActivity(myIntent); 
			
			//notificationManager.notify(Unique_Integer_Number, notification);
			
			// http://stackoverflow.com/questions/18102052/how-to-display-multiple-notifications-in-android
	}
}

	
	
	
	
	
	

