package net.yasme.android.connection;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.codehaus.jackson.map.ObjectMapper;

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

public class UserTask extends ConnectionTask {

    private static UserTask instance;

    public static UserTask getInstance() {
        if (instance == null) {
            instance = new UserTask();
        }
        return instance;
    }

    private UserTask() {
        try {
            this.uri = new URIBuilder(baseURI).setPath("/usr").build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public Long registerUser(User user) throws RestServiceException {

        try {
            HttpResponse httpResponse = executeRequest(Request.POST, "", user);
            return Long.parseLong(new BufferedReader(new InputStreamReader(httpResponse
                    .getEntity().getContent(), "UTF-8")).readLine());
        } catch (IOException e) {
            throw new RestServiceException(Error.CONNECTION_ERROR);
        }
    }

    public void changeUserData(User user) throws RestServiceException {
        executeRequest(Request.PUT, "", user);
        System.out.println("[DEBUG] User data changed");
    }

    public User getUserData() throws RestServiceException {

        try {
            HttpResponse httpResponse = executeRequest(Request.GET, "");
            return new ObjectMapper().readValue(((new BufferedReader(new InputStreamReader(
                    httpResponse.getEntity().getContent(), "UTF-8"))).readLine()), User.class);
        } catch (IOException e) {
            throw new RestServiceException(Error.CONNECTION_ERROR);
        }
    }

    //TODO: implement new Methods for getting profile picture
}

@@ -32,16 +32,60 @@
 ction;%0A%0A
+import android.graphics.drawable.Drawable;%0A%0A
 import n
@@ -394,27 +394,92 @@
 java.io.
-IOException
+ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0Aimport
@@ -2094,46 +2094,941 @@
 ent 
-new Methods for getting profile picture
+method%0A    public void uploadProfilePicture(Drawable drawable) throws RestServiceException %7B%0A%0A        executeRequest(Request.POST, %22profile%22);%0A%0A    %7D%0A%0A    public Drawable getOwnProfilePicture() throws RestServiceException %7B%0A%0A        try %7B%0A            InputStream stream = (executeRequest(Request.GET, %22profile%22)).getEntity().getContent();%0A            return Drawable.createFromStream(stream, null);%0A%0A        %7D catch (IOException e) %7B%0A            throw new RestServiceException(Error.CONNECTION_ERROR);%0A        %7D%0A    %7D%0A%0A    public Drawable getProfilePicture(long userId) throws RestServiceException %7B%0A        try %7B%0A            String path = %22profile/%22 + userId;%0A            InputStream stream = (executeRequest(Request.GET, path)).getEntity().getContent();%0A            return Drawable.createFromStream(stream, null);%0A%0A        %7D catch (IOException e) %7B%0A            throw new RestServiceException(Error.CONNECTION_ERROR);%0A        %7D%0A    %7D
 %0A%7D%0A

implement getProfilePic methods