package com.novoda.downloadmanager;

import android.app.Notification;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

class NotificationCreator<T> {

    private final Context applicationContext;
    private final NotificationCustomizer<T> notificationCustomizer;
    private NotificationChannelProvider notificationChannelProvider;

    NotificationCreator(Context context, NotificationCustomizer<T> customizer, NotificationChannelProvider notificationChannelProvider) {
        this.applicationContext = context.getApplicationContext();
        this.notificationCustomizer = customizer;
        this.notificationChannelProvider = notificationChannelProvider;
    }

    void setNotificationChannelProvider(NotificationChannelProvider notificationChannelProvider) {
        this.notificationChannelProvider = notificationChannelProvider;
    }

    private int notificationId;
    private NotificationCustomizer.NotificationDisplayState notificationDisplayState;
    private NotificationCompat.Builder builder;

    NotificationInformation createNotification(final T notificationPayload) {
        return new NotificationInformation() {
            @Override
            public int getId() {
                if (notificationPayload instanceof DownloadBatchStatus) {
                    return ((DownloadBatchStatus) notificationPayload).getDownloadBatchId().hashCode();
                }

                return notificationPayload.hashCode();
            }

            @Override
            public Notification getNotification() {
                int newNotificationId = getId();
                NotificationCustomizer.NotificationDisplayState newNotificationDisplayState = notificationDisplayState();

                if (builder == null || hasChanged(newNotificationId, newNotificationDisplayState)) {
                    builder = new NotificationCompat.Builder(applicationContext, notificationChannelProvider.channelId());
                    notificationId = newNotificationId;
                    notificationDisplayState = newNotificationDisplayState;
                }

                return notificationCustomizer.customNotificationFrom(builder, notificationPayload);
            }

            private boolean hasChanged(int newNotificationId, NotificationCustomizer.NotificationDisplayState newNotificationDisplayState) {
                return notificationId != newNotificationId && !notificationDisplayState.equals(newNotificationDisplayState);
            }

            @Override
            public NotificationCustomizer.NotificationDisplayState notificationDisplayState() {
                return notificationCustomizer.notificationDisplayState(notificationPayload);
            }
        };
    }
}

@@ -910,94 +910,8 @@
 Id;%0A
-    private NotificationCustomizer.NotificationDisplayState notificationDisplayState;%0A
     
@@ -1528,130 +1528,8 @@
 d();
-%0A                NotificationCustomizer.NotificationDisplayState newNotificationDisplayState = notificationDisplayState();
 %0A%0A  
@@ -1565,27 +1565,16 @@
 null %7C%7C 
-hasChanged(
 newNotif
@@ -1586,14 +1586,13 @@
 onId
-, newN
+ != n
 otif
@@ -1598,29 +1598,18 @@
 fication
-DisplayState)
+Id
 ) %7B%0A    
@@ -1787,84 +1787,8 @@
 Id;%0A
-                    notificationDisplayState = newNotificationDisplayState;%0A
     
@@ -1921,289 +1921,8 @@
  %7D%0A%0A
-            private boolean hasChanged(int newNotificationId, NotificationCustomizer.NotificationDisplayState newNotificationDisplayState) %7B%0A                return notificationId != newNotificationId && !notificationDisplayState.equals(newNotificationDisplayState);%0A            %7D%0A%0A
     

Use only notification id when creating new builder.