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 boolean mIsOrganizer = false;
    private long mLastOrganizerCheck = 0;
    private String mCheckedId = null;
    private SharedPreferences mPreferences;

    OrganizerChecker(SharedPreferences preferences) {
        mPreferences = preferences;
        initOrganizer();
    }

    private void initOrganizer() {
        mLastOrganizerCheck = mPreferences.getLong(PREF_ORGANIZER_CHECK_TIME, 0);
        mCheckedId = mPreferences.getString(PREF_ORGANIZER_CHECK_ID, null);
        mIsOrganizer = mPreferences.getBoolean(PREF_ORGANIZER_STATE, false);
    }

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

    public boolean isOrganizer() {
        return mIsOrganizer;
    }

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

        final String currentId = PlusUtils.getCurrentPersonId(apiClient);

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

                    savePreferences();
                }

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

    private void savePreferences() {
        mPreferences.edit()
            .putLong(PREF_ORGANIZER_CHECK_TIME, mLastOrganizerCheck)
            .putString(PREF_ORGANIZER_CHECK_ID, mCheckedId)
            .putBoolean(PREF_ORGANIZER_STATE, isOrganizer())
            .apply();
    }

    public interface Callbacks {
        void onOrganizerResponse(boolean isOrganizer);

        void onErrorResponse();
    }
}

@@ -768,18 +768,17 @@
 boolean 
-mI
+i
 sOrganiz
@@ -806,18 +806,17 @@
 te long 
-mL
+l
 astOrgan
@@ -824,16 +824,25 @@
 zerCheck
+TimeStamp
  = 0;%0A  
@@ -858,18 +858,17 @@
  String 
-mC
+c
 heckedId
@@ -906,18 +906,17 @@
 erences 
-mP
+p
 referenc
@@ -974,34 +974,38 @@
 nces) %7B%0A        
-mP
+this.p
 references = pre
@@ -1081,34 +1081,33 @@
 zer() %7B%0A        
-mL
+l
 astOrganizerChec
@@ -1107,21 +1107,29 @@
 zerCheck
+TimeStamp
  = 
-mP
+p
 referenc
@@ -1170,34 +1170,33 @@
 ME, 0);%0A        
-mC
+c
 heckedId = mPref
@@ -1190,18 +1190,17 @@
 kedId = 
-mP
+p
 referenc
@@ -1244,34 +1244,33 @@
  null);%0A        
-mI
+i
 sOrganizer = mPr
@@ -1266,18 +1266,17 @@
 nizer = 
-mP
+p
 referenc
@@ -1354,34 +1354,33 @@
 zer() %7B%0A        
-mP
+p
 references.edit(
@@ -1598,18 +1598,17 @@
  return 
-mI
+i
 sOrganiz
@@ -1769,34 +1769,33 @@
 ) %7B%0A            
-mI
+i
 sOrganizer = fal
@@ -1802,34 +1802,33 @@
 se;%0A            
-mC
+c
 heckedId = null;
@@ -2054,18 +2054,17 @@
 .equals(
-mC
+c
 heckedId
@@ -2109,18 +2109,17 @@
 lis() %3E 
-mL
+l
 astOrgan
@@ -2127,16 +2127,25 @@
 zerCheck
+TimeStamp
  + ORGAN
@@ -2172,34 +2172,33 @@
 ) %7B%0A            
-mI
+i
 sOrganizer = fal
@@ -2450,18 +2450,17 @@
         
-mL
+l
 astOrgan
@@ -2468,16 +2468,25 @@
 zerCheck
+TimeStamp
  = Syste
@@ -2528,18 +2528,17 @@
         
-mC
+c
 heckedId
@@ -2563,34 +2563,33 @@
                 
-mI
+i
 sOrganizer = org
@@ -2682,34 +2682,33 @@
 ganizerResponse(
-mI
+i
 sOrganizer);%0A%0A  
@@ -2864,18 +2864,17 @@
         
-mI
+i
 sOrganiz
@@ -3043,10 +3043,9 @@
 nse(
-mI
+i
 sOrg
@@ -3119,10 +3119,9 @@
     
-mP
+p
 refe
@@ -3186,10 +3186,9 @@
 ME, 
-mL
+l
 astO
@@ -3200,16 +3200,25 @@
 zerCheck
+TimeStamp
 )%0A      
@@ -3263,10 +3263,9 @@
 ID, 
-mC
+c
 heck

Move from hungarian notation