/**
 * 
 */
package com.beem.project.beem.jingle.demo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;

import com.beem.project.beem.R;
import com.beem.project.beem.jingle.JingleService;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author darisk
 * 
 */
public class JingleCallActivity extends Activity {

    private XMPPConnection con;
    private ConnectionConfiguration conf;
    private JingleService jingle;

    private Button btconnect;
    private Button btcall;
    private EditText mEdJID;
    private EditText mEdPassword;
    private EditText mEdReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.jingle_call_activity);
	// localhost
	conf = new ConnectionConfiguration("10.0.2.2", 5222);
	
	mEdJID = (EditText) findViewById(R.id.jingledemocalljid);
	mEdPassword = (EditText) findViewById(R.id.jingledemocallpassword);
	mEdReceiver = (EditText) findViewById(R.id.jingledemocallreceiver);
	btconnect = (Button) findViewById(R.id.jingledemocallconnectbutton);
	btconnect.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		String jid = mEdJID.getText().toString();
		String login = StringUtils.parseName(jid);
		con = new XMPPConnection(conf);
		String password = mEdPassword.getText().toString();
		try {
		    con.connect();
	            con.login(login, password);
	            jingle = new JingleService(con);
	            btcall.setEnabled(true);
                } catch (XMPPException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
                }
	    }

	});
	btcall = (Button) findViewById(R.id.jingledemocallbutton);
	btcall.setOnClickListener(new OnClickListener() {
	
	    @Override
	    public void onClick(View arg0) {
		String jid = mEdReceiver.getText().toString();
		if (!"".equals(jid)){
		    jingle.call(jid);
		    Toast.makeText(JingleCallActivity.this, "Appel en cours", Toast.LENGTH_SHORT);
		}else
		    Toast.makeText(JingleCallActivity.this, "Remplir le champ (JID complet en  toto@tutu.com/truc)", Toast.LENGTH_SHORT);
	    }
	});

    }

}
