package com.example.db01;

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;


public class AlarmReceiver extends BroadcastReceiver 
{
	

	private DbAdapter dbHelper;
	
	@Override
	public void onReceive(Context context, Intent intent)
	{
		
		dbHelper = new DbAdapter(context);
		dbHelper.open();
		
		long alarmMsgId = intent.getLongExtra(AlarmMsg.COL_ID, -1);
		long alarmId = intent.getLongExtra(AlarmMsg.COL_ALARMID, -1);
		
		AlarmMsg alarmMsg = new AlarmMsg(alarmMsgId);
		
		alarmMsg.setStatus(AlarmMsg.EXPIRED);
		
		
		alarmMsg.setAlarmId(alarmMsgId);
				
// sauver la modification		
		dbHelper.save_msg();
		//dbHelper.save_msg(alarmId, datetime, status)
		//alarmMsg.persist(RemindMe.db);
		
		
		
		Alarm alarm = new Alarm(alarmId);
		String name = dbHelper.alarm_load(alarmId);
		//alarm.load(RemindMe.db);
		
		//Notification n = new Notification(R.drawable.ic_launcher, alarm.getName(), System.currentTimeMillis());
		Notification n = new Notification(R.drawable.ic_launcher, name, System.currentTimeMillis());
		
		PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
		
		//n.setLatestEventInfo(context, "Salut ****", alarm.getName(), pi);		
		n.setLatestEventInfo(context, "Salut ****", name, pi);
		
/*		debug
		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)alarmMsgId, n);
	}

}
