package it.near.sdk;

import android.content.Context;
import android.content.SharedPreferences;

import it.near.sdk.Communication.Constants;

/**
 * Created by cattaneostefano on 12/04/16.
 */
public class GlobalConfig {

    private static GlobalConfig mInstance = null;
    private Context mContext;
    private final String APIKEY = "apikey";
    private String apiKey;
    private final String APPID = "appid";
    private String appId;
    private final String SENDERID = "senderid";
    private String senderId;
    private final String NOTIFICATIONIMAGE = "notification_image";
    private int notificationImage = 0;
    private SharedPreferences sp;
    private String prefsNameSuffix = "NearConfig";
    private SharedPreferences.Editor editor;

    public GlobalConfig(Context mContext) {
        this.mContext = mContext;
        setUpSharedPreferences();
    }

    private void setUpSharedPreferences() {
        String PACK_NAME = mContext.getApplicationContext().getPackageName();
        String PREFS_NAME = PACK_NAME + prefsNameSuffix;
        sp = mContext.getSharedPreferences(PREFS_NAME, 0);
        editor = sp.edit();
    }

    public static GlobalConfig getInstance(Context context){
        if(mInstance == null)
        {
            mInstance = new GlobalConfig(context);
        }
        return mInstance;
    }

    public int getNotificationImage() {
        if (notificationImage == 0){
            notificationImage = sp.getInt(NOTIFICATIONIMAGE, 0);
        }
        return notificationImage;
    }

    public void setNotificationImage(int notificationImage) {
        this.notificationImage = notificationImage;
        editor.putInt(NOTIFICATIONIMAGE, notificationImage).apply();
    }


    public String getApiKey() {
        if (apiKey == null){
            apiKey = sp.getString(APIKEY, null);
        }
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
        editor.putString(APIKEY, apiKey).apply();
    }

    public String getAppId() {
        if (appId == null){
            appId = sp.getString(APPID, null);
        }
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
        editor.putString(APPID, appId);
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
        editor.putString(SENDERID, senderId).apply();
    }

    public String getSenderId() {
        if (senderId == null){
            senderId = sp.getString(SENDERID, null);
        }
        return senderId;
    }
}

@@ -507,24 +507,212 @@
 g senderId;%0A
+    private final String DEVICETOKEN = %22devicetoken%22;%0A    private String deviceToken;%0A    private static final String INSTALLATIONID = %22installationid%22;%0A    private String installationId;%0A
     private 
@@ -1989,22 +1989,24 @@
 piKey = 
-sp.
 get
+Local
 String(A
@@ -2010,22 +2010,16 @@
 g(APIKEY
-, null
 );%0A     
@@ -2130,34 +2130,32 @@
 ey;%0A        
-editor.put
+setLocal
 String(APIKE
@@ -2164,24 +2164,16 @@
  apiKey)
-.apply()
 ;%0A    %7D%0A
@@ -2248,30 +2248,32 @@
     appId = 
-sp.
 get
+Local
 String(APPID
@@ -2272,22 +2272,16 @@
 ng(APPID
-, null
 );%0A     
@@ -2387,34 +2387,32 @@
 Id;%0A        
-editor.put
+setLocal
 String(APPID
@@ -2420,32 +2420,189 @@
  appId);%0A    %7D%0A%0A
+%0A    public String getSenderId() %7B%0A        if (senderId == null)%7B%0A            senderId = getLocalString(SENDERID);%0A        %7D%0A        return senderId;%0A    %7D%0A%0A
     public void 
@@ -2674,26 +2674,24 @@
         
-editor.put
+setLocal
 String(S
@@ -2712,52 +2712,371 @@
 rId)
-.apply();%0A    %7D%0A%0A    public String getSender
+;%0A    %7D%0A%0A    public String getDeviceToken() %7B%0A        if (deviceToken == null)%7B%0A            deviceToken = getLocalString(DEVICETOKEN);%0A        %7D%0A        return deviceToken;%0A    %7D%0A%0A    public void setDeviceToken(String deviceToken) %7B%0A        this.deviceToken = deviceToken;%0A        setLocalString(DEVICETOKEN, deviceToken);%0A    %7D%0A%0A    public String getInstallation
 Id()
@@ -3082,38 +3082,44 @@
 ) %7B%0A        if (
-sender
+installation
 Id == null)%7B%0A   
@@ -3131,88 +3131,479 @@
     
-senderId = sp.getString(SENDERID, null
+installationId = getLocalString(INSTALLATIONID);%0A        %7D%0A        return installationId;%0A    %7D%0A%0A    public void setInstallationId(String installationId)%7B%0A        this.installationId = installationId;%0A        setLocalString(INSTALLATIONID, installationId);%0A    %7D%0A%0A    private void setLocalString(String name, String value)%7B%0A        editor.putString(name, value).apply(
 );%0A    
+%7D%0A%0A
     
-%7D%0A        return senderId
+private String getLocalString(String name)%7B%0A        return sp.getString(name, null)
 ;%0A    %7D%0A
 %7D%0A
@@ -3600,10 +3600,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Add device token and installation id to the global config