package com.garpr.android.data2;


import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.garpr.android.App;
import com.garpr.android.misc.Console;
import com.garpr.android.misc.Constants;
import com.garpr.android.misc.Heartbeat;

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


abstract class Call<T> extends Thread implements ErrorListener, Listener<JSONObject> {


    private Exception mException;
    private JSONObject mJSONResponse;
    protected final Response<T> mResponse;




    Call(final Response<T> response) throws IllegalArgumentException {
        if (response == null) {
            throw new IllegalArgumentException("Response can't be null");
        }

        mResponse = response;
    }


    String getBaseUrl() {
        return Constants.API_URL + '/';
    }


    abstract String getCallName();


    abstract JsonObjectRequest getRequest();


    final void make() {
        final Heartbeat heartbeat = mResponse.getHeartbeat();

        if (mResponse.isAlive() && heartbeat != null && heartbeat.isAlive()) {
            final JsonObjectRequest request = getRequest();
            request.setTag(heartbeat);

            final RequestQueue requestQueue = App.getRequestQueue();
            requestQueue.add(request);
        } else {
            Console.d(getCallName(), "Call canceled before being queued");
        }
    }


    @Override
    public final void onErrorResponse(final VolleyError error) {
        Console.e(getCallName(), "Network error", error);

        if (mResponse.isAlive()) {
            mException = error;
            start();
        } else {
            Console.d(getCallName(), "Call canceled after response was received");
        }
    }


    abstract void onJSONResponse(final JSONObject json) throws JSONException;


    @Override
    public final void onResponse(final JSONObject response) {
        if (mResponse.isAlive()) {
            mJSONResponse = response;
            start();
        } else {
            Console.d(getCallName(), "Call canceled after response was received");
        }
    }


    @Override
    public final void run() {
        super.run();

        if (mResponse.isAlive()) {
            if (mException != null) {
                mResponse.error(mException);
            } else if (mJSONResponse != null) {
                try {
                    onJSONResponse(mJSONResponse);
                } catch (final JSONException e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new IllegalStateException(getCallName() + " Thread started but no response data available");
            }
        } else {
            Console.d(getCallName(), "Call canceled after Thread began");
        }
    }


    @Override
    public final String toString() {
        return getCallName();
    }


}

@@ -494,23 +494,8 @@
 l%3CT%3E
- extends Thread
  imp
@@ -516,16 +516,27 @@
 istener,
+ Heartbeat,
  Listene
@@ -557,80 +557,8 @@
 %7B%0A%0A%0A
-    private Exception mException;%0A    private JSONObject mJSONResponse;%0A
     
@@ -992,27 +992,179 @@
     
-final void make() %7B
+@Override%0A    public final boolean isAlive() %7B%0A        return mResponse.isAlive();%0A    %7D%0A%0A%0A    final void make() %7B%0A        if (!isAlive()) %7B%0A            return;%0A        %7D%0A
 %0A   
@@ -1239,31 +1239,8 @@
 if (
-mResponse.isAlive() && 
 hear
@@ -1249,19 +1249,20 @@
 eat 
-!
+=
 = null 
-&& 
+%7C%7C !
 hear
@@ -1276,32 +1276,59 @@
 sAlive()) %7B%0A    
+        return;%0A        %7D%0A%0A
         final Js
@@ -1371,28 +1371,24 @@
 ();%0A        
-    
 request.setT
@@ -1407,28 +1407,24 @@
 );%0A%0A        
-  
-  
 final Reques
@@ -1472,28 +1472,24 @@
 ();%0A        
-    
 requestQueue
@@ -1507,110 +1507,8 @@
 t);%0A
-        %7D else %7B%0A            Console.d(getCallName(), %22Call canceled before being queued%22);%0A        %7D%0A
     
@@ -1657,34 +1657,25 @@
         if (
-mResponse.
+!
 isAlive()) %7B
@@ -1691,158 +1691,290 @@
     
-mException = error;%0A            start();%0A        %7D else %7B%0A            Console.d(getCallName(), %22Call canceled after response was received%22);%0A        %7D
+return;%0A        %7D%0A%0A        final Runnable runnable = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                if (isAlive()) %7B%0A                    mResponse.error(error);%0A                %7D%0A            %7D%0A        %7D;%0A%0A        new Thread(runnable).start();
 %0A   
@@ -2142,34 +2142,25 @@
         if (
-mResponse.
+!
 isAlive()) %7B
@@ -2176,173 +2176,86 @@
     
-mJSONResponse = response;%0A            start();%0A        %7D else %7B%0A            Console.d(getCallName(), %22Call canceled after response was received%22);%0A  
+return;%0A        %7D%0A%0A        final Runnable runnable = new Runnable() %7B%0A
       
-%7D%0A
   
-  %7D%0A%0A%0A
     
@@ -2264,36 +2264,38 @@
 verride%0A    
+        
 public
- final
  void run() 
@@ -2308,22 +2308,8 @@
     
-super.run();%0A%0A
     
@@ -2316,26 +2316,16 @@
     if (
-mResponse.
 isAlive(
@@ -2345,149 +2345,26 @@
     
-if (mException != null) %7B%0A                mResponse.error(mException);%0A            %7D else if (mJSONResponse != null)
+        try
  %7B%0A    
-            try %7B%0A
     
@@ -2398,14 +2398,9 @@
 nse(
-mJSONR
+r
 espo
@@ -2401,24 +2401,28 @@
 (response);%0A
+    
             
@@ -2471,32 +2471,36 @@
                 
+    
 throw new Runtim
@@ -2522,38 +2522,32 @@
             
-    %7D%0A
         
     %7D else %7B
@@ -2530,36 +2530,25 @@
             
-    %7D else %7B
+%7D
 %0A           
@@ -2556,221 +2556,73 @@
     
-throw new IllegalStateException(getCallName() + %22 Thread started but no response data available%22);%0A            %7D%0A        %7D else %7B%0A            Console.d(getCallName(), %22Call canceled after Thread began%22);%0A        %7D
+%7D%0A            %7D%0A        %7D;%0A%0A        new Thread(runnable).start();
 %0A   

call responses are now always in a separate thread