package de.bitdroid.flooding.alarms;

import java.util.Set;

import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.fasterxml.jackson.databind.ObjectMapper;

import de.bitdroid.flooding.R;
import de.bitdroid.flooding.news.NewsItem;
import de.bitdroid.flooding.news.NewsManager;
import de.bitdroid.flooding.ods.cep.BaseEventReceiver;
import de.bitdroid.flooding.ods.cep.CepManager;
import de.bitdroid.flooding.utils.Log;


public final class EventReceiver extends BaseEventReceiver {

	private static final String
		EXTRA_EPL_STMT = "EXTRA_EPL_STMT",
		EXTRA_ALARM = "EXTRA_ALARM";

	private static final ObjectMapper mapper = new ObjectMapper();

	@Override
	protected void onReceive(Context context, String eplStmt, String eventId) {
		Log.debug("Received event with id " + eventId);

		Intent intent = new Intent(context, EventIntentService.class);
		intent.putExtra(EXTRA_EPL_STMT, eplStmt);
		context.startService(intent);
	}


	public static final class EventIntentService extends IntentService {

		private static final EplStmtCreator stmtCreator = new EplStmtCreator();

		public EventIntentService() {
			super(EventIntentService.class.getSimpleName());
		}

		@Override
		protected void onHandleIntent(Intent intent) {
			String eplStmt = intent.getStringExtra(EXTRA_EPL_STMT);

			// find matching alarm and show news
			Set<Alarm> alarms = AlarmManager.getInstance(getApplicationContext()).getAll();
			for (Alarm alarm : alarms) {
				if (alarm.accept(stmtCreator, null).equals(eplStmt)) {
					String json = mapper.valueToTree(alarm).toString();
					Intent newsIntent = new Intent(this, EventToNewsReceiver.class);
					newsIntent.putExtra(EXTRA_ALARM, json);
					sendBroadcast(newsIntent);
					return;
				}
			}

			// if no alarm was found for epl stmt, then there was an error
			// when last trying to unregister it --> retry now
			Intent newsIntent = new Intent(this, EventToNewsReceiver.class);
			newsIntent.putExtra(EXTRA_EPL_STMT, eplStmt);
			sendBroadcast(newsIntent);
		}
	}


	public static final class EventToNewsReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String alarmJson = intent.getStringExtra(EXTRA_ALARM);
			String eplStmt = intent.getStringExtra(EXTRA_EPL_STMT);

			// check or error while unregistering
			if (alarmJson == null && eplStmt != null) {
				CepManager.getInstance(context).unregisterEplStmt(eplStmt);

			// show news
			} else {
				LevelAlarm alarm = null;
				try {
					alarm = mapper.treeToValue(
							mapper.readTree(alarmJson), 
							LevelAlarm.class);
				} catch (Exception e) {
					Log.error("failed to recreate alarm", e);
				}

				String location = alarm.getRiver() + " - " + alarm.getStation();

				String title = context.getString(
						R.string.alarms_triggered_title,
						location);

				String relation = alarm.getAlarmWhenAbove() 
					? context.getString(R.string.alarms_new_trigger_above)
					: context.getString(R.string.alarms_new_trigger_below);

				String msg = context.getString(
						R.string.alarms_triggered_msg,
						location,
						relation,
						alarm.getLevel());

				NewsItem news = new NewsItem.Builder(
						title,
						msg,
						System.currentTimeMillis())
					.setNavigationPos(1)
					.build();
				NewsManager.getInstance(context).addItem(news, true);
			}
		}
	}

}

@@ -509,16 +509,63 @@
 ls.Log;%0A
+import de.bitdroid.flooding.utils.StringUtils;%0A
 %0A%0Apublic
@@ -2830,35 +2830,104 @@
 n = 
-alarm.getRiver() + %22 - %22 + 
+%0A%09%09%09%09%09StringUtils.toProperCase(alarm.getRiver()) %0A%09%09%09%09%09+ %22 - %22 %0A%09%09%09%09%09+ StringUtils.toProperCase(
 alar
@@ -2940,16 +2940,17 @@
 tation()
+)
 ;%0A%0A%09%09%09%09S

news title will be proper case