package com.alexstyl.specialdates;

import android.app.AlarmManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.alexstyl.android.AlarmManagerCompat;
import com.alexstyl.resources.ResourcesModule;
import com.alexstyl.specialdates.dailyreminder.DailyReminderPreferences;
import com.alexstyl.specialdates.dailyreminder.DailyReminderScheduler;
import com.alexstyl.specialdates.events.ContactsObserver;
import com.alexstyl.specialdates.events.PeopleEventsDatabaseUpdater;
import com.alexstyl.specialdates.events.PreferenceChangedEventsUpdateTrigger;
import com.alexstyl.specialdates.events.namedays.activity.NamedaysInADayModule;
import com.alexstyl.specialdates.events.peopleevents.PeopleEventsModule;
import com.alexstyl.specialdates.facebook.FacebookPreferences;
import com.alexstyl.specialdates.facebook.friendimport.FacebookFriendsScheduler;
import com.alexstyl.specialdates.images.AndroidContactsImageDownloader;
import com.alexstyl.specialdates.images.ImageModule;
import com.alexstyl.specialdates.images.NutraBaseImageDecoder;
import com.alexstyl.specialdates.ui.widget.ViewModule;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.L;
import com.novoda.notils.logger.simple.Log;

import javax.inject.Inject;

import net.danlew.android.joda.JodaTimeAndroid;

import static java.util.Arrays.asList;

public class MementoApplication extends Application {

    private AppComponent appComponent;

    @Inject PeopleEventsDatabaseUpdater updater;

    @Override
    public void onCreate() {
        super.onCreate();

        appComponent =
                DaggerAppComponent.builder()
                        .appModule(new AppModule(this))
                        .resourcesModule(new ResourcesModule(getResources()))
                        .imageModule(new ImageModule(getResources()))
                        .peopleEventsModule(new PeopleEventsModule(this))
                        .viewModule(new ViewModule(getResources()))
                        .namedaysInADayModule(new NamedaysInADayModule())
                        .build();

        appComponent.inject(this);

        initialiseDependencies();
        ErrorTracker.startTracking(this);

        DailyReminderPreferences preferences = DailyReminderPreferences.newInstance(this);
        if (preferences.isEnabled()) {
            AlarmManagerCompat alarmManager = AlarmManagerCompat.from(this);
            new DailyReminderScheduler(alarmManager, this).setupReminder(preferences);
        }
        if (FacebookPreferences.newInstance(this).isLoggedIn()) {
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            new FacebookFriendsScheduler(this, alarmManager).scheduleNext();
        }

        setupDatabaseRefresher();
    }

    private SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Log.e("Should have triggered");
        }
    };

    PreferenceChangedEventsUpdateTrigger preferenceChangedEventsUpdateTrigger;

    private void setupDatabaseRefresher() {
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        defaultSharedPreferences.registerOnSharedPreferenceChangeListener(listener);

        preferenceChangedEventsUpdateTrigger = new PreferenceChangedEventsUpdateTrigger(
                EasyPreferences.createForDefaultPreferences(this),
                getResources(),
                R.string.key_enable_namedays,
                R.string.key_nameday_lang,
                R.string.key_namedays_full_name
        );

        updater.startMonitoring(
                asList(
                        new ContactsObserver(getContentResolver()),
                        preferenceChangedEventsUpdateTrigger
                ));
    }

    protected void initialiseDependencies() {
        Log.setShowLogs(BuildConfig.DEBUG);
        JodaTimeAndroid.init(this);
        initImageLoader(this);
    }

    @SuppressWarnings("MagicNumber")
    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.MIN_PRIORITY)
                .threadPoolSize(10)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .imageDecoder(new NutraBaseImageDecoder(BuildConfig.DEBUG))
                .imageDownloader(new AndroidContactsImageDownloader(context));
        L.writeLogs(BuildConfig.DEBUG);
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config.build());
    }

    public AppComponent getApplicationModule() {
        return appComponent;
    }
}

@@ -129,95 +129,8 @@
 ext;
-%0Aimport android.content.SharedPreferences;%0Aimport android.preference.PreferenceManager;
 %0A%0Aim
@@ -2916,629 +2916,127 @@
     
-private SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() %7B%0A        @Override%0A        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) %7B%0A            Log.e(%22Should have triggered%22);%0A        %7D%0A    %7D;%0A%0A    PreferenceChangedEventsUpdateTrigger preferenceChangedEventsUpdateTrigger;%0A%0A    private void setupDatabaseRefresher() %7B%0A        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);%0A        defaultSharedPreferences.registerOnSharedPreferenceChangeListener(listener);%0A
+PreferenceChangedEventsUpdateTrigger preferenceChangedEventsUpdateTrigger;%0A%0A    private void setupDatabaseRefresher() %7B
 %0A   

Remove unused listener