package com.kuckovsky.richard.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;


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

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

public class Loading extends AppCompatActivity {

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

        SocketIO.getInstance().connect();

        SocketIO.getInstance().mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call (Object...args) {
                Log.i("OK", "Som prihlaseny");

                SocketIO.getInstance().mSocket.emit("list_rooms", "", new Ack() {
                    @Override
                    public void call(Object... args) {
                        JSONObject obj = (JSONObject)args[0];
                        try {
                            JSONArray results = obj.getJSONArray("results");

                            Intent intent = new Intent(Loading.this, TabBarController.class);
                            intent.putExtra("rooms", results.toString());
                            startActivity(intent);

                        } catch (JSONException e) {
                            Log.e("Chyba","Nenacitalo izby");
                            return;
                        }

                        //String answer = (String)args[0];
                        //Log.i("odpoved", answer);

                    }
                });
            }
        });
    }
}
