package com.mixpanel.android.mpmetrics;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Represents a Survey, configured in Mixpanel, suitable for showing to a user.
 *
 * Unless you have disabled automatic survey display, you shouldn't need to work with this class directly.
 *
 * The typical use of this class follows the pattern.
 * <pre>
 * {@code
 *   Activity parent = this;
 *   mixpanel.getPeople().checkDecideService(new SurveyCallbacks() {
 *       {@literal @}Override
 *       public void foundSurvey(Survey survey) {
 *           if (survey != null) {
 *               mixpanel.getPeople().showSurvey(survey, parent);
 *           }
 *       }
 *   });
 * }
 * </pre>
 */
public class Survey implements Parcelable {

    public static Creator<Survey> CREATOR =
            new Creator<Survey>() {
                @Override
                public Survey createFromParcel(final Parcel source) {
                    final String jsonString = source.readString();
                    try {
                        final JSONObject json = new JSONObject(jsonString);
                        return new Survey(json);
                    } catch (JSONException e) {
                        throw new RuntimeException("Corrupted JSON object written to survey parcel.", e);
                    } catch (BadDecideObjectException e) {
                        throw new RuntimeException("Unexpected or incomplete object written to survey parcel.", e);
                    }
                }

                @Override
                public Survey[] newArray(final int size) {
                    return new Survey[size];
                }
            };

    /* package */ Survey(JSONObject description) throws BadDecideObjectException {
        try {
            mDescription = description;
            mId = description.getInt("id");
            final JSONArray collectionsJArray = description.getJSONArray("collections");
            final JSONObject collection0 = collectionsJArray.getJSONObject(0);
            mCollectionId = collection0.getInt("id");

            final JSONArray questionsJArray = description.getJSONArray("questions");
            if (questionsJArray.length() == 0) {
                throw new BadDecideObjectException("Survey has no questions.");
            }
            final List<Question> questionsList = new ArrayList<Question>(questionsJArray.length());
            for (int i = 0; i < questionsJArray.length(); i++) {
                final JSONObject q = questionsJArray.getJSONObject(i);
                questionsList.add(new Question(q));
            }
            mQuestions = Collections.unmodifiableList(questionsList);
        } catch (final JSONException e) {
            throw new BadDecideObjectException("Survey JSON was unexpected or bad", e);
        }
    }

    /* package */ String toJSON() {
        return mDescription.toString();
    }

    public int getId() {
        return mId;
    }

    public int getCollectionId() {
        return mCollectionId;
    }

    public List<Question> getQuestions() {
        return mQuestions;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeString(toJSON());
    }

    public enum QuestionType {
        UNKNOWN {
            @Override
           public String toString() {
                return "*unknown_type*";
            }
        },
        MULTIPLE_CHOICE {
            @Override
            public String toString() {
                return "multiple_choice";
            }
        },
        TEXT {
            @Override
            public String toString() {
                return "text";
            }
        };
    };

    public class Question {
        private Question(JSONObject question) throws JSONException, BadDecideObjectException {
            mQuestionId = question.getInt("id");
            mQuestionType = question.getString("type");
            mPrompt = question.getString("prompt");

            List<String> choicesList = Collections.<String>emptyList();
            if (question.has("extra_data")) {
                final JSONObject extraData = question.getJSONObject("extra_data");
                if (extraData.has("$choices")) {
                    final JSONArray choices = extraData.getJSONArray("$choices");
                    choicesList = new ArrayList<String>(choices.length());
                    for (int i = 0; i < choices.length(); i++) {
                        choicesList.add(choices.getString(i));
                    }
                }
            }
            mChoices = Collections.unmodifiableList(choicesList);
            if (getType() == QuestionType.MULTIPLE_CHOICE && mChoices.size() == 0) {
                throw new BadDecideObjectException("Question is multiple choice but has no answers:" + question.toString());
            }
        }

        public int getId() {
            return mQuestionId;
        }

        public String getPrompt() {
            return mPrompt;
        }

        public List<String> getChoices() {
            return mChoices;
        }

        public QuestionType getType() {
            if (QuestionType.MULTIPLE_CHOICE.toString().equals(mQuestionType)) {
                return QuestionType.MULTIPLE_CHOICE;
            }
            if (QuestionType.TEXT.toString().equals(mQuestionType)) {
                return QuestionType.TEXT;
            }
            return QuestionType.UNKNOWN;
        }

        private final int mQuestionId;
        private final String mQuestionType;
        private final String mPrompt;
        private final List<String> mChoices;
    }

    private final JSONObject mDescription;
    private final int mId;
    private final int mCollectionId;
    private final List<Question> mQuestions;
}

@@ -38,65 +38,8 @@
 s;%0A%0A
-import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0A
 impo
@@ -207,162 +207,131 @@
 t;%0A%0A
-/**%0A * Represents a Survey, configured in Mixpanel, suitable for showing to a user.%0A *%0A * Unless you have disabled automatic survey display, you shouldn't
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0A/**%0A * Represents a Survey, configured in Mixpanel.%0A *%0A * You only
  nee
@@ -360,422 +360,109 @@
 ass 
-directly.%0A *%0A * The typical use of this class follows the pattern.%0A * %3Cpre%3E%0A * %7B@code%0A *   Activity parent = this;%0A *   mixpanel.getPeople().checkDecideService(new SurveyCallbacks() %7B%0A *       %7B@literal @%7DOverride%0A *       public void foundSurvey(Survey survey) %7B%0A *           if (survey != null) %7B%0A *               mixpanel.getPeople().showSurvey(survey, parent);%0A *           %7D%0A *       %7D%0A *   %7D);%0A * %7D%0A * %3C/pre%3E
+if you call getSurveyIfAvailable() and want to%0A * display a custom interface for the survey yourself.
 %0A */
@@ -550,28 +550,16 @@
 REATOR =
-%0A           
  new Cre
@@ -579,32 +579,24 @@
 ) %7B%0A        
-        
 @Override%0A  
@@ -589,32 +589,24 @@
   @Override%0A
-        
         publ
@@ -663,32 +663,24 @@
             
-        
 final String
@@ -726,38 +726,22 @@
         
-        
 try %7B%0A
-        
         
@@ -812,32 +812,24 @@
             
-        
 return new S
@@ -849,32 +849,24 @@
             
-        
 %7D catch (JSO
@@ -877,32 +877,24 @@
 eption e) %7B%0A
-        
             
@@ -975,32 +975,24 @@
 rcel.%22, e);%0A
-        
             
@@ -1042,32 +1042,24 @@
             
-        
 throw new Ru
@@ -1154,52 +1154,29 @@
     
-        %7D%0A                %7D%0A%0A       
+%7D%0A        %7D%0A%0A
         
  @Ov
@@ -1163,33 +1163,32 @@
      %7D%0A%0A        
- 
 @Override%0A      
@@ -1177,32 +1177,24 @@
   @Override%0A
-        
         publ
@@ -1240,32 +1240,24 @@
             
-        
 return new S
@@ -1277,34 +1277,18 @@
         
-        %7D%0A        
+%7D%0A
     %7D;%0A%0A

Remove old code example + formatting