package com.alwaysallthetime.messagebeast;

import com.alwaysallthetime.adnlib.AppDotNetClient;
import com.alwaysallthetime.adnlib.data.Configuration;
import com.alwaysallthetime.adnlib.response.ConfigurationResponseHandler;

import java.util.Calendar;
import java.util.Date;

public class ConfigurationUtility {
    public static void updateConfiguration(AppDotNetClient client) {
        Date configurationSaveDate = ADNSharedPreferences.getConfigurationSaveDate();
        boolean fetchNewConfig = configurationSaveDate == null;
        if(!fetchNewConfig) {
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(configurationSaveDate);
            cal2.setTime(new Date());
            boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                    cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
            fetchNewConfig = !sameDay;
        }
        if(fetchNewConfig) {
            client.retrieveConfiguration(new ConfigurationResponseHandler() {
                @Override
                public void onSuccess(Configuration responseData) {
                    ADNSharedPreferences.saveConfiguration(responseData);
                }
            });
        }
    }
}

@@ -310,30 +310,336 @@
 y %7B%0A
-    public static void
+%0A    /**%0A     * Update the Configuration if due.%0A     *%0A     * http://developers.app.net/docs/resources/config/#how-to-use-the-configuration-object%0A     *%0A     * @param client the AppDotNetClient to use for the request%0A     * @return true if the configuration is being fetched, false otherwise.%0A     */%0A    public static boolean
  upd
@@ -1586,20 +1586,51 @@
 ;%0A        %7D%0A
+        return fetchNewConfig;%0A
     %7D%0A%7D%0A

return true if configuration is being updated, false otherwise.