package org.edx.mobile.module.analytics;

import org.edx.mobile.R;
import org.edx.mobile.util.Environment;
import org.edx.mobile.util.LogUtil;

import android.content.Context;

import com.segment.analytics.Analytics;
import com.segment.analytics.AnalyticsContext;
import com.segment.analytics.Options;
import com.segment.analytics.Properties;
import com.segment.analytics.Traits;

public class SegmentTracker {
    
    private Analytics analytics;
    
    public SegmentTracker(Context context) {
        try {
            String writeKey = Environment.getInstance().getConfig().getSegmentIOWriteKey();
            String debugging = context.getString(R.string.analytics_debug);
            int queueSize = context.getResources().getInteger(R.integer.analytics_queue_size);

            if(writeKey!=null) {
                LogUtil.log(getClass().getName(), "SegmentTracker created with write key: " + writeKey);
                // Now Analytics.with will return the custom instance
                analytics = new Analytics.Builder(context, writeKey)
                        .debugging(Boolean.parseBoolean(debugging))
                        .queueSize(queueSize)
                        .build();
            }
        } catch(RuntimeException ex) {
            ex.printStackTrace();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * This function is used to reset the user/
     * flush all remaining events for particular user
     */
    public void resetIdentifyUser() {
        try {
            if (analytics != null) {
                analytics.flush();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Calls track method of Analytics.
     * @param event
     * @param props
     */
    public void track(String event, Properties props) {
        try {
            if (analytics != null) {
                analytics.track(event, props);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Calls screen method of Analytics.
     * @param category
     * @param name
     */
    public  void screen(String category, String name, Properties properties) {
        try {
            if (analytics != null) {
                analytics.screen(category, name, properties);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Calls identify method of Analytics.
     * @param id
     * @param traits
     * @param options
     */
    public void identify(String id, Traits traits, Options options) {
        try {
            if (analytics != null) {
                analytics.identify(id, traits, options);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * This method is unused in GA release of the app.
     * @return
     */
    private AnalyticsContext getContext() {
        try {
            if (analytics != null) {
                AnalyticsContext analyticsContext = analytics.getAnalyticsContext(); 
                return analyticsContext;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    private void logout() {
        try {
            if (analytics != null) {
                analytics.logout();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

@@ -404,20 +404,58 @@
 acker %7B%0A
+%0A
     
+/* Singleton instance of Analytics */
 %0A    pri
@@ -451,32 +451,39 @@
  */%0A    private 
+static 
 Analytics analyt
@@ -533,24 +533,61 @@
  context) %7B%0A
+        if (analytics == null) %7B%0A    
         try 
@@ -580,32 +580,36 @@
           try %7B%0A
+    
             Stri
@@ -692,24 +692,28 @@
             
+    
 String debug
@@ -776,16 +776,20 @@
         
+ 
+   
 int queu
@@ -876,18 +876,23 @@
         
+    
 if
+ 
 (writeKe
@@ -892,18 +892,20 @@
 writeKey
+ 
 !=
+ 
 null) %7B%0A
@@ -920,16 +920,20 @@
         
+    
 LogUtil.
@@ -1029,16 +1029,20 @@
         
+    
 // Now A
@@ -1095,32 +1095,36 @@
                 
+  
+  
 analytics = new 
@@ -1184,16 +1184,20 @@
         
+    
 .debuggi
@@ -1232,16 +1232,20 @@
 gging))%0A
+    
         
@@ -1306,16 +1306,20 @@
         
+    
 .build()
@@ -1328,34 +1328,40 @@
             
-%7D%0A
+    %7D%0A  
         
 %7D catch(Runt
@@ -1348,23 +1348,26 @@
         
+  
 %7D catch
+ 
 (Runtime
@@ -1386,32 +1386,36 @@
 ) %7B%0A            
+  
+  
 ex.printStackTra
@@ -1420,39 +1420,44 @@
 race();%0A        
+   
+ 
 %7D catch
+ 
 (Exception ex) %7B
@@ -1461,32 +1461,36 @@
 ) %7B%0A            
+   
+ 
 ex.printStackTra
@@ -1487,32 +1487,46 @@
 ntStackTrace();%0A
+            %7D%0A
         %7D%0A    %7D%0A

Use singleton instance of segment Analytics