package org.tndata.android.compass.task;

import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.tndata.android.compass.model.User;
import org.tndata.android.compass.util.Constants;
import org.tndata.android.compass.util.NetworkHelper;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;


/**
 * Sets the time zone to the current timezone in the background.
 *
 * @author Ismael Alonso
 * @version 1.0.0
 */
public class UpdateProfileTask extends AsyncTask<User, Void, Boolean>{
    private OnProfileUpdateCallback mCallback;


    /**
     * Constructor.
     *
     * @param callback the callback implementing object.
     */
    public UpdateProfileTask(@Nullable OnProfileUpdateCallback callback){
        mCallback = callback;
    }

    @Override
    protected Boolean doInBackground(User... params){
        //If no parameters were passed
        if (params.length == 0){
            //No profile is updated, therefore failure.
            return false;
        }

        User user = params[0];
        //If the user has no profile to be updated
        if (user.getUserprofileId() == -1){
            //Failure
            return false;
        }

        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json");
        headers.put("Content-type", "application/json");
        headers.put("Authorization", "Token " + user.getToken());

        String url = Constants.BASE_URL + "userprofiles/" + user.getUserprofileId() + "/";
        JSONObject body = new JSONObject();
        try{
            body.put("timezone", TimeZone.getDefault().getID());
        }
        catch (JSONException jx){
            jx.printStackTrace();
            return false;
        }

        InputStream stream = NetworkHelper.httpPutStream(url, headers, body.toString());
        if (stream == null){
            return false;
        }

        //Don't need the result at the moment, but may in the future
        /*String result = "";
        try{
            BufferedReader bReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

            String line;
            while ((line = bReader.readLine()) != null){
                result += line;
            }
            bReader.close();

            Log.d("PROFILE UPDATE", result);
        }
        catch (Exception x){
            x.printStackTrace();
            return false;
        }*/

        return true;
    }

    @Override
    protected void onPostExecute(Boolean success){
        Log.d("Profile Update", "success: " + success);
        if (mCallback != null){
            mCallback.onProfileUpdated(success);
        }
    }


    /**
     * Callback interface for profile update events.
     *
     * @author Ismael Alonso
     * @version 1.0.0
     */
    public interface OnProfileUpdateCallback{
        /**
         * Called when the update process is complete.
         *
         * @param success true if the update succeeded, false otherwise.
         */
        void onProfileUpdated(boolean success);
    }
}

@@ -344,16 +344,44 @@
 elper;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -2011,17 +2011,17 @@
 (stream 
-=
+!
 = null)%7B
@@ -2037,390 +2037,35 @@
     
-return false;%0A        %7D%0A%0A        //Don't need the result at the moment, but may in the future%0A        /*String result = %22%22;%0A        try%7B%0A            BufferedReader bReader = new BufferedReader(new InputStreamReader(stream, %22UTF-8%22));%0A%0A            String line;%0A            while ((line = bReader.readLine()) != null)%7B%0A                result += line;%0A            %7D%0A            bReader
+try%7B%0A                stream
 .clo
@@ -2070,17 +2070,16 @@
 lose();%0A
-%0A
         
@@ -2086,51 +2086,14 @@
     
-Log.d(%22PROFILE UPDATE%22, result);
+%7D
 %0A    
-    %7D%0A
     
@@ -2103,16 +2103,18 @@
  catch (
+IO
 Exceptio
@@ -2115,16 +2115,18 @@
 ception 
+io
 x)%7B%0A    
@@ -2133,16 +2133,22 @@
         
+    
+io
 x.printS
@@ -2152,32 +2152,46 @@
 ntStackTrace();%0A
+            %7D%0A
             retu
@@ -2185,36 +2185,35 @@
          return 
-fals
+tru
 e;%0A        %7D*/%0A%0A
@@ -2208,19 +2208,16 @@
        %7D
-*/%0A
 %0A       
@@ -2220,27 +2220,28 @@
      return 
-tru
+fals
 e;%0A    %7D%0A%0A  

Remove comment block and close the stream