package org.commcare.utils;

/**
 * @author Phillip Mates (pmates@dimagi.com)
 */
public enum FormUploadResult {
    /**
     * Everything worked great!
     */
    FULL_SUCCESS(0),

    /**
     * There was a problem with the server's response
     */
    FAILURE(1),

    /**
     * The server returned an authentication error
     */
    AUTH_FAILURE(2),

    /**
     * There was a problem with the transport layer during transit
     */
    TRANSPORT_FAILURE(3),

    /**
     * There is a problem with this record that prevented submission success
     */
    RECORD_FAILURE(4),

    /**
     * The user session ended while trying to upload a form
     */
    PROGRESS_LOGGED_OUT(5),

    PROGRESS_SDCARD_REMOVED(6),

    /**
     * HQ received the form submission but encountered an error while processing it, so the
     * form has not resulted in any changes to the HQ database
     */
    PROCESSING_FAILURE(7);

    private final int orderVal;
    public String processingFailureReason;

    FormUploadResult(int orderVal) {
        this.orderVal = orderVal;
    }

    public void setProcessingFailureReason(String s) {
        this.processingFailureReason = s;
    }

    public static FormUploadResult getWorstResult(FormUploadResult[] results) {
        FormUploadResult worstResult = FULL_SUCCESS;
        for (FormUploadResult result : results) {
            if (result.orderVal > worstResult.orderVal) {
                worstResult = result;
            }
        }
        return worstResult;
    }
}

@@ -195,138 +195,367 @@
   * 
-There was a problem with the server's response%0A     */%0A    FAILURE(1),%0A%0A    /**%0A     * The server returned an authentication error
+Non-specific error used as default assumption before attempting submission, and when%0A     * certain exceptions are encountered (IOException or TaskCancelledException)%0A     */%0A    FAILURE(1),%0A%0A    /**%0A     * There is a problem with this record that prevented submission success for this record%0A     * specifically, but others will be allowed to continue trying
 %0A   
@@ -567,12 +567,14 @@
     
-AUTH
+RECORD
 _FAI
@@ -602,67 +602,250 @@
   * 
-There was a problem with the transport layer during transit
+HQ received the form submission but encountered an error while processing it, so the%0A     * form has not resulted in any changes to the HQ database%0A     */%0A    PROCESSING_FAILURE(3),%0A%0A    /**%0A     * The server returned an authentication error
 %0A   
@@ -857,17 +857,12 @@
     
-TRANSPORT
+AUTH
 _FAI
@@ -858,33 +858,33 @@
    AUTH_FAILURE(
-3
+4
 ),%0A%0A    /**%0A    
@@ -884,33 +884,34 @@
 **%0A     * There 
-i
+wa
 s a problem with
@@ -917,51 +917,40 @@
 h th
-is record that prevented submission success
+e transport layer during transit
 %0A   
@@ -950,38 +950,41 @@
 sit%0A     */%0A    
-RECORD
+TRANSPORT
 _FAILURE(4),%0A%0A  
@@ -968,33 +968,33 @@
 ANSPORT_FAILURE(
-4
+5
 ),%0A%0A    /**%0A    
@@ -1081,17 +1081,17 @@
 GED_OUT(
-5
+6
 ),%0A%0A    
@@ -1118,207 +1118,8 @@
 VED(
-6),%0A%0A    /**%0A     * HQ received the form submission but encountered an error while processing it, so the%0A     * form has not resulted in any changes to the HQ database%0A     */%0A    PROCESSING_FAILURE(
 7);%0A

reorder FormUploadResults to reflect actual 'best to worst' ordering