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;
    private JSONObject mQueue;
    private String mQueueName;

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

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

        this.mQueueName = 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(mQueueName);

        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 {
                    mQueue = new JSONObject(result);
                    QueueActivity.this.onQueueUpdate();
                } catch (JSONException e) {
                    //TODO
                    e.printStackTrace();
                }
            }
        }).execute("method", "queue/" + mQueueName);
        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", mQueueName);
    }

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

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

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

    private void onQueueUpdate() {
        try {
            mAdapter = new QueueAdapter(mQueue.getJSONArray("queue"), this);
            mRecyclerView.setAdapter(mAdapter);
        } catch (JSONException e) {
        }
    }

    @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);
    }
}

@@ -395,16 +395,41 @@
 ception;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -2603,56 +2603,1004 @@
     
-%7D%0A%0A    private void removeUser(Object... args) %7B
+    try %7B%0A            mQueue.getJSONArray(%22queue%22).put(args%5B0%5D);%0A            runOnUiThread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    onQueueUpdate();%0A                %7D%0A            %7D);%0A        %7D catch (JSONException e) %7B%0A        %7D%0A    %7D%0A%0A    private void removeUser(Object... args) %7B%0A        try %7B%0A            String id = ((JSONObject) args%5B0%5D).getString(%22ugKthid%22);%0A            JSONArray queuees = mQueue.getJSONArray(%22queue%22);%0A            for (int i = 0; i %3C queuees.length(); i++) %7B%0A                if (queuees.getJSONObject(i).getString(%22ugKthid%22).equals(id)) %7B%0A                    queuees.remove(i);%0A                    break;%0A                %7D%0A            %7D%0A            runOnUiThread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    onQueueUpdate();%0A                %7D%0A            %7D);%0A            System.out.println(Arrays.toString(args));%0A        %7D catch (JSONException e) %7B%0A        %7D
 %0A   

Add callbacks for users leaving and joining queue