package com.example.androidhive;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
 
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;

public class HomesActivity extends Activity 
{
	List<Button> properties;// = new ArrayList<Button>();
	Button btnNewProp;
	Button btnLogout;
	private static String username;
	
	UserFunctions userFunctions;
	
	// JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_TUPLE = "tuples";
    private static String KEY_IDPROPERTY = "idProperty";
    private static String KEY_ADDRESS = "address";
    private static String KEY_HOUSEURL = "houseURL";
    private static String KEY_NAME = "name";
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homes);
		
		properties = new ArrayList<Button>();
		
		userFunctions = new UserFunctions();
		
		// may need to account for newly registered user here
		Intent intent = getIntent();
		username = intent.getStringExtra("name");// pull from previous page
		//dbID = intent.getStringExtra("id");
		
		// make database call
		UserFunctions userFunction = new UserFunctions();
    	JSONObject json = userFunction.getHomes(username);
    	if(json == null){
    		Log.e("com.examples.androidhive","no json received.");
    	}
    	
    	//lists of received data
    	List<Integer> idProperty = new ArrayList<Integer>();
    	List<String> address = new ArrayList<String>();
    	List<String> houseURL = new ArrayList<String>();
    	
    	try {
                if (json.getString(KEY_SUCCESS) != null) {
                    //loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                    	JSONArray tuples = json.getJSONArray(KEY_TUPLE);
                    	for(int i = 0; i< tuples.length(); i++){
                    		JSONObject curTuple = tuples.getJSONObject(i);
                    		idProperty.add(curTuple.getInt(KEY_IDPROPERTY));
                    		address.add(curTuple.getString(KEY_ADDRESS));
                    		houseURL.add(curTuple.getString(KEY_HOUSEURL));
                    	}
                    }
                        
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
     
        
		// fill xml based on db call
        LinearLayout ll = (LinearLayout) findViewById(R.id.roomsLayout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        
        // may need separate file
        for(int i = 0; i < idProperty.size(); i++)
        {
        	
            Button btn = new Button(this);
            btn.setId(i);
            final int id_ = btn.getId();
            // set text to address
            btn.setText(address.get(i).toString());
            ll.addView(btn, params);
            // fill properties based on db call
            properties.add((Button) findViewById(id_));
        }
        
        
        Button btn = new Button(this);
        btn.setId(idProperty.size());
        btn.setText("Add new property +");
        ll.addView(btn, params);
        btnNewProp = (Button) findViewById(idProperty.size());
        
      //logout button
        Button logout = new Button(this);
        logout.setId(idProperty.size()+1);
        logout.setText("Logout");
        ll.addView(logout, params);
        btnLogout = (Button) findViewById(idProperty.size()+1);
       
		
		
		for(int i = 0; i < properties.size(); i++)
		{
			final String addr = address.get(i);
			final int id = idProperty.get(i);
			properties.get(i).setOnClickListener(new View.OnClickListener() 
			{

				@Override
				public void onClick(View arg0) {
					// go to next page with given property selected
					
					Intent next = new Intent(getApplicationContext(),
	                        RoomsActivity.class);
					next.putExtra("addr", addr);
					next.putExtra("id",id);
	                startActivity(next);
				}
				 
			});
		}
		
		 btnNewProp.setOnClickListener(new View.OnClickListener() 
		 {

			@Override
			public void onClick(View arg0) {
				// create new entry in database and go to next page
				
				// int value = userFunctions.newHome(username);
				
				Intent next = new Intent(getApplicationContext(),
                        RoomsActivity.class);
				next.putExtra("addr", "New Property");
				//next.putExtra("id",value);
                startActivity(next);
			}
			 
		 });
		 
		 btnLogout.setOnClickListener(new View.OnClickListener() 
		 {

			@Override
			public void onClick(View arg0) {
				userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing rooms screen
                finish();
				
			}
			 
		 });
	}
}

@@ -4668,19 +4668,30 @@
 (%22id%22,id
+.toString()
 );%0A
-
 %09       
@@ -4926,58 +4926,77 @@
 %09%09%09%09
-%0A%09%09%09%09// int value = userFunctions.newHome(
+JSONObject jsonID = userFunctions.addProperty(%22New Property%22,
 username
 );%0A%09
@@ -4991,23 +4991,120 @@
 username
+,null,0
 );%0A
+%0A
 %09%09%09%09
+int value = (int) jsonID.getJSONArray(KEY_TUPLE).getJSONObject(0).getInt(KEY_IDPROPERTY);
 %0A%09%09%09%09Int
@@ -5243,18 +5243,16 @@
 %22);%0A%09%09%09%09
-//
 next.put
@@ -5267,16 +5267,27 @@
 d%22,value
+.toString()
 );%0A     

Update HomesActivity.java