package net.maxhorstmann.askthecrowds.services;

import java.io.UnsupportedEncodingException;

import net.maxhorstmann.askthecrowds.models.ApiResult;
import net.maxhorstmann.askthecrowds.models.Poll;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

public class BackendService {
	
	private final String baseUrl = "http://askthecrowds.cloudapp.net/api";
	private final int httpConnectionTimeout = 5000;

	public String createUser()	{
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost post = new HttpPost(baseUrl + "/users");
		HttpResponse response;
		
		try {
			response = httpclient.execute(post);
		    StatusLine statusLine = response.getStatusLine();
		    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
		    	return "created";
		    }
		} 
		catch (Exception ex)
		{
		}
		
		return "1234-5678-1234-5678";
		
	}
	
	public String postPoll(Poll poll) {
		
		Gson gson = new Gson();
		String json = gson.toJson(poll);
		
		HttpClient httpclient = new DefaultHttpClient();
		int timeout = 5;
		HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), httpConnectionTimeout);
		HttpConnectionParams.setSoTimeout(httpclient.getParams(), httpConnectionTimeout);
		
		HttpPost post = new HttpPost(baseUrl + "/polls");
		try {
			post.setEntity(new StringEntity(json));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpResponse response;
		
		try {
			response = httpclient.execute(post);
		    StatusLine statusLine = response.getStatusLine();
		    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
				String jsonResult = EntityUtils.toString(response.getEntity());
				ApiResult result = gson.fromJson(jsonResult, ApiResult.class);
				return result.Payload;
				
		    }
		} 
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		
		return null;
		
		
	}
}

@@ -788,16 +788,59 @@
 = 5000;%0D
+%0A%09%0D%0A%09private final Gson gson = new Gson();%0D
 %0A%0D%0A%09publ
@@ -1339,35 +1339,8 @@
 %09%09%0D%0A
-%09%09Gson gson = new Gson();%0D%0A
 %09%09St
@@ -1405,234 +1405,24 @@
 t = 
-new DefaultHttpClient();%0D%0A%09%09int timeout = 5;%0D%0A%09%09HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), httpConnectionTimeout);%0D%0A%09%09HttpConnectionParams.setSoTimeout(httpclient.getParams(), httpConnectionTimeout);%0D%0A%09%09
+getHttpClient();
 %0D%0A%09%09
@@ -1601,16 +1601,26 @@
 ;%0D%0A%09%09%7D%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09HttpRe
@@ -1630,35 +1630,8 @@
 nse 
-response;%0D%0A%09%09%0D%0A%09%09try %7B%0D%0A%09%09%09
 resp
@@ -1674,32 +1674,11 @@
     
-StatusLine statusLine = 
+if(
 resp
@@ -1697,38 +1697,16 @@
 usLine()
-;%0D%0A%09%09    if(statusLine
 .getStat
@@ -1900,18 +1900,16 @@
 Payload;
-%0D%0A
 %09%09%09%09%0D%0A%09%09
@@ -1999,19 +1999,16 @@
 rn null;
-%0D%0A%09
 %09%0D%0A%09%09%0D%0A%09
@@ -2010,11 +2010,317 @@
 %09%09%0D%0A%09%7D%0D%0A
+%09%0D%0A%09private HttpClient getHttpClient()%0D%0A%09%7B%0D%0A%09%09HttpClient httpclient = new DefaultHttpClient();%0D%0A%09%09HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), httpConnectionTimeout);%0D%0A%09%09HttpConnectionParams.setSoTimeout(httpclient.getParams(), httpConnectionTimeout);%0D%0A%09%09return httpclient;%0D%0A%09%7D%0D%0A%09%0D%0A%09%0D%0A
 %7D%0D%0A

clean up code: