package jcotter.listenmoe.service.notification;

import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;

import jcotter.listenmoe.R;
import jcotter.listenmoe.model.Song;
import jcotter.listenmoe.service.StreamService;
import jcotter.listenmoe.ui.MenuActivity;
import jcotter.listenmoe.ui.RadioActivity;
import jcotter.listenmoe.util.AuthUtil;

public class AppNotification {
    protected int NOTIFICATION_ID = 1;
    protected StreamService service;

    public void init(StreamService service) {
        this.service = service;
    }

    public void update() {
        if (!StreamService.isStreamStarted) {
            return;
        }

        // Get current song info
        final Song song = service.getCurrentSong();
        if (song == null) {
            return;
        }

        final boolean isPlaying = service.isPlaying();

        // Construct content string with song title and anime
        final String currentSongAnime = song.getAnime();
        String title = song.getTitle();
        if (!currentSongAnime.equals("")) {
            title += "\n[" + currentSongAnime + "]";
        }

        // Play/pause action
        final NotificationCompat.Action playPauseAction = new NotificationCompat.Action(
                isPlaying ? R.drawable.icon_pause : R.drawable.icon_play,
                isPlaying ? service.getString(R.string.action_pause) : service.getString(R.string.action_play),
                getPlaybackAction(service.getClass(), StreamService.PLAY, !isPlaying)
        );

        // Favorite action
        NotificationCompat.Action favoriteAction;
        if (AuthUtil.isAuthenticated(service)) {
            favoriteAction = new NotificationCompat.Action(
                    song.isFavorite() ? R.drawable.favorite_full : R.drawable.favorite_empty,
                    song.isFavorite() ? service.getString(R.string.action_unfavorite) : service.getString(R.string.action_favorite),
                    getPlaybackAction(service.getClass(), StreamService.FAVORITE, true)
            );
        } else {
            favoriteAction = new NotificationCompat.Action(
                    R.drawable.favorite_empty,
                    service.getString(R.string.action_favorite),
                    getPlaybackAction(MenuActivity.class, MenuActivity.TAB_INDEX, 2)
            );
        }

        // Stop action
        final NotificationCompat.Action stopAction = new NotificationCompat.Action(
                R.drawable.icon_close,
                service.getString(R.string.action_stop),
                getPlaybackAction(service.getClass(), StreamService.STOP, true)
        );

        // Build the notification
        final Intent action = new Intent(service, RadioActivity.class);
        action.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(service)
                .setSmallIcon(R.drawable.icon_notification)
                .setColor(ContextCompat.getColor(service, R.color.colorAccent))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                .setContentTitle(song.getArtist())
                .setContentText(title)
                .setCategory(NotificationCompat.CATEGORY_SERVICE)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(clickIntent)
                .setOngoing(isPlaying)
                .addAction(playPauseAction)
                .addAction(favoriteAction)
                .addAction(stopAction);

        service.startForeground(NOTIFICATION_ID, builder.build());
    }

    private PendingIntent getPlaybackAction(final Class target, final String action, final boolean value) {
        final Intent intent = new Intent(service, target);
        intent.putExtra(action, value);

        return PendingIntent.getService(service, 0, intent, 0);
    }

    private PendingIntent getPlaybackAction(final Class target, final String action, final int value) {
        final Intent intent = new Intent(service, target);
        intent.putExtra(action, value);

        return PendingIntent.getService(service, 0, intent, 0);
    }
}

@@ -483,23 +483,21 @@
 %7B%0A    pr
-otec
+iva
 te
-d
  int NOT
@@ -524,15 +524,13 @@
   pr
-otec
+iva
 te
-d
  Str
@@ -550,16 +550,56 @@
 rvice;%0A%0A
+    private int actionRequestCode = 0;%0A%0A
     publ
@@ -1041,16 +1041,126 @@
 d anime%0A
+        final StringBuilder titleBuilder = new StringBuilder();%0A        titleBuilder.append(song.getTitle());%0A
         
@@ -1212,48 +1212,8 @@
 ();%0A
-        String title = song.getTitle();%0A
     
@@ -1273,53 +1273,169 @@
 itle
- += %22%5Cn%5B%22 + currentSongAnime + %22%5D%22;%0A        %7D
+Builder.append(%22%5Cn%22);%0A            titleBuilder.append(String.format(%22%5B %25s %5D%22, currentSongAnime));%0A        %7D%0A        final String title = titleBuilder.toString();
 %0A%0A  
@@ -1769,35 +1769,43 @@
 ckAction
-(s
+Service(StreamS
 ervice.
-getC
+c
 lass
-()
 , Stream
@@ -2292,35 +2292,43 @@
 ckAction
-(s
+Service(StreamS
 ervice.
-getC
+c
 lass
-()
 , Stream
@@ -2592,16 +2592,24 @@
 ckAction
+Activity
 (MenuAct
@@ -2914,27 +2914,35 @@
 tion
-(s
+Service(StreamS
 ervice.
-getC
+c
 lass
-()
 , St
@@ -3229,17 +3229,49 @@
 action, 
-0
+PendingIntent.FLAG_UPDATE_CURRENT
 );%0A%0A    
@@ -3939,25 +3939,45 @@
 Ongoing(
-isPlaying
+StreamService.isStreamStarted
 )%0A      
@@ -4207,32 +4207,39 @@
 etPlaybackAction
+Service
 (final Class tar
@@ -4436,37 +4436,177 @@
 ervice, 
-0, intent, 0);%0A    %7D%0A
+++actionRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);%0A    %7D%0A%0A    // TODO: possibly move this since it's related to the activity instead of the service
 %0A    pri
@@ -4641,16 +4641,24 @@
 ckAction
+Activity
 (final C
@@ -4834,39 +4834,40 @@
 endingIntent.get
-Service
+Activity
 (service, 0, int
@@ -4864,20 +4864,70 @@
 ce, 
-0, intent, 0
+++actionRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT
 );%0A 

Fix up notification