package org.gdg.frisbee.android.app;

import android.content.SharedPreferences;
import android.text.format.DateUtils;

import com.google.android.gms.common.api.GoogleApiClient;

import org.gdg.frisbee.android.api.Callback;
import org.gdg.frisbee.android.api.model.OrganizerCheckResponse;
import org.gdg.frisbee.android.utils.PlusUtils;
import org.gdg.frisbee.android.utils.PrefUtils;

public class OrganizerChecker {

    private static final String PREF_ORGANIZER_CHECK_TIME = "pref_organizer_check_time";
    private static final String PREF_ORGANIZER_CHECK_ID = "pref_organizer_check_id";
    private static final String PREF_ORGANIZER_STATE = "pref_organizer_state";
    private static final long ORGANIZER_CHECK_MAX_TIME = DateUtils.WEEK_IN_MILLIS;

    private final SharedPreferences preferences;

    private boolean isOrganizer = false;
    private long lastOrganizerCheckTimeStamp = 0;
    private String checkedId = null;

    OrganizerChecker(SharedPreferences preferences) {
        this.preferences = preferences;
        initOrganizer();
    }

    private void initOrganizer() {
        lastOrganizerCheckTimeStamp = preferences.getLong(PREF_ORGANIZER_CHECK_TIME, 0);
        checkedId = preferences.getString(PREF_ORGANIZER_CHECK_ID, null);
        isOrganizer = preferences.getBoolean(PREF_ORGANIZER_STATE, false);
    }

    void resetOrganizer() {
        preferences.edit()
            .putBoolean(OrganizerChecker.PREF_ORGANIZER_STATE, false)
            .putLong(OrganizerChecker.PREF_ORGANIZER_CHECK_TIME, 0)
            .apply();
    }

    public boolean isOrganizer() {
        return isOrganizer;
    }

    void checkOrganizer(GoogleApiClient apiClient, final Callbacks responseHandler) {
        if (!PrefUtils.isSignedIn(apiClient.getContext())) {
            isOrganizer = false;
            checkedId = null;
            responseHandler.onOrganizerResponse(false);
            return;
        }

        final String currentId = PlusUtils.getCurrentPersonId(apiClient);

        if (currentId != null
            && (!currentId.equals(checkedId)
            || System.currentTimeMillis() > lastOrganizerCheckTimeStamp + ORGANIZER_CHECK_MAX_TIME)) {
            isOrganizer = false;
            App.getInstance().getGdgXHub().checkOrganizer(currentId).enqueue(new Callback<OrganizerCheckResponse>() {
                @Override
                public void success(OrganizerCheckResponse organizerCheckResponse) {
                    lastOrganizerCheckTimeStamp = System.currentTimeMillis();
                    checkedId = currentId;
                    isOrganizer = organizerCheckResponse.getChapters().size() > 0;
                    responseHandler.onOrganizerResponse(isOrganizer);

                    savePreferences();
                }

                @Override
                public void failure(Throwable error) {
                    isOrganizer = false;
                    responseHandler.onErrorResponse();
                }
            });
        } else {
            responseHandler.onOrganizerResponse(isOrganizer);
        }
    }

    private void savePreferences() {
        preferences.edit()
            .putLong(PREF_ORGANIZER_CHECK_TIME, lastOrganizerCheckTimeStamp)
            .putString(PREF_ORGANIZER_CHECK_ID, checkedId)
            .putBoolean(PREF_ORGANIZER_STATE, isOrganizer())
            .apply();
    }

    public interface Callbacks {
        void onOrganizerResponse(boolean isOrganizer);

        void onErrorResponse();
    }
}

@@ -332,56 +332,8 @@
 ils;
-%0Aimport org.gdg.frisbee.android.utils.PrefUtils;
 %0A%0Apu
@@ -1675,57 +1675,104 @@
     
-if (!PrefUtils.isSignedIn(apiClient.getContext())
+final String currentId = PlusUtils.getCurrentPersonId(apiClient);%0A%0A        if (currentId == null
 ) %7B%0A
@@ -1933,86 +1933,37 @@
     
-final String currentId = PlusUtils.getCurrentPersonId(apiClient);%0A%0A        if 
+if (isLastOrganizerCheckValid
 (cur
@@ -1972,16 +1972,12 @@
 ntId
- != null
+)) %7B
 %0A   
@@ -1989,148 +1989,89 @@
     
-&& (!currentId.equals(checkedId)%0A            %7C%7C System.currentTimeMillis() %3E lastOrganizerCheckTimeStamp + ORGANIZER_CHECK_MAX_TIME)) %7B%0A    
+responseHandler.onOrganizerResponse(isOrganizer);%0A            return;%0A        %7D%0A%0A
     
@@ -2087,36 +2087,32 @@
 anizer = false;%0A
-    
         App.getI
@@ -2213,36 +2213,32 @@
 ) %7B%0A            
-    
 @Override%0A      
@@ -2235,36 +2235,32 @@
 ide%0A            
-  
-  
 public void succ
@@ -2324,28 +2324,24 @@
             
-    
 lastOrganize
@@ -2394,36 +2394,32 @@
                 
-  
-  
 checkedId = curr
@@ -2425,20 +2425,16 @@
 rentId;%0A
-    
         
@@ -2512,36 +2512,32 @@
                 
-  
-  
 responseHandler.
@@ -2587,20 +2587,16 @@
         
-    
 savePref
@@ -2614,35 +2614,27 @@
             
-  
-  
 %7D%0A%0A
-    
             
@@ -2647,36 +2647,32 @@
 ide%0A            
-  
-  
 public void fail
@@ -2702,36 +2702,32 @@
                 
-    
 isOrganizer = fa
@@ -2739,36 +2739,32 @@
                 
-  
-  
 responseHandler.
@@ -2782,32 +2782,38 @@
 onse();%0A        
+    %7D%0A
         %7D%0A      
@@ -2809,113 +2809,227 @@
    %7D
+);
 %0A    
+%7D%0A%0A
     
-    %7D);%0A        %7D else %7B%0A            responseHandler.onOrganizerResponse(isOrganizer);%0A        %7D
+private boolean isLastOrganizerCheckValid(String currentId) %7B%0A        return currentId.equals(checkedId)%0A            && System.currentTimeMillis() %3C= lastOrganizerCheckTimeStamp + ORGANIZER_CHECK_MAX_TIME;
 %0A   

Remove the usage of `isSignedIn` by doing a null check on the user id