package com.platypii.baseline;

import com.platypii.baseline.events.AudibleEvent;
import com.platypii.baseline.events.LoggingEvent;
import com.platypii.baseline.views.MainActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Manage notification bars
 */
class Notifications implements BaseService {
    private static final String TAG = "Notifications";
    private static final int notificationId = 117;

    private Context context;

    @Override
    public void start(@NonNull Context context) {
        this.context = context;
        EventBus.getDefault().register(this);
        update();
    }

    private void update() {
        final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            Log.e(TAG, "failed to get notification manager");
            return;
        }

        final boolean logging = Services.logger.isLogging();
        final boolean audible = Services.audible.isEnabled();
        if(logging || audible) {
            // Show/update notification
            final Intent mainIntent = new Intent(context, MainActivity.class);
            final PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
            Notification.Builder builder = new Notification.Builder(context)
                    .setSmallIcon(R.drawable.ws_white)
                    .setContentTitle(context.getString(R.string.app_name_long))
                    .setOngoing(true)
                    .setContentIntent(mainPendingIntent);
            // Add stop action
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                final Intent stopIntent = new Intent(context, MainActivity.class);
                stopIntent.setType("baseline/stop");
                final PendingIntent stopPendingIntent = PendingIntent.getActivity(context, 0, stopIntent, 0);
                final Notification.Action stopAction = new Notification.Action.Builder(R.drawable.square, "Stop", stopPendingIntent).build();
                builder = builder.addAction(stopAction);
            }
            if(logging && audible) {
                builder = builder.setUsesChronometer(true).setWhen(Services.logger.getStartTime());
                builder = builder.setContentText(context.getString(R.string.notify_audible_logging));
            } else if(logging) {
                builder = builder.setUsesChronometer(true).setWhen(Services.logger.getStartTime());
                builder = builder.setContentText(context.getString(R.string.notify_logging));
            } else {
                builder = builder.setContentText(context.getString(R.string.notify_audible));
            }
            final Notification notification = builder.build();
            notificationManager.notify(notificationId, notification);
        } else {
            // Hide notification
            notificationManager.cancel(notificationId);
        }
    }

    // Subscribe to updates
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoggingEvent(LoggingEvent event) {
        update();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudibleEvent(AudibleEvent event) {
        update();
    }

    @Override
    public void stop() {
        EventBus.getDefault().unregister(this);
        context = null;
    }
}

@@ -1439,32 +1439,426 @@
 otification%0A    
+        final Notification notification = getNotification(logging, audible);%0A            notificationManager.notify(notificationId, notification);%0A        %7D else %7B%0A            // Hide notification%0A            notificationManager.cancel(notificationId);%0A        %7D%0A    %7D%0A%0A    private Notification getNotification(boolean logging, boolean audible) %7B%0A        // Intent to open MainActivity on stop%0A
         final In
@@ -1908,36 +1908,32 @@
 ctivity.class);%0A
-    
         final Pe
@@ -2026,20 +2026,16 @@
         
-   
- 
 Notifica
@@ -2103,28 +2103,24 @@
             
-    
 .setSmallIco
@@ -2150,36 +2150,32 @@
                 
- 
-   
 .setContentTitle
@@ -2230,28 +2230,24 @@
             
-  
-  
 .setOngoing(
@@ -2252,20 +2252,16 @@
 g(true)%0A
-    
         
@@ -2314,20 +2314,16 @@
         
-    
 // Add s
@@ -2337,28 +2337,24 @@
 ion%0A        
- 
-   
 if (Build.VE
@@ -2402,28 +2402,24 @@
 AT_WATCH) %7B%0A
-    
             
@@ -2481,28 +2481,24 @@
 ity.class);%0A
-    
             
@@ -2542,28 +2542,24 @@
             
-    
 final Pendin
@@ -2636,28 +2636,24 @@
 Intent, 0);%0A
-    
             
@@ -2782,36 +2782,32 @@
 ();%0A            
-   
- 
 builder = builde
@@ -2835,34 +2835,26 @@
 n);%0A        
-    %7D%0A    
+%7D%0A
         if(l
@@ -2878,36 +2878,32 @@
 ) %7B%0A            
-   
- 
 builder = builde
@@ -2962,36 +2962,32 @@
 etStartTime());%0A
-    
             buil
@@ -3072,28 +3072,24 @@
 ));%0A        
- 
-   
 %7D else if(lo
@@ -3101,36 +3101,32 @@
 ) %7B%0A            
-  
-  
 builder = builde
@@ -3185,36 +3185,32 @@
 etStartTime());%0A
-    
             buil
@@ -3287,28 +3287,24 @@
 ));%0A        
-    
 %7D else %7B%0A   
@@ -3304,36 +3304,32 @@
 e %7B%0A            
- 
-   
 builder = builde
@@ -3394,28 +3394,24 @@
 ));%0A        
-    
 %7D%0A          
@@ -3412,248 +3412,31 @@
     
-    final Notification notification = builder.build();%0A            notificationManager.notify(notificationId, notification);%0A        %7D else %7B%0A            // Hide notification%0A            notificationManager.cancel(notificationId);%0A        %7D
+return builder.build();
 %0A   

Split out getNotification method