package org.ieeeguc.ieeeguc.models;

import android.util.Log;

import org.ieeeguc.ieeeguc.HTTPResponse;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class User{


    private String type;
    private String email;
    private String password;
    private String first_name;
    private String last_name;
    private String birthdate;
    private String gender;
    private String IEEE_membership_ID;


    /**
     *
     * @param token The access token which's provided when a user logs in.
     * @param oldPassword the current user's account password.
     * @param newPassword the new password that will override the old one.
     * @param IEEE_membership_ID
     * @param HTTPResponse Used to control the flow of the app after the editProfile is performed.
     */

    public void editProfile(String token,
                            String oldPassword,
                            String newPassword,
                            String IEEE_membership_ID,
                            HTTPResponse HTTPResponse){

        if(oldPassword == password && newPassword.length() >= 6){

            // The user entered the right old passwrod and a valid new password,
            // then we the app will perform with sending the editing request to the server.

            OkHttpClient client = new OkHttpClient();
            HashMap<String , String > body = new HashMap<>();
            body.put("type",type);
            body.put("email",email);
            body.put("password",newPassword);
            body.put("first_name",first_name);
            body.put("last_name",last_name);
            body.put("birthdate",birthdate);
            body.put("gender",gender);
            body.put("IEEE_membership_ID",IEEE_membership_ID);


            Request request = new Request.Builder().put(RequestBody.create(MediaType.parse("application/json"),
                    new JSONObject(body).toString()))
                    .addHeader("Authorization",token)
                    .addHeader("user_agent","Android")
                    .url("http://ieeeguc.org/api/user").build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {



                }
            });

        }else if(newPassword.length() < 6){

            //TODO:Should be replaced with a toast or a small textView indicating the log message.
            Log.v("org.ieeeguc.ieeeguc","password should contain at least 6 characters");

        }else{

            //TODO:Should be replaced with a toast or a small textView indicating the log message.
            Log.v("org.ieeeguc.ieeeguc","Old password incorrect !");
        }





    }
}

@@ -98,24 +98,56 @@
 PResponse;%0D%0A
+import org.json.JSONException;%0D%0A
 import org.j
@@ -434,276 +434,105 @@
 r%7B%0D%0A
-%0D%0A%0D%0A    private String type;%0D%0A    private String email;%0D%0A    private String password;%0D%0A    private String first_name;%0D%0A    private String last_name;%0D%0A    private String birthdate;%0D%0A    private String gender;%0D%0A    private String IEEE_membership_ID;%0D%0A%0D%0A%0D%0A    /**%0D%0A     *
+    /**%0D%0A     * This method is called when the user performs an editing operation on his profile.
 %0D%0A  
@@ -793,20 +793,20 @@
  @param 
-HTTP
+http
 Response
@@ -1115,16 +1115,22 @@
         
+final 
 HTTPResp
@@ -1134,20 +1134,20 @@
 esponse 
-HTTP
+http
 Response
@@ -1272,18 +1272,18 @@
 ld passw
-r
 o
+r
 d and a 
@@ -2394,48 +2394,8 @@
 %0D%0A%0D%0A
-                    e.printStackTrace();
 %0D%0A  
@@ -2529,28 +2529,28 @@
 eption %7B%0D%0A%0D%0A
-%0D%0A%0D%0A
+    
             
@@ -2557,475 +2557,1256 @@
     
-%7D%0D%0A            %7D);%0D%0A%0D%0A        %7Delse if(newPassword.length() %3C 6)%7B%0D%0A%0D%0A            //TODO:Should be replaced with a toast or a small textView indicating the log message.%0D%0A            Log.v(%22org.ieeeguc.ieeeguc%22,%22password should contain at least 6 characters%22);%0D%0A%0D%0A        %7Delse%7B%0D%0A%0D%0A            //TODO:Should be replaced with a toast or a small textView indicating the log message.%0D%0A            Log.v(%22org.ieeeguc.ieeeguc%22,%22Old password incorrect !%22);%0D%0A        %7D%0D%0A%0D%0A%0D%0A%0D%0A
+//Getting the status code.%0D%0A                    int statusCode = response.code();%0D%0A                    String code = String.valueOf(statusCode);%0D%0A%0D%0A                    if(code.charAt(0) == 2)%7B%0D%0A%0D%0A                        // The received code is of the format 2xx, and the call was successful.%0D%0A%0D%0A                        try %7B%0D%0A                            JSONObject responseBody = new JSONObject(response.body().toString());%0D%0A                            httpResponse.onSuccess(statusCode,responseBody);%0D%0A                        %7D catch (JSONException e) %7B%0D%0A                            e.printStackTrace();%0D%0A                        %7D%0D%0A%0D%0A                    %7D%0D%0A%0D%0A                    else%7B%0D%0A%0D%0A                        // The received code is of the format 3xx or 4xx or 5xx,%0D%0A                        // and the call wasn't successful.%0D%0A%0D%0A                        try%7B%0D%0A                            JSONObject responseBody = new JSONObject(response.body().toString());%0D%0A                            httpResponse.onFailure(statusCode,responseBody);%0D%0A                        %7Dcatch (JSONException e)%7B%0D%0A%0D%0A                            e.printStackTrace();%0D%0A                        %7D%0D%0A%0D%0A                    %7D%0D%0A%0D%0A%0D%0A%0D%0A                %7D%0D%0A            %7D);%0D%0A%0D%0A        %7D
 %0D%0A%0D%0A

Version 2 : editProfile instance method ready is ready for testing.