package org.commcare.android.logging;

import android.content.SharedPreferences;

import org.commcare.CommCareApp;
import org.commcare.CommCareApplication;
import org.commcare.network.HttpRequestGenerator;
import org.commcare.session.CommCareSession;
import org.commcare.suite.model.Profile;
import org.commcare.utils.SessionStateUninitException;

/**
 * Created by amstone326 on 2/11/16.
 */
public class ReportingUtils {

    public static int getAppBuildNumber() {
        CommCareApp app = CommCareApplication.instance().getCurrentApp();
        if (app != null) {
            Profile profile = app.getCommCarePlatform().getCurrentProfile();
            if (profile != null) {
                return profile.getVersion();
            }
        }
        return -1;
    }

    public static String getAppId() {
        try {
            CommCareApp app = CommCareApplication.instance().getCurrentApp();
            if (app != null) {
                Profile profile = app.getCommCarePlatform().getCurrentProfile();
                if (profile != null) {
                    return profile.getUniqueId();
                }
            }
            return "";
        } catch (NullPointerException npe) {
            // don't fail hard, return empty string
            return "";
        }
    }

    public static String getCurrentSession() {
        CommCareSession currentSession;
        try {
            currentSession = CommCareApplication.instance().getCurrentSession();
            return currentSession.getFrame().toString();
        } catch (SessionStateUninitException e) {
            return "";
        }
    }

    /*
     * Helper methods for ACRA and user reporting. Catch broad exception so we never crash
     * when trying to file a bug.
     */

    public static String getDomain() {
        try {
            SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
            return prefs.getString(HttpRequestGenerator.USER_DOMAIN_SUFFIX, "not found");
        } catch (Exception e) {
            return "Domain not set.";
        }
    }

    public static String getPostURL() {
        try {
            SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
            return prefs.getString(HttpRequestGenerator.USER_DOMAIN_SUFFIX, "not found");
        } catch (Exception e) {
            return "PostURL not set.";
        }
    }

    public static String getUser() {
        try {
            return CommCareApplication.instance().getSession().getLoggedInUser().getUsername();
        } catch (Exception e) {
            return "User not logged in.";
        }
    }

    public static String getVersion() {
        try {
            return CommCareApplication.instance().getCurrentVersionString();
        } catch (Exception e) {
            return "Version not set.";
        }
    }

}

@@ -807,34 +807,16 @@
 pId() %7B%0A
-        try %7B%0A    
         
@@ -877,28 +877,24 @@
 rrentApp();%0A
-    
         if (
@@ -912,36 +912,32 @@
 ) %7B%0A            
-    
 Profile profile 
@@ -989,36 +989,32 @@
 ();%0A            
-    
 if (profile != n
@@ -1028,36 +1028,32 @@
                 
-   
- 
 return profile.g
@@ -1070,36 +1070,32 @@
 ();%0A            
-    
 %7D%0A            %7D%0A
@@ -1088,32 +1088,26 @@
         
- 
-   %7D%0A  
+%7D%0A
         
   return
@@ -1090,34 +1090,32 @@
       %7D%0A        
-  
 return %22%22;%0A     
@@ -1113,138 +1113,8 @@
 %22%22;%0A
-        %7D catch (NullPointerException npe) %7B%0A            // don't fail hard, return empty string%0A            return %22%22;%0A        %7D%0A
     

Revert "never fail hard if we can't get the appid"