package se.kth.csc.stayawhile;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class QueueActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private Socket mSocket;

    {
        try {
            mSocket = IO.socket("http://queue.csc.kth.se/");
        } catch (URISyntaxException e) {
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String queue = getIntent().getStringExtra("queue");
        setContentView(R.layout.activity_queue);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(queue);

        mRecyclerView = (RecyclerView) findViewById(R.id.queue_people);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        new APITask(new APICallback() {
            @Override
            public void r(String result) {
                try {
                    JSONObject queuePeople = new JSONObject(result);
                    QueueActivity.this.onQueueUpdate(queuePeople);
                } catch (JSONException e) {
                    //TODO
                    e.printStackTrace();
                }
            }
        }).execute("method", "queue/" + queue);
        mSocket.on("join", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                newUser(args);
            }
        });
        mSocket.on("leave", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                removeUser(args);
            }
        });
        mSocket.connect();
        mSocket.emit("listen", queue);
    }

    private void newUser(Object... args) {
    }

    private void removeUser(Object... args) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSocket.disconnect();
    }

    private void onQueueUpdate(JSONObject queuePeople) throws JSONException {
        mAdapter = new QueueAdapter(queuePeople.getJSONArray("queue"), this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -714,16 +714,78 @@
 mSocket;
+%0A    private JSONObject mQueue;%0A    private String mQueueName;
 %0A%0A    %7B%0A
@@ -1045,19 +1045,22 @@
     
-String queu
+this.mQueueNam
 e = 
@@ -1412,21 +1412,26 @@
 etTitle(
-q
+mQ
 ueue
+Name
 );%0A%0A    
@@ -1803,37 +1803,21 @@
         
-JSONObject queuePeopl
+mQueu
 e = new 
@@ -1889,27 +1889,16 @@
 eUpdate(
-queuePeople
 );%0A     
@@ -2076,21 +2076,26 @@
 eue/%22 + 
-q
+mQ
 ueue
+Name
 );%0A     
@@ -2507,21 +2507,26 @@
 isten%22, 
-q
+mQ
 ueue
+Name
 );%0A    %7D
@@ -2776,55 +2776,30 @@
 ate(
-JSONObject queuePeople) throws JSONException %7B%0A
+) %7B%0A        try %7B%0A    
     
@@ -2834,19 +2834,14 @@
 ter(
-q
+mQ
 ueue
-People
 .get
@@ -2867,32 +2867,36 @@
  this);%0A        
+ 
+   
 mRecyclerView.se
@@ -2911,24 +2911,70 @@
 (mAdapter);%0A
+        %7D catch (JSONException e) %7B%0A        %7D%0A
     %7D%0A%0A    @

Move queue json object to field