package ch.trvlr.trvlr;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;

import static com.android.volley.Request.Method;

public class ListPublicChatMembersActivity extends BaseListUsersActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Travelers");
        chatId = getIntent().getExtras().getInt("chatId");
        populateListView();

        if (currentUser == null) {
            currentUser = AppController.getInstance().getCurrentUser();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TravelerBO traveler = (TravelerBO) parent.getItemAtPosition(position);
                int uid1 = currentUser.getId();
                int uid2 = traveler.getId();

                AppController.getInstance().addToRequestQueue(new JsonArrayRequest(Method.GET,
                        "http://trvlr.ch:8080/api/private-chats/" + uid1 + "/" + uid2,
                        null,
                        loadPrivateChatSuccess(),
                        loadError()
                ));
            }
        });
    }

    protected void populateListView() {
        AppController.getInstance().addToRequestQueue(new JsonArrayRequest(Method.GET,
                "http://trvlr.ch:8080/api/public-chats/" + chatId + "/travelers",
                null,
                loadTravelersSuccess(),
                loadError()
        ));
    }

    protected Response.Listener<JSONArray> loadTravelersSuccess() {
        return new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    SparseArray<TravelerBO> sparseArray = new SparseArray<TravelerBO>();

                    for (int i = 0; i < response.length(); i++) {
                        TravelerBO traveler = new TravelerBO(
                                response.getJSONObject(i).getInt("id"),
                                response.getJSONObject(i).getString("firstName"),
                                response.getJSONObject(i).getString("lastName"),
                                response.getJSONObject(i).getString("email"),
                                response.getJSONObject(i).getString("uid")
                        );

                        if (traveler.getId() != currentUser.getId()) {
                            // Only add traveler if it's not me.
                            sparseArray.put(traveler.getId(), traveler);
                        }
                    }

                    TravelerAdapter adapter = new TravelerAdapter(sparseArray);
                    mListView.setAdapter(adapter);
                } catch (JSONException e) {
                    Toast.makeText(ListPublicChatMembersActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        };
    }

    protected Response.Listener<JSONArray> loadPrivateChatSuccess() {
        return new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    Intent intent = new Intent(getApplicationContext(), PublicChatActivity.class);
                    // We always create a new BO.
                    // TODO: Actually, we don't.
                    int chatId = response.getJSONObject(0).getInt("id");
                    JSONArray travelers = response.getJSONObject(0).getJSONArray("allTravelers");
                    int uId1 = travelers.getJSONObject(0).getInt("id");
                    int uId2 = travelers.getJSONObject(1).getInt("id");
                    int indexToLoad = uId1 != currentUser.getId() ? 0 : 1;
                    TravelerBO chatPartner = new TravelerBO(
                        travelers.getJSONObject(indexToLoad).getInt("id"),
                        travelers.getJSONObject(indexToLoad).getString("firstName"),
                        travelers.getJSONObject(indexToLoad).getString("lastName"),
                        travelers.getJSONObject(indexToLoad).getString("email"),
                        travelers.getJSONObject(indexToLoad).getString("uid")
                    );

                    ChatBO bo = new ChatBO(chatId, chatPartner.getFullname(), chatPartner);
                    ((AppController) getApplication()).setCurrentActivePrivateChat(bo);
                    startActivity(intent);
                } catch (JSONException e) {
                    Toast.makeText(ListPublicChatMembersActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        // TODO drop this
        // to check current activity in the navigation drawer
        //mNavigationView.getMenu().findItem(R.layout.activity_base_list_users).setChecked(true);
    }
}

@@ -286,16 +286,69 @@
 Request;
+%0Aimport com.android.volley.toolbox.JsonObjectRequest;
 %0A%0Aimport
@@ -398,16 +398,44 @@
 ception;
+%0Aimport org.json.JSONObject;
 %0A%0Aimport
@@ -1423,37 +1423,38 @@
 stQueue(new Json
-Array
+Object
 Request(Method.G
@@ -3516,29 +3516,30 @@
 istener%3CJSON
-Array
+Object
 %3E loadPrivat
@@ -3588,37 +3588,38 @@
 se.Listener%3CJSON
-Array
+Object
 %3E() %7B%0A          
@@ -3661,37 +3661,38 @@
  onResponse(JSON
-Array
+Object
  response) %7B%0A   
@@ -3950,33 +3950,16 @@
 esponse.
-getJSONObject(0).
 getInt(%22
@@ -4015,33 +4015,16 @@
 esponse.
-getJSONObject(0).
 getJSONA

Fix private chats JSON response parser