package pt.isep.mei.simov.droneappcontroller;

import android.util.Log;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;

/**
 * Created by almeida on 23-12-2014.
 */
public class Connection {
    String HOST = "aalmeid.ddns.net";
    int PORT = 5222;
    String SERVICE = "xmppserver";
    String USERNAME = "";
    String PASSWORD = "";

    static XMPPConnection connection;
    final ArrayList <String> usersList = new ArrayList<>();

    public Connection(String user, String password) {
        this.USERNAME = user;
        this.PASSWORD = password;
        setConnection();
    }

    private void setConnection() {

        final CountDownLatch latch = new CountDownLatch(1);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // Create a connection
                ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
                connection = new XMPPConnection(connConfig);
                try {
                    connection.connect();
                    Log.i("XMPP", "[SettingsDialog] Connected to " + connection.getHost());
                } catch (XMPPException ex) {
                    Log.e("XMPP", "[SettingsDialog] Failed to connect to " + connection.getHost());
                    Log.e("XMPP", ex.toString());
                }
                try {
                    connection.login(USERNAME, PASSWORD);
                    Log.i("XMPP", "Logged in as " + connection.getUser());

                    // Set the status to available
                    Presence presence = new Presence(Presence.Type.available);
                    connection.sendPacket(presence);

                    Roster roster = connection.getRoster();
                    Collection<RosterEntry> entries = roster.getEntries();

                    for (final RosterEntry entry : entries) {

                        Log.d("XMPP", "--------------------------------------");
                        Log.d("XMPP", "RosterEntry " + entry);
                        Log.d("XMPP", "User: " + entry.getUser());

                        Log.d("XMPP", "Name: " + entry.getName());
                        Log.d("XMPP", "Status: " + entry.getStatus());
                        Log.d("XMPP", "Type: " + entry.getType());
                        Presence entryPresence = roster.getPresence(entry.getUser());
                        Log.d("XMPP", "Presence Status: " + entryPresence.getStatus());
                        Log.d("XMPP", "Presence Type: " + entryPresence.getType());

                        Presence.Type type = entryPresence.getType();
                        if (type == Presence.Type.available)
                            Log.d("XMPP", "Presence AVIALABLE");
                        Log.d("XMPP", "Presence : " + entryPresence);

                        usersList.add(entry.getUser());
                    }

                    latch.countDown();

                } catch (XMPPException ex) {
                    Log.e("XMPP", "Failed to log in as " + USERNAME);
                    Log.e("XMPP", ex.toString());
                }
            }
        });
        t.start();

        try{
            latch.await();
        }catch(InterruptedException e){
            Log.e("Error","got interrupted!",e);
        }
    }

    public boolean createAccount (String username, String password){
        try {
            AccountManager accountManager = connection.getAccountManager();
            accountManager.createAccount(username, password);
            connection.disconnect();
            return true;
        }catch (Exception e){
            Log.e("Error","problem with create account", e);
            return false;
        }
    }

    public ArrayList getUsers(){
        return usersList;
    }

    public static XMPPConnection getConnection(){
        return connection;
    }
}