package net.squanchy.notification;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Comparator;

import net.squanchy.schedule.domain.view.Event;

import org.joda.time.LocalDateTime;

import io.reactivex.Observable;

public class NotificationService extends IntentService {

    private static final int NOTIFICATION_INTERVAL_MINUTES = 10;

    private NotificationCreator notificationCreator;
    private Notifier notifier;

    public NotificationService() {
        super(NotificationService.class.getSimpleName());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationCreator = new NotificationCreator(this);
        notifier = Notifier.from(this);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        LocalDateTime notificationIntervalEnd = new LocalDateTime().plusMinutes(NOTIFICATION_INTERVAL_MINUTES);

        Observable<Event> favourites = Observable.empty(); // TODO load all events from somewhere
        Observable<Event> sortedFavourites = favourites.sorted(byStartDate());

        LocalDateTime now = new LocalDateTime();
        sortedFavourites
                .filter(event -> event.startTime().isAfter(now))
                .filter(event -> isBeforeOrEqualTo(event.startTime(), notificationIntervalEnd))
                .toList()
                .map(notificationCreator::createFrom)
                .subscribe(notifier::showNotifications);

        sortedFavourites
                .filter(event -> event.startTime().isAfter(notificationIntervalEnd))
                .take(1)
                .subscribe(this::scheduleNextAlarm);
    }

    private boolean isBeforeOrEqualTo(LocalDateTime start, LocalDateTime notificationIntervalEnd) {
        return start.isBefore(notificationIntervalEnd) || start.isEqual(notificationIntervalEnd);
    }

    private Comparator<Event> byStartDate() {
        return (event1, event2) -> event1.startTime().compareTo(event2.startTime());
    }

    private void scheduleNextAlarm(Event event) {
        LocalDateTime serviceAlarm = event.startTime().minusMinutes(NOTIFICATION_INTERVAL_MINUTES);

        Intent serviceIntent = new Intent(this, NotificationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, serviceAlarm.toDateTime().getMillis(), pendingIntent);
    }
}

@@ -188,24 +188,111 @@
 tent.Intent;
+%0Aimport android.content.SharedPreferences;%0Aimport android.preference.PreferenceManager;
 %0A%0Aimport jav
@@ -307,24 +307,47 @@
 omparator;%0A%0A
+import net.squanchy.R;%0A
 import net.s
@@ -575,16 +575,84 @@
 ES = 10;
+%0A    private static final boolean SHOW_NOTIFICATIONS_DEFAULT = true;
 %0A%0A    pr
@@ -728,16 +728,59 @@
 otifier;
+%0A    private SharedPreferences preferences;
 %0A%0A    pu
@@ -1039,24 +1039,99 @@
 from(this);%0A
+        preferences = PreferenceManager.getDefaultSharedPreferences(this);%0A
     %7D%0A%0A    @
@@ -1526,24 +1526,69 @@
 DateTime();%0A
+        if (shouldShowNotifications()) %7B%0A    
         sort
@@ -1592,32 +1592,36 @@
 ortedFavourites%0A
+    
                 
@@ -1669,16 +1669,20 @@
 r(now))%0A
+    
         
@@ -1785,16 +1785,20 @@
         
+    
 .toList(
@@ -1799,16 +1799,20 @@
 oList()%0A
+    
         
@@ -1865,32 +1865,36 @@
                 
+  
+  
 .subscribe(notif
@@ -1917,16 +1917,26 @@
 ations);
+%0A        %7D
 %0A%0A      
@@ -2116,32 +2116,288 @@
 tAlarm);%0A    %7D%0A%0A
+    private boolean shouldShowNotifications() %7B%0A        String notificationPreferenceKey = getString(R.string.about_to_start_notification_preference_key);%0A        return preferences.getBoolean(notificationPreferenceKey, SHOW_NOTIFICATIONS_DEFAULT);%0A    %7D%0A%0A
     private bool

Check for preferences status before showing notifications