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;
	
	private final Gson gson = new Gson();

	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) {
		
		String json = gson.toJson(poll);
		
		HttpClient httpclient = getHttpClient();
		HttpPost post = new HttpPost(baseUrl + "/polls");
		try {
			post.setEntity(new StringEntity(json));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			HttpResponse response = httpclient.execute(post);
		    if(response.getStatusLine().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;	
		
	}
	
	private HttpClient getHttpClient()
	{
		HttpClient httpclient = new DefaultHttpClient();
		HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), httpConnectionTimeout);
		HttpConnectionParams.setSoTimeout(httpclient.getParams(), httpConnectionTimeout);
		return httpclient;
	}
	
	
}

@@ -1341,90 +1341,20 @@
 %0D%0A%09%09
-S
 tr
-ing json = gson.toJson(poll);
+y 
 %0D%0A%09%09
+%7B
 %0D%0A%09
-%09HttpClient httpclient = getHttpClient();%0D%0A
 %09%09Ht
@@ -1359,36 +1359,35 @@
 HttpPost post = 
-new 
+get
 HttpPost(baseUrl
@@ -1383,166 +1383,36 @@
 ost(
-baseUrl + %22/polls%22);%0D%0A%09%09try %7B%0D%0A%09%09%09post.setEntity(new StringEntity(json));%0D%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09try %7B
+%22polls%22, gson.toJson(poll));
 %0D%0A%09%09
@@ -1432,34 +1432,39 @@
  response = 
-httpc
+getHttpC
 lient
+()
 .execute(pos
@@ -1618,26 +1618,14 @@
 %09%09%09%09
-ApiResult result =
+return
  gso
@@ -1667,28 +1667,8 @@
 ass)
-;%0D%0A%09%09%09%09return result
 .Pay
@@ -1672,20 +1672,16 @@
 Payload;
-%09%09%09%09
 %0D%0A%09%09    
@@ -1743,30 +1743,317 @@
 race();%0D%0A%09%09%7D
+%09%09
 %0D%0A%09%09
-%0D%0A
+return null;%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09private HttpPost getHttpPost(String route, String entity)%0D%0A%09%7B%0D%0A%09%09HttpPost post = new HttpPost(baseUrl + %22/%22 + route);%0D%0A%09%09try %7B%0D%0A%09%09%09post.setEntity(new StringEntity(entity));%0D%0A%09%09%09return post;%0D%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09
 %09%09return nul
@@ -2054,21 +2054,21 @@
 rn null;
-%09
 %0D%0A%09%09
+%7D
 %0D%0A%09%7D%0D%0A%09%0D

make more compact