package com.alexstyl.specialdates.events.peopleevents;

import android.content.Context;
import android.os.Handler;

import com.alexstyl.specialdates.MementoApplication;
import com.alexstyl.specialdates.events.namedays.NamedayDatabaseRefresher;
import com.alexstyl.specialdates.events.namedays.NamedayPreferences;
import com.alexstyl.specialdates.upcoming.NamedaySettingsMonitor;
import com.alexstyl.specialdates.util.ContactsObserver;

class PeopleEventsUpdater {

    private final BirthdayDatabaseRefresher birthdayDatabaseRefresher;
    private final EventPreferences eventPreferences;
    private final NamedaySettingsMonitor namedayMonitor;
    private final ContactsObserver contactsObserver;

    private NamedayDatabaseRefresher namedayDatabaseRefresher;

    static PeopleEventsUpdater newInstance(Context context) {
        BirthdayDatabaseRefresher birthdayDatabaseRefresher = BirthdayDatabaseRefresher.newInstance(context);
        NamedayDatabaseRefresher namedayDatabaseRefresher = NamedayDatabaseRefresher.newInstance(context);
        EventPreferences eventPreferences = new EventPreferences(context);
        ContactsObserver contactsObserver = new ContactsObserver(context.getContentResolver(), new Handler());
        NamedaySettingsMonitor namedaySettingsMonitor = new NamedaySettingsMonitor(NamedayPreferences.newInstance(context));
        return new PeopleEventsUpdater(birthdayDatabaseRefresher, namedayDatabaseRefresher, eventPreferences, contactsObserver, namedaySettingsMonitor);
    }

    PeopleEventsUpdater(BirthdayDatabaseRefresher birthdayDatabaseRefresher,
                        NamedayDatabaseRefresher namedayDatabaseRefresher, EventPreferences eventPreferences, ContactsObserver contactsObserver, NamedaySettingsMonitor namedayMonitor) {
        this.birthdayDatabaseRefresher = birthdayDatabaseRefresher;
        this.namedayDatabaseRefresher = namedayDatabaseRefresher;
        this.eventPreferences = eventPreferences;
        this.contactsObserver = contactsObserver;
        this.namedayMonitor = namedayMonitor;
    }

    public void updateEventsIfNeeded() {
        boolean isFirstRun = isFirstTimeRunning();
        if (isFirstRun) {
            birthdayDatabaseRefresher.refreshBirthdays();
            namedayDatabaseRefresher.refreshNamedaysIfEnabled();
            eventPreferences.markEventsAsInitialised();
        } else {
            updateEventsIfSettingsChanged();
        }
    }

    private boolean isFirstTimeRunning() {
        return !eventPreferences.hasBeenInitialised();
    }

    private void updateEventsIfSettingsChanged() {
        boolean wereContactsUpdated = contactsObserver.wereContactsUpdated();
        boolean wereNamedaysSettingsUpdated = namedayMonitor.dataWasUpdated();

        if (wereNamedaysSettingsUpdated) {
            namedayDatabaseRefresher = NamedayDatabaseRefresher.newInstance(MementoApplication.getContext());
        }
        if (wereContactsUpdated) {
            birthdayDatabaseRefresher.refreshBirthdays();
        }
        if (wereContactsUpdated || wereNamedaysSettingsUpdated) {
            namedayDatabaseRefresher.refreshNamedaysIfEnabled();
        }
        resetMonitorFlags();
    }

    private void resetMonitorFlags() {
        namedayMonitor.refreshData();
        contactsObserver.resetFlag();
    }

    public void register() {
        contactsObserver.register();
    }
}

@@ -109,16 +109,63 @@
 ndler;%0A%0A
+import com.alexstyl.specialdates.ErrorTracker;%0A
 import c
@@ -353,16 +353,80 @@
 rences;%0A
+import com.alexstyl.specialdates.permissions.PermissionChecker;%0A
 import c
@@ -802,16 +802,71 @@
 bserver;
+%0A    private final PermissionChecker permissionChecker;
 %0A%0A    pr
@@ -1509,24 +1509,102 @@
 (context));%0A
+        PermissionChecker permissionChecker = new PermissionChecker(context);%0A
         retu
@@ -1630,16 +1630,33 @@
 Updater(
+%0A                
 birthday
@@ -1673,16 +1673,32 @@
 fresher,
+%0A               
  nameday
@@ -1715,16 +1715,32 @@
 fresher,
+%0A               
  eventPr
@@ -1749,16 +1749,32 @@
 erences,
+%0A               
  contact
@@ -1783,16 +1783,32 @@
 bserver,
+%0A               
  nameday
@@ -1822,16 +1822,60 @@
 sMonitor
+,%0A                permissionChecker%0A        
 );%0A    %7D
@@ -2135,16 +2135,53 @@
 yMonitor
+, PermissionChecker permissionChecker
 ) %7B%0A    
@@ -2464,51 +2464,293 @@
     
-%7D%0A%0A    public void updateEventsIfNeeded() %7B
+    this.permissionChecker = permissionChecker;%0A    %7D%0A%0A    public void updateEventsIfNeeded() %7B%0A        if (!permissionChecker.canReadAndWriteContacts()) %7B%0A            ErrorTracker.track(new RuntimeException(%22Tried to update events without permission%22));%0A            return;%0A        %7D%0A
 %0A   
@@ -3070,24 +3070,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    p

Check permission before updating people events