package ins.developers.indoornavigationsystem;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {
    final String TAG = this.getClass().getSimpleName();
    TextView responseText;
    Button getRequestButton;
    EditText employeeSelector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        responseText = (TextView) findViewById(R.id.result);
        getRequestButton = (Button) findViewById(R.id.get_button);
        employeeSelector = (EditText) findViewById(R.id.node_setter);

        getRequestButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Instantiate the RequestQueue.
                RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
                String selectedEmployee = employeeSelector.getText().toString();
                String employeeId = selectedEmployee.length() != 0 ? selectedEmployee : "1"; // Default nodeId
                String url ="http://10.0.2.2:8050/get_device_pos/" + employeeId; //TO-DO: Change to get_employee_pos

                // Request a string response from the provided URL.
                StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                responseText.setText(response);
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, error.toString());
                        responseText.setText("Error");
                    }
                });
                // Add the request to the RequestQueue.
                queue.add(stringRequest);
            }
        });
    }
}

@@ -446,22 +446,26 @@
 toolbox.
-String
+JsonObject
 Request;
@@ -508,16 +508,76 @@
 olley;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 public c
@@ -1568,13 +1568,18 @@
 ult 
-nodeI
+employee i
 d%0A  
@@ -1778,22 +1778,26 @@
         
-String
+JsonObject
 Request 
@@ -1816,22 +1816,26 @@
 t = new 
-String
+JsonObject
 Request(
@@ -1905,22 +1905,26 @@
 istener%3C
-String
+JSONObject
 %3E() %7B%0A  
@@ -2014,90 +2014,649 @@
 nse(
-String response) %7B%0A                                responseText.setText(response);
+JSONObject response) %7B%0A                                try %7B%0A                                    String xVal = response.getString(%22pos_x%22);%0A                                    String yVal = response.getString(%22pos_y%22);%0A                                    String zVal = response.getString(%22pos_z%22);%0A                                    responseText.setText(%22X:%22 + xVal + %22 Y:%22 + yVal + %22 Z:%22 + zVal);%0A                                %7D catch (JSONException e) %7B%0A                                    responseText.setText(%22Error parsing server response!%22);%0A                                    e.printStackTrace();%0A                                %7D
 %0A   
@@ -2944,16 +2944,37 @@
 t(%22Error
+, employee not found!
 %22);%0A    

Parse the server response as JSON