package rs.elfak.mosis.marko.discounthunt.api.endpoints;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;

import org.json.JSONException;
import org.json.JSONObject;
import rs.elfak.mosis.marko.discounthunt.DiscountHunt;

public class BaseEndpoint {

    public static final String API_URL = "https://discount-hunt-markomedia.c9users.io";

    private String url;
    private String resourceName;
    public BaseEndpoint(String path, String resourceName) {
        this.resourceName = resourceName;
        this.url = API_URL + path;
    }

    public void get(int id, Response.Listener successListener,
                    Response.ErrorListener errorListener) {
        execute(url + "/" + id, Request.Method.GET, null, successListener, errorListener);
    }

    public void post(JSONObject jsonObject, Response.Listener successListener,
                    Response.ErrorListener errorListener) {
        JSONObject requestBody = createJsonRequest(jsonObject);
        execute(url, Request.Method.POST, requestBody, successListener, errorListener);
    }

    public void put(int id, JSONObject jsonObject, Response.Listener successListener,
                    Response.ErrorListener errorListener) {
        JSONObject requestBody = createJsonRequest(jsonObject);
        execute(url + "/" + id, Request.Method.PUT, requestBody, successListener, errorListener);
    }

    public void delete(int id, JSONObject requestBody, Response.Listener successListener,
                    Response.ErrorListener errorListener) {
        execute(url + "/" + id,Request.Method.DELETE, requestBody, successListener, errorListener);
    }

    private void execute(String url, int type, JSONObject requestBody, Response.Listener successListener,
                         Response.ErrorListener errorListener) {
        try {
            if(DiscountHunt.currentSession != null){
                requestBody.put("token", DiscountHunt.currentSession.getString("token"));
            }
            JsonObjectRequest request = new JsonObjectRequest(type,
                    url + ".json", requestBody, successListener, errorListener);
            DiscountHunt.requestQueue.add(request);
        }catch (JSONException ex) {

        }
    }

    private JSONObject createJsonRequest(JSONObject jsonObject) {
        try {
            JSONObject requestJsonObject = new JSONObject();
            requestJsonObject.put(resourceName, jsonObject);
            return requestJsonObject;
        }catch (JSONException ex) { }
        return null;
    }
}

@@ -762,36 +762,16 @@
 istener,
-%0A                   
  Respons
@@ -825,32 +825,42 @@
 e(url + %22/%22 + id
+ + %22.json%22
 , Request.Method
@@ -1081,36 +1081,40 @@
 estBody = create
-Json
+Resource
 Request(jsonObje
@@ -1137,16 +1137,26 @@
 cute(url
+ + %22.json%22
 , Reques
@@ -1404,28 +1404,32 @@
 ody = create
-Json
+Resource
 Request(json
@@ -1467,16 +1467,26 @@
 %22/%22 + id
+ + %22.json%22
 , Reques
@@ -1586,32 +1586,8 @@
  id,
- JSONObject requestBody,
  Res
@@ -1617,37 +1617,16 @@
 istener,
-%0A                    
 Response
@@ -1688,16 +1688,17 @@
 /%22 + id,
+ 
 Request.
@@ -1712,27 +1712,20 @@
 DELETE, 
-requestBody
+null
 , succes
@@ -1828,16 +1828,41 @@
 estBody,
+%0A                        
  Respons
@@ -1892,34 +1892,8 @@
 ner,
-%0A                         
 Resp
@@ -1940,170 +1940,475 @@
     
+S
 tr
-y %7B%0A            if(DiscountHunt.currentSession != null)%7B%0A                requestBody.put(%22token%22, DiscountHunt.currentSession.getString(%22token%22));%0A            %7D
+ing finalUrl = url;%0A        boolean hasSession = DiscountHunt.currentSession != null;%0A        try %7B%0A            // Put auth token into the request%0A            if(requestBody != null && hasSession)%7B%0A                requestBody.put(%22token%22, DiscountHunt.currentSession.getString(%22token%22));%0A            %7Delse if(hasSession)%7B%0A                finalUrl = finalUrl + %22?token=%22 + DiscountHunt.currentSession.getString(%22token%22);%0A            %7D%0A%0A            // Create request
 %0A   
@@ -2475,56 +2475,51 @@
 ype,
-%0A                    url + %22.json%22, requestBody,
+ finalUrl, requestBody,%0A                   
  suc
@@ -2544,24 +2544,60 @@
 rListener);%0A
+            // Add request to queue%0A
             
@@ -2671,26 +2671,16 @@
 on ex) %7B
-%0A%0A        
 %7D%0A    %7D%0A
@@ -2709,20 +2709,24 @@
 t create
-Json
+Resource
 Request(

Fix BaseEndpoint to properly send auth token