package uk.co.ourfriendirony.medianotifier.notifier;

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.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;

import uk.co.ourfriendirony.medianotifier.R;
import uk.co.ourfriendirony.medianotifier.activities.ActivityMovieNotifications;
import uk.co.ourfriendirony.medianotifier.activities.ActivityTVNotifications;
import uk.co.ourfriendirony.medianotifier.db.MovieDatabase;
import uk.co.ourfriendirony.medianotifier.db.TVShowDatabase;

public class NotifierReceiver extends BroadcastReceiver {
    int unwatchedEpisodes = 0;
    int unwatchedMovies = 0;
    int unwatchedTotal = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        unwatchedEpisodes = new TVShowDatabase(context).countUnwatchedReleasedEpisodes();
        unwatchedMovies = new MovieDatabase(context).countUnwatchedReleasedMovies();
        unwatchedTotal = unwatchedEpisodes + unwatchedMovies;

        if (unwatchedTotal > 0) {
            NotificationCompat.Builder notification = getBuilder(context);

            PendingIntent notificationIntent = PendingIntent.getActivity(
                    context, 0,
                    getNotificationIntent(context),
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
            notification.setContentIntent(notificationIntent);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            int notificationId = 1;
            notificationManager.notify(notificationId, notification.build());
        }
    }

    @NonNull
    private Intent getNotificationIntent(Context context) {
        if (unwatchedEpisodes > 0)
            return new Intent(context, ActivityTVNotifications.class);
        else if (unwatchedMovies > 0)
            return new Intent(context, ActivityMovieNotifications.class);
        return new Intent(context, ActivityTVNotifications.class);
    }

    private NotificationCompat.Builder getBuilder(Context context) {
        return new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.img_app_icon)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_text) + unwatchedEpisodes)
                .setDefaults(Notification.DEFAULT_ALL);
    }
}

@@ -353,16 +353,49 @@
 ompat;%0A%0A
+import java.text.MessageFormat;%0A%0A
 import u
@@ -2261,32 +2261,244 @@
 text context) %7B%0A
+        String text = MessageFormat.format(%22%7B1%7D %7B2%7D (%7B3%7D/%7B4%7D/%7B5%7D)%22,%0A                context.getString(R.string.notification_text),%0A                unwatchedTotal, unwatchedEpisodes, unwatchedMovies, 0%0A        );%0A
         return n
@@ -2536,16 +2536,16 @@
 ontext)%0A
-
         
@@ -2708,73 +2708,12 @@
 ext(
-con
 text
-.getString(R.string.notification_text) + unwatchedEpisodes
 )%0A  

Tweak notification to include the episode/movie totals