/**
 * 
 */
package com.beem.project.beem.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.beem.project.beem.BeemService;
import com.beem.project.beem.R;
import com.beem.project.beem.service.aidl.IXmppFacade;

/**
 * @author nikita
 */
public class AddContact extends Activity {

    protected static final String TAG = "AddContact";
    private String mLogin;
    private String mAlias;
    private final List<String> mGroup = new ArrayList<String>();
    private IXmppFacade xmppFacade;
    private final ServiceConnection mServConn = new BeemServiceConnection();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.addcontact);
	Button ok = (Button) findViewById(R.id.addc_ok);
	ok.setOnClickListener(mOkListener);
	bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
	super.onStop();
	unbindService(mServConn);
    }

    private class BeemServiceConnection implements ServiceConnection {

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
	    xmppFacade = IXmppFacade.Stub.asInterface(service);
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
	    xmppFacade = null;
	}
    }

    private String getWidgetText(int id) {
	EditText widget = (EditText) this.findViewById(id);
	return widget.getText().toString();
    }

    private final OnClickListener mOkListener = new OnClickListener() {

	@Override
	public void onClick(View v) {
	    boolean valid = true;
	    mLogin = getWidgetText(R.id.addc_login);
	    boolean isEmail = Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", mLogin);
	    if (!isEmail) {
		Toast.makeText(AddContact.this, getString(R.string.AddCContactAddedLoginError), Toast.LENGTH_SHORT)
		    .show();
		return;
	    }
	    mAlias = getWidgetText(R.id.addc_alias);
	    if (mLogin.length() == 0)
		valid = false;
	    if (getWidgetText(R.id.addc_group).length() != 0)
		mGroup.add(getWidgetText(R.id.addc_group));
	    if (valid) {
		try {
		    if (xmppFacade.getRoster().addContact(mLogin, mAlias, mGroup.toArray(new String[mGroup.size()])) == null) {
			Toast.makeText(AddContact.this, getString(R.string.AddCContactAddedError), Toast.LENGTH_SHORT)
			    .show();
			return;
		    } else {
			Toast.makeText(AddContact.this, getString(R.string.AddCContactAdded), Toast.LENGTH_SHORT)
			    .show();
		    }
		    finish();
		} catch (RemoteException e) {
		    Toast.makeText(AddContact.this, e.getMessage(), Toast.LENGTH_SHORT).show();
		    e.printStackTrace();
		}
		setResult(RESULT_OK);
	    } else {
		Toast.makeText(AddContact.this, getString(R.string.AddCBadForm), Toast.LENGTH_SHORT).show();
		setResult(RESULT_CANCELED);
	    }

	}
    };
}
