package com.mobile.countme.implementation.controllers;

import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * Created by Torgeir on 15.10.2015.
 */

public class HttpSenderThread extends Thread {
    private JSONObject obj;
    private String url;
    private LoginInfo info;
    private HttpPostKind postType;

    HttpSenderThread(JSONObject obj, String url, LoginInfo info, HttpPostKind postType){
        this.obj = obj;
        this.url = url;
        this.info = info;
        this.postType = postType;

    }


    public void run(){
        sendJSON();
    }

    //sendJSON is a delegation method that sends a jsonObject to an url
    private void sendJSON(){
        Log.d("SendJSON", "SendJSON started");
        HttpPost post = new HttpPost( url );
        StringEntity string;
        HttpResponse response;

        try {
            string = new StringEntity(obj.toString());
            string.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(string);
            response = new DefaultHttpClient().execute(post);
            Log.d("SendJSON", "JSON sent");
            String json_string;
            JSONObject receivedObject;
            switch(postType){

                case TRIP:
                    break;
                case ERROR:
                    String json_string1 = EntityUtils.toString(response.getEntity());
                    Log.d("Received errorresponse", json_string1);
                    synchronized (obj) {
                        obj.notifyAll();
                    }
                    break;
                case LOGIN:
                    json_string = EntityUtils.toString(response.getEntity());
                    receivedObject = new JSONObject(json_string);
                    synchronized (info) {
                        info.setUserID(receivedObject.getString("_id"));
                        info.setToken(receivedObject.getString("token"));
                        info.notifyAll();
                    }
                    Log.d("Received loginresponse", receivedObject.toString());
                    break;
                case CREATEUSER:
                    json_string = EntityUtils.toString(response.getEntity());
                    receivedObject = new JSONObject(json_string);
                    synchronized (info) {
                        info.setUsername(receivedObject.getString("username"));
                        info.notifyAll();
                    }
                    Log.d("Create user response", receivedObject.toString());

            }
        }
        catch (Exception e){
            e.printStackTrace();

        }
        //If response is needed somewhere, figure out how to communicate with main thread.

    }
}

@@ -112,16 +112,71 @@
 sponse;%0A
+import org.apache.http.client.ClientProtocolException;%0A
 import o
@@ -456,22 +456,127 @@
 son.JSON
-Object
+Exception;%0Aimport org.json.JSONObject;%0A%0Aimport java.io.IOException;%0Aimport java.io.UnsupportedEncodingException
 ;%0A%0A/**%0A 
@@ -3072,16 +3072,20 @@
  catch (
+JSON
 Exceptio
@@ -3088,16 +3088,17 @@
 ption e)
+ 
 %7B%0A      
@@ -3103,16 +3103,491 @@
         
+Log.d(%22Exception%22, %22JSONException%22);%0A            e.printStackTrace();%0A        %7D%0A        catch (ClientProtocolException e) %7B%0A%0A            Log.d(%22Exception%22, %22ClientProtoclException%22);%0A            e.printStackTrace();%0A        %7D%0A        catch (UnsupportedEncodingException e) %7B%0A%0A            Log.d(%22Exception%22, %22UnsupportedEncodingException%22);%0A            e.printStackTrace();%0A        %7D%0A        catch (IOException e) %7B%0A%0A            Log.d(%22Exception%22, %22IOException%22);%0A            
 e.printS
@@ -3598,17 +3598,16 @@
 Trace();
-%0A
 %0A       

add exception identification via logging