package org.azavea.otm.ui;

import org.azavea.otm.App;
import org.azavea.otm.Download;
import org.azavea.otm.LoginManager;
import org.azavea.otm.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private LoginManager loginManager = App.getLoginManager();
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    }
    
    private boolean validate(String user, String pass) {
    	if ("".equals(user) || "".equals(pass)) {
    		String msg = "Please enter both a Username and a Password";
    		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    		return false;
    	}
    	return true;
    }
    
    public void cancel(View view) {
		Intent next = new Intent(App.getInstance(), Download.class);
        startActivity(next);    	
    }
    
    public void login(View view) {
    	String username = ((EditText)findViewById(R.id.login_username))
    			.getText().toString().trim();
    	String password = ((EditText)findViewById(R.id.login_password))
    			.getText().toString().trim();
    
    	if (validate(username, password)) {
    		final ProgressDialog dialog = ProgressDialog.show(this, "", 
                    "Logging in...", true, true);
    		loginManager.logIn(this, username, password, new Callback() {

				@Override
				public boolean handleMessage(Message msg) {
					dialog.cancel();
					Bundle data = msg.getData();
					if(data != null && data.getBoolean("success")) {
						Intent next = new Intent(App.getInstance(), Download.class);
			            startActivity(next);
			            return true;
					} else {
						Toast.makeText(App.getInstance(), 
								data.getString("message"), 
								Toast.LENGTH_LONG).show();
						return false;
					}
					
				} 
			});
    		
    	}
    }
    

}

@@ -52,40 +52,8 @@
 pp;%0A
-import org.azavea.otm.Download;%0A
 impo
@@ -178,39 +178,8 @@
 og;%0A
-import android.content.Intent;%0A
 impo
@@ -1087,104 +1087,55 @@
 ) %7B%0A
-%09%09Intent next = new Intent(App.getInstance(), Download.class);%0A        startActivity(next);    %09
+    %09setResult(RESULT_CANCELED);%0A    %09finish();
 %0A   
@@ -1439,16 +1439,129 @@
 ) %7B%0A    
+%09%09requestLogin(username, password);%0A    %09%7D%0A    %7D%0A%0A%09private void requestLogin(String username, String password) %7B%0A
 %09%09final 
@@ -1631,20 +1631,16 @@
         
- 
-   
 %22Logging
@@ -1661,20 +1661,16 @@
  true);%0A
-    
 %09%09loginM
@@ -1726,17 +1726,16 @@
 ck() %7B%0A%0A
-%09
 %09%09%09@Over
@@ -1742,17 +1742,16 @@
 ride%0A%09%09%09
-%09
 public b
@@ -1790,17 +1790,16 @@
 ) %7B%0A%09%09%09%09
-%09
 dialog.c
@@ -1803,25 +1803,24 @@
 g.cancel();%0A
-%09
 %09%09%09%09Bundle d
@@ -1848,32 +1848,15 @@
 %09%09%09%09
-%09
 if(data
- != null && data
 .get
@@ -1886,107 +1886,45 @@
 %09%09%09%09
-%09Intent next = new Intent(App.getInstance(), Download.class
+setResult(RESULT_OK
 );%0A%09%09%09
+%09
-            startActivity(next
+%09finish(
 );%0A
-%09
 %09%09  
@@ -1950,17 +1950,16 @@
 ue;%0A%09%09%09%09
-%09
 %7D else %7B
@@ -1959,17 +1959,16 @@
  else %7B%0A
-%09
 %09%09%09%09%09Toa
@@ -2006,17 +2006,16 @@
 %0A%09%09%09%09%09%09%09
-%09
 data.get
@@ -2037,25 +2037,24 @@
 %22), %0A%09%09%09%09%09%09%09
-%09
 Toast.LENGTH
@@ -2068,17 +2068,16 @@
 show();%0A
-%09
 %09%09%09%09%09ret
@@ -2095,26 +2095,18 @@
 %09%09%09%09
-%09
 %7D%0A
-%09%09%09%09%09%0A%09
 %09%09%09%7D %0A%09%09
 %09%7D);
@@ -2105,37 +2105,14 @@
  %0A%09%09
-%09
 %7D);%0A
-    %09%09%0A    %09%7D%0A    %7D%0A    %0A
+%09%7D 
 %0A%7D%0A

Refactor login method to be less nested