package com.ldc.bananamuffin;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class BreakfastNotifier extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1;
    public static final String PACKAGE_NAME = "com.whatsapp";
    public static final String WHATSAPP_PACKAGE_NAME = PACKAGE_NAME;
    public static final String TYPE_TEXT_PLAIN = "text/plain";

    public static enum Action {
        SHOW, YES, NO
    }

    public BreakfastNotifier() {
        Log.i(BananaMuffin.TAG, "Breakfast Notifier created.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Log.i(BananaMuffin.TAG, "Handling action: " + action + " for Alarm intent: " + intent);
        if (action != null) {
            switch (Action.valueOf(action)) {
                case SHOW:
                    show(context);
                    break;
                case YES:
                    yes(context, "Yes");
                    break;
                case NO:
                    no(context);
                    break;
                default:
                    Log.d(BananaMuffin.TAG, "Action not supported " + action);
            }
        }
    }

    private void show(Context context) {
        Log.i(BananaMuffin.TAG, "Show notification");

        Intent yesIntent = new Intent(context, BreakfastNotifier.class);
        yesIntent.setAction(Action.YES.name());
        PendingIntent pendingYes = PendingIntent.getService(context, 0, yesIntent, 0);

        Intent noIntent = new Intent(context, BreakfastNotifier.class);
        noIntent.setAction(Action.NO.name());
        PendingIntent pendingNo = PendingIntent.getService(context, 0, noIntent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.notification)
                .setContentTitle("Tanto va la gatta al largo ...")
                .setContentText("... che ci lascia il Muffin-o")
                .addAction(0, context.getString(R.string.yes), pendingYes)
                .addAction(0, context.getString(R.string.no), pendingNo);

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // mId allows you to update the notification later on.
        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    private void yes(Context context, String message) {
        Log.i(BananaMuffin.TAG, "Press Yes");
        Intent send = new Intent();
        send.setAction(Intent.ACTION_SEND);
        send.putExtra(Intent.EXTRA_TEXT, message);
        send.setType(TYPE_TEXT_PLAIN);
        send.setPackage(WHATSAPP_PACKAGE_NAME); // TODO Check if Whatsapp exists
        context.startActivity(send);
    }

    private void no(Context context) {
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Log.i(BananaMuffin.TAG, "Press No");
        notificationManager.cancel(NOTIFICATION_ID);
    }

}
@@ -195,32 +195,74 @@
 content.Intent;%0A
+import android.content.pm.PackageManager;%0A
 import android.s
@@ -295,16 +295,16 @@
 Compat;%0A
-
 import a
@@ -624,16 +624,86 @@
 /plain%22;
+%0A    public static final String CHOOSER_TEXT = %22Send message using: %22;
 %0A%0A    pu
@@ -1821,39 +1821,41 @@
 endingIntent.get
-Service
+Broadcast
 (context, 0, yes
@@ -2036,23 +2036,25 @@
 tent.get
-Service
+Broadcast
 (context
@@ -2880,24 +2880,83 @@
 ress Yes%22);%0A
+        // Create an intent to send a message via Whatsapp%0A
         Inte
@@ -2976,16 +2976,34 @@
  Intent(
+Intent.ACTION_SEND
 );%0A     
@@ -3017,22 +3017,21 @@
 .set
-Action
+Flags
 (Intent.
 ACTI
@@ -3022,35 +3022,46 @@
 lags(Intent.
-ACTION_SEND
+FLAG_ACTIVITY_NEW_TASK
 );%0A        s
@@ -3297,32 +3297,77 @@
 text context) %7B%0A
+        Log.i(BananaMuffin.TAG, %22Press No%22);%0A
         Notifica
@@ -3392,32 +3392,32 @@
 cationManager =%0A
+
                 
@@ -3498,53 +3498,8 @@
 E);%0A
-        Log.i(BananaMuffin.TAG, %22Press No%22);%0A
     

Send a text via Whatsapp