package uk.co.jarofgreen.ymtimers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

/**
 * 
 * @author James Baster <jamesbaster.co.uk>
 * @copyright JMB Technology 2013
 * @license 3-clause BSD
 *
 */
public class OurApplication extends Application  {

	@Override
	public void onCreate() {
		super.onCreate();
	}

	protected List<Timer> timers = new ArrayList<Timer>();
	private Object timersLock = new Object();
	protected int nextTimerID = 1;
	
	public void addTimer(PossibleTimer possibleTimer) {
		synchronized(timersLock) {
			Log.i("OurApplication","adding timer");
			timers.add(new Timer(possibleTimer, nextTimerID));
			Collections.sort(timers);
			nextTimerID++;
		}
		startAlarmService();
	}
	

	public void onChangeTimerTime() {
		synchronized(timersLock) {
			Log.i("OurApplication","timer changed");
			Collections.sort(timers);
		}
	}
	
	
	
	
	public List<Timer> getTimers() {
		List<Timer> r = new ArrayList<Timer>();
		synchronized(timersLock) {
			Log.i("OurApplication","Getting Timers");
			for (Timer timer: timers) {
				r.add(timer);
			}
		}
		return r;
	}
	
	public Timer getTimerByID(int id) {
		synchronized(timersLock) {
			Log.i("OurApplication","Get specifir timer");
			for (Timer timer: timers) {
				if (timer.getId() == id) {
					return timer;
				}
			}
			return null;
		}	
	}
	
	public void deleteTimer(Timer timer) {
		synchronized(timersLock) {
			timers.remove(timer);
		}
	}
	
	boolean alarmServiceStarted = false;
	
	public void startAlarmService() {
		if (!alarmServiceStarted) {
			Log.i("OurApplication","Starting Alarm Service");
			// intent
			Intent ii = new Intent(getApplicationContext(), AlarmService.class);
			PendingIntent pi = PendingIntent.getService(getApplicationContext(), 2222, ii, PendingIntent.FLAG_CANCEL_CURRENT);
			// time
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, 15);
			//registering our pending intent with alarmmanager
			AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
			am.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),15000, pi);
			// started
			alarmServiceStarted = true;
		}
	}
	
}

@@ -525,16 +525,76 @@
 ();%0A%09%7D%0A%0A
+%09//######################################### Alarm Timers%0A%09%0A
 %09protect
@@ -1690,48 +1690,472 @@
 %09%7D%0A%09
-%7D%0A%09%0A%09boolean alarmServiceStarted = false
+%09if (timers.size() == 0) %7B%0A%09%09%09stopAlarmService();%0A%09%09%7D%0A%09%7D%0A%09%0A%09//######################################### Alarm Service%0A%09// The Alarm Service runs every 15 seconds.%0A%09// For now it just plays a noise if any timers have expired, but it should also update a notification.%0A%09// When there are no timers left, it is stopped to save battery. %0A%09%0A%09boolean alarmServiceStarted = false;%0A%09%0A%09protected Intent alarmServiceIntent;%0A%09protected PendingIntent alarmServicePendingIntent
 ;%0A%09%0A
@@ -2292,17 +2292,101 @@
 %0A%09%09%09
+if (alarmServiceIntent == null %7C%7C alarmServicePendingIntent == null) %7B%0A%09%09%09%09alarmService
 Intent
- ii
  = n
@@ -2444,16 +2444,29 @@
 ss);%0A%09%09%09
+%09alarmService
 PendingI
@@ -2475,11 +2475,8 @@
 ent 
-pi 
 = Pe
@@ -2533,10 +2533,26 @@
 22, 
-ii
+alarmServiceIntent
 , Pe
@@ -2585,16 +2585,21 @@
 RRENT);%0A
+%09%09%09%7D%0A
 %09%09%09// ti
@@ -2878,18 +2878,41 @@
 ,15000, 
-pi
+alarmServicePendingIntent
 );%0A%09%09%09//
@@ -2960,10 +2960,229 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09public void stopAlarmService() %7B%0A%09%09if (alarmServiceStarted) %7B%0A%09%09%09AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);%0A%09%09%09am.cancel(alarmServicePendingIntent);%0A%09%09%09alarmServiceStarted = false;%09%09%09%0A%09%09%7D%0A%09%7D%0A%09%0A%09%0A
 %7D%0A

Stop alarm service if no timers left