package com.rcarrillocruz.android.openstackdroid;

import android.net.Uri;
import android.os.Handler;

import android.app.ListActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.google.gson.Gson;
import com.rcarrillocruz.android.openstackdroid.CloudControllerResultReceiver.Receiver;

public class LoginActivity extends ListActivity implements Receiver {
	private CloudControllerResultReceiver mReceiver;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ConnectionProfile[] profiles = { 
        	new ConnectionProfile("admin on Testing cloud", "http://192.168.1.20:5000", "admin", "openstack", "793e8386d75f47b1bd078a3c0ddd9a49"),
        	new ConnectionProfile("devops on Production", "http://192.168.1.20:5000", "devops", "lol", "2")
        };		
        
        ArrayAdapter<ConnectionProfile> adapter = new ArrayAdapter<ConnectionProfile>(this, android.R.layout.simple_list_item_1, profiles);
        setListAdapter(adapter);
        
        mReceiver = new CloudControllerResultReceiver(new Handler());
        mReceiver.setReceiver(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ConnectionProfile profileClicked = (ConnectionProfile) getListAdapter().getItem(position);
        
        Intent intent = new Intent(this, CloudControllerService.class);
        intent.setData(Uri.parse("http://192.168.1.20:5000"));
        intent.putExtra(CloudControllerService.OPERATION, CloudControllerService.GET_TOKEN_OPERATION);
        intent.putExtra(CloudControllerService.RECEIVER, mReceiver);
        
        Bundle params = new Bundle();
        params.putString("username", profileClicked.getUsername());
        params.putString("password", profileClicked.getPassword());
        params.putString("tenantId", profileClicked.getTenantId());
        
        intent.putExtra(CloudControllerService.PARAMS, params);
        
        startService(intent);
    }


	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		// TODO Auto-generated method stub
		if (resultCode == 200) {
			Toast.makeText(this, resultData.getString("results"),Toast.LENGTH_LONG).show();
		}
	}
    
}

@@ -309,16 +309,46 @@
 w.Menu;%0A
+import android.view.MenuItem;%0A
 import a
@@ -2438,16 +2438,271 @@
 %0A    %7D%0A%0A
+%09@Override%0A%09public boolean onOptionsItemSelected(MenuItem item) %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09switch(item.getItemId()) %7B%0A%09%09case R.id.add_profile:%0A%09%09%09addConnectionProfile();%0A%09%09%09return true;%0A%09%09%7D%0A%09%09%0A%09%09return super.onOptionsItemSelected(item);%0A%09%7D%0A%0A
 %0A%09@Overr
@@ -2925,15 +2925,137 @@
 %0A%09%09%7D%0A%09%7D%0A
-    
+%0A%09private void addConnectionProfile() %7B%0A%09%09Intent i = new Intent(this, ConnectionProfileActivity.class);%0A%09%09startActivity(i);%0A%09%7D
 %0A%7D%0A

Add code to handle add profile button and hand out to ConnectionProfileActivity