package net.yasme.android.connection;

import net.yasme.android.entities.User;
import net.yasme.android.exception.RestServiceException;
import net.yasme.android.exception.UserError;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class UserTask {

	private String url;

	public UserTask(String url) {
		this.url = url;
	}

	/**
	 * registerUser() get as return value an ID which should be saved on the
	 * client to use it for all user requests
	 * 
	 * @param user
	 * @return userID, which should be stored on the device
	 */
	public String registerUser(User user) throws RestServiceException {
		try {

			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url + "/usr");

			// To Do: Complete UserObject as JSon

			JSONObject obj = new JSONObject();

			// To Do: UserDaten to JSon
			// Edit UserObject from Server

			obj.put("pw", user.getPw());
			obj.put("email", user.getEmail());
			obj.put("name", user.getName());

			String json = obj.toString();

			StringEntity se = new StringEntity(json);

			httpPost.setEntity(se);

			httpPost.setHeader("Content-type", "application/json");
			httpPost.setHeader("Accept", "application/json");

			HttpResponse httpResponse = httpclient.execute(httpPost);

			switch (httpResponse.getStatusLine().getStatusCode()) {
			case 201:
				return (new BufferedReader(new InputStreamReader(httpResponse
						.getEntity().getContent(), "UTF-8"))).readLine();
			case 500:
				throw new RestServiceException(UserError.REGISTRATION_FAILED);
			default:
				new RestServiceException(UserError.ERROR);
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String[] loginUser(User user) throws RestServiceException {

		try {

			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url + "/sign/in");

			// To Do: Complete UserObject as JSon

			JSONObject obj = new JSONObject();

			// To Do: UserDaten to JSon
			// Edit UserObject from Server

			obj.put("email", user.getEmail());
			obj.put("pw", user.getPw());

			String json = obj.toString();

			StringEntity se = new StringEntity(json);

			httpPost.setEntity(se);

			httpPost.setHeader("Content-type", "application/json");
			httpPost.setHeader("Accept", "application/json");

			HttpResponse httpResponse = httpclient.execute(httpPost);

			switch (httpResponse.getStatusLine().getStatusCode()) {
			case 200:
				Header[] header = httpResponse.getAllHeaders();
				System.out.println(header.length);
				return null;
			case 401:
				throw new RestServiceException(UserError.LOGIN_FAILED);
			default:
				new RestServiceException(UserError.ERROR);
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public User getUserData(String userID) {

		User user = null;

		try {

			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url + "/usr/" + userID);
			request.addHeader("accept", "application/json");

			HttpResponse response = client.execute(request);

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), "UTF-8"));
			String json = reader.readLine();

			JSONObject jObject = new JSONObject(json);

			user = new User(null, jObject.getString("name"),
					jObject.getString("email"));

			/******** DEBUG ***********/
			System.out.println(user.getEmail() + " " + user.getName());
			/******** DEBUG*END *******/

		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return user;
	}
}

@@ -2586,32 +2586,35 @@
 from Server%0A%0A%09%09%09
+// 
 obj.put(%22email%22,
@@ -2627,32 +2627,35 @@
 getEmail());%0A%09%09%09
+// 
 obj.put(%22pw%22, us
@@ -2664,24 +2664,110 @@
 .getPw());%0A%0A
+%09%09%09// Debug: Testdaten%0A%09%09%09obj.put(%22email%22, %22flo@yasme.net%22);%0A%09%09%09obj.put(%22pw%22, %22pw%22);%0A%0A
 %09%09%09String js
@@ -3088,32 +3088,33 @@
 StatusCode()) %7B%0A
+%0A
 %09%09%09case 200:%0A%09%09%09
@@ -3124,17 +3124,15 @@
 ader
-%5B%5D header
+ userID
  = h
@@ -3150,61 +3150,97 @@
 .get
-All
+First
 Header
-s();%0A%09%09%09%09System.out.println(header.length
+(%22userId%22);%0A%09%09%09%09Header token = httpResponse.getFirstHeader(%22Authorization%22
 );%0A
+%0A
 %09%09%09%09
@@ -3247,21 +3247,70 @@
 return n
-ull
+ew String%5B%5D %7B userID.getValue(), token.getValue() %7D
 ;%0A
+%0A
 %09%09%09case 
@@ -3366,32 +3366,33 @@
 .LOGIN_FAILED);%0A
+%0A
 %09%09%09default:%0A%09%09%09%09
@@ -3379,36 +3379,42 @@
 );%0A%0A%09%09%09default:%0A
-
 %09%09%09%09
+throw 
 new RestServiceE

edit loginUser()