package co.cutely.asim.service;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.nick.packet.Nick;

import java.io.IOException;

/**
 * Service handling all XMPP connection logic
 */
public class XmppService extends Service {
	private static final String TAG = XmppService.class.getSimpleName();

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public int onStartCommand(final Intent intent, final int flags, final int startId) {
		Log.i(TAG, "Started with start id " + startId + ": " + intent);
		connect(new XmppServiceConnectionConfiguration("0xxon.net", "smacktest", "thisIsThePasswordForSmacktest"));

		// continue running until stopped
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	private void connect(final XmppServiceConnectionConfiguration conf) {
		new AsyncTask<XmppServiceConnectionConfiguration, Void, AbstractXMPPConnection>() {
			@Override
			protected AbstractXMPPConnection doInBackground(final XmppServiceConnectionConfiguration... configs) {
				Log.i(TAG, "In doInBackground");

				if ( configs.length != 1 ) {
					Log.e(TAG, "Wrong number of connection configurations in connect");
					assert(false);
				}

				XmppServiceConnectionConfiguration config = configs[0];

				Log.i(TAG, "trying to connect to " + config.service);
                AbstractXMPPConnection conn = new XMPPTCPConnection(config.service);

                try {
                    conn.connect();
                    Log.i(TAG, "trying to login...");
                    conn.login(config.user, config.password);
                    return conn;
                } catch (SmackException e) {
                    Log.e(TAG, "Error connecting", e);
                } catch (IOException e) {
                    Log.e(TAG, "Error connecting", e);
                } catch (XMPPException e) {
                    Log.e(TAG, "Error connecting", e);
                }

                return null;
            }


			@Override
			protected void onPostExecute(final AbstractXMPPConnection connection) {
				onConnected(connection);
			}
		}.execute(conf);

	}

	private void onConnected(AbstractXMPPConnection conn) {
		if (conn == null) {
			Log.i(TAG, "Didn't connect =(");
			return;
		}

		Log.i(TAG, "Connected to " + conn.getServiceName() + " with " + conn.getUser() );

		Roster roster = conn.getRoster();

		Log.i(TAG, "Duming roster");
		for ( RosterEntry e : roster.getEntries() ) {
			Log.i(TAG, "User " + e.getName() + "(" + e.getUser() + ")" + e.getGroups());
		}
	}

	public static class XmppServiceConnectionConfiguration {
		public final String service;
		public final String user;
		public final String password;

		public XmppServiceConnectionConfiguration(String service, String user, String password) {
			this.service = service;
			this.user = user;
			this.password = password;
		}
	}
}

@@ -1643,32 +1643,20 @@
 rvice);%0A
-                
+%09%09%09%09
 Abstract
@@ -1721,86 +1721,44 @@
 );%0A%0A
-                try %7B%0A                    conn.connect();%0A                    
+%09%09%09%09try %7B%0A%09%09%09%09%09conn.connect();%0A%09%09%09%09%09
 Log.
@@ -1787,36 +1787,21 @@
 n...%22);%0A
-                    
+%09%09%09%09%09
 conn.log
@@ -1838,57 +1838,30 @@
 d);%0A
-                    return conn;%0A                
+%09%09%09%09%09return conn;%0A%09%09%09%09
 %7D ca
@@ -1885,36 +1885,21 @@
 on e) %7B%0A
-                    
+%09%09%09%09%09
 Log.e(TA
@@ -1925,32 +1925,20 @@
 g%22, e);%0A
-                
+%09%09%09%09
 %7D catch 
@@ -1955,36 +1955,21 @@
 on e) %7B%0A
-                    
+%09%09%09%09%09
 Log.e(TA
@@ -1995,32 +1995,20 @@
 g%22, e);%0A
-                
+%09%09%09%09
 %7D catch 
@@ -2031,28 +2031,13 @@
 ) %7B%0A
-                    
+%09%09%09%09%09
 Log.
@@ -2071,43 +2071,19 @@
 e);%0A
-                %7D%0A%0A                
+%09%09%09%09%7D%0A%0A%09%09%09%09
 retu
@@ -2095,20 +2095,11 @@
 ll;%0A
-            
+%09%09%09
 %7D%0A%0A%0A

fix indentation - back to tabs. No idea how that happened :(