package com.alexstyl.specialdates.contact;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.alexstyl.specialdates.AppComponent;
import com.alexstyl.specialdates.EasyPreferences;
import com.alexstyl.specialdates.ErrorTracker;
import com.alexstyl.specialdates.MementoApplication;
import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.events.ContactsObserver;
import com.alexstyl.specialdates.events.PeopleEventsMonitor;
import com.alexstyl.specialdates.events.PreferenceChangedEventsUpdateTrigger;
import com.alexstyl.specialdates.events.peopleevents.EventPreferences;
import com.alexstyl.specialdates.permissions.PermissionChecker;
import com.novoda.notils.logger.simple.Log;

import java.util.concurrent.Callable;
import java.util.logging.Logger;

import javax.inject.Inject;

import io.reactivex.Observable;

import static java.util.Arrays.asList;

public class EventUpdatedService extends Service {

    @Inject
    PeopleEventsMonitor eventsMonitor;
    @Inject
    EventPreferences eventPreferences;

    PreferenceChangedEventsUpdateTrigger preferenceChangedEventsUpdateTrigger;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        AppComponent applicationModule = ((MementoApplication) getApplication()).getApplicationModule();
        applicationModule.inject(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        setupDatabaseRefresher();
        return START_STICKY;
    }

    private void setupDatabaseRefresher() {
        preferenceChangedEventsUpdateTrigger = new PreferenceChangedEventsUpdateTrigger(
                EasyPreferences.createForDefaultPreferences(this),
                getResources(),
                R.string.key_enable_bank_holidays,
                R.string.key_enable_namedays,
                R.string.key_nameday_lang,
                R.string.key_namedays_contacts_only,
                R.string.key_namedays_full_name
        );

        eventsMonitor.startMonitoring(
                asList(
                        new ContactsObserver(getContentResolver()),
                        preferenceChangedEventsUpdateTrigger
                ));

        boolean eventsHaveBeenInitialised = eventPreferences.hasBeenInitialised();
        if (!eventsHaveBeenInitialised) {
            Observable.fromCallable(new Callable<Integer>() {
                @Override
                public Integer call() {
                    eventsMonitor.updateEvents();
                    eventPreferences.markEventsAsInitialised();
                    return 5;
                }
            })
                    .subscribe();
        }
    }
}

@@ -225,55 +225,8 @@
 es;%0A
-import com.alexstyl.specialdates.ErrorTracker;%0A
 impo
@@ -582,215 +582,73 @@
 es;%0A
+%0A
 import 
-com.alexstyl.specialdates.permissions.PermissionChecker;%0Aimport com.novoda.notils.logger.simple.Log;%0A%0Aimport java.util.concurrent.Callable;%0Aimport java.util.logging.Logger;%0A%0Aimport javax.inject.Inject
+javax.inject.Inject;%0Aimport java.util.concurrent.Callable
 ;%0A%0Ai
@@ -771,16 +771,64 @@
 vice %7B%0A%0A
+    private static final int A_NUMBER = 5;%0A    %0A
     @Inj
@@ -926,16 +926,24 @@
 s;%0A%0A    
+private 
 Preferen
@@ -2564,17 +2564,24 @@
  return 
-5
+A_NUMBER
 ;%0A      

Remove magic number