package org.bostonandroid.umbrellatoday;

import java.util.Hashtable;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class AlarmService extends Service {
    public final static String TAG = "AlarmReceiver";
    private NotificationManager notificationManager;
    private Hashtable<Integer, ReportRetriever> retrievers;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        retrievers = new Hashtable<Integer, ReportRetriever>();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        ReportConsumer consumer = new AlarmServiceReportConsumer(intent,
                startId);
        ReportRetriever r = new ReportRetriever(consumer);
        retrievers.put(startId, r);
        r.execute(intent.getDataString());
    }

    private void showNotification(String message, PendingIntent contentIntent) {
        Notification notification = new Notification(
                R.drawable.notification_icon, "Umbrella Today", System
                        .currentTimeMillis());

        notification.setLatestEventInfo(AlarmService.this, "UmbrellaToday",
                message, contentIntent);

        notificationManager.notify(1, notification);
    }

    private class AlarmServiceReportConsumer implements ReportConsumer {
        private Intent intent;
        private int startId;

        AlarmServiceReportConsumer(Intent intent, int startId) {
            this.intent = intent;
            this.startId = startId;
        }

        public void consumeReport(Report report) {
            if (report.getAnswer().equals("yes")) {
                PendingIntent contentIntent = notificationIntent(intent);
                showNotification("You should bring your Umbrella today",
                        contentIntent);
                retrievers.remove(startId);
                if (retrievers.isEmpty()) {
                    stopSelf();
                }
            }
        }

        private PendingIntent notificationIntent(Intent intent) {
            Intent notificationIntent = new Intent(Intent.ACTION_VIEW, intent
                    .getData());
            notificationIntent.setClass(AlarmService.this,
                    UmbrellaForToday.class);

            PendingIntent contentIntent = PendingIntent.getActivity(
                    AlarmService.this, 0, notificationIntent, 0);

            return contentIntent;
        }
    }
}

@@ -1117,440 +1117,8 @@
  %7D%0A%0A
-    private void showNotification(String message, PendingIntent contentIntent) %7B%0A        Notification notification = new Notification(%0A                R.drawable.notification_icon, %22Umbrella Today%22, System%0A                        .currentTimeMillis());%0A%0A        notification.setLatestEventInfo(AlarmService.this, %22UmbrellaToday%22,%0A                message, contentIntent);%0A%0A        notificationManager.notify(1, notification);%0A    %7D%0A%0A
     
@@ -1202,24 +1202,38 @@
 private 
-Intent i
+PendingIntent contentI
 ntent;%0A 
@@ -1347,23 +1347,50 @@
 his.
-intent = intent
+contentIntent = notificationIntent(intent)
 ;%0A  
@@ -1497,20 +1497,32 @@
         
-if (
+String answer = 
 report.g
@@ -1531,16 +1531,40 @@
 Answer()
+;%0A            if (answer
 .equals(
@@ -1593,82 +1593,8 @@
     
-PendingIntent contentIntent = notificationIntent(intent);%0A                
 show
@@ -1669,16 +1669,149 @@
         
+ contentIntent);%0A            %7D else if (answer.equals(%22snow%22)) %7B%0A                showNotification(%22You should wear snow pants today%22,
  content
@@ -1818,32 +1818,39 @@
 Intent);%0A       
+     %7D%0A
          retriev
@@ -1834,32 +1834,35 @@
      %7D%0A         
+   
 retrievers.remov
@@ -1869,28 +1869,24 @@
 e(startId);%0A
-    
             
@@ -1929,20 +1929,16 @@
         
-   
- 
 stopSelf
@@ -1945,26 +1945,8 @@
 ();%0A
-                %7D%0A
     
@@ -2423,21 +2423,491 @@
 t;%0A        %7D
+%0A%0A        private void showNotification(String message, PendingIntent contentIntent) %7B%0A            Notification notification = new Notification(%0A                    R.drawable.notification_icon, %22Umbrella Today%22, System%0A                            .currentTimeMillis());%0A%0A            notification.setLatestEventInfo(AlarmService.this, %22UmbrellaToday%22,%0A                    message, contentIntent);%0A%0A            notificationManager.notify(startId, notification);%0A        %7D
 %0A    %7D%0A%7D%0A

Refactor AlarmService.