package org.dosomething.android.activities;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.dosomething.android.R;
import org.dosomething.android.context.SessionContext;
import org.dosomething.android.context.UserContext;
import org.dosomething.android.tasks.AbstractWebserviceTask;
import org.json.JSONObject;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.facebook.android.Facebook;
import com.google.inject.Inject;

public class Login extends RoboActivity {
	
	private static final int REQ_SIGN_UP = 111;
	private static final int REQ_FACEBOOK_LOGIN = 111;
    
	private Facebook facebook = new Facebook("105775762330");
	
	@InjectView(R.id.username) private EditText username;
	@InjectView(R.id.password) private EditText password;
	
	@Inject private SessionContext sessionContext;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
    
    public void logIn(View v){
    	String email = this.username.getText().toString();
    	String password = this.password.getText().toString();
    	
    	//goToProfile();
    	
    	new MyTask(email, password).execute();
    }
    
    private void goToProfile(){
    	startActivity(new Intent(this, Profile.class));
    	finish();
    }
    
    public void signUp(View v){
    	startActivityForResult(new Intent(this, Register.class), REQ_SIGN_UP);
    }
    
    public void facebookLogin(View v){

//    	facebook.authorize(this, new String[]{"birthday"}, REQ_FACEBOOK_LOGIN, new DialogListener(){
//
//			@Override
//			public void onComplete(Bundle values) {
//				Toast.makeText(getApplicationContext(), "complete", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onFacebookError(FacebookError e) {
//				Toast.makeText(getApplicationContext(), "facebook error", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onError(DialogError e) {
//				Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onCancel() {
//				Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
//			}
//    		
//    	});
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQ_FACEBOOK_LOGIN){
        	 facebook.authorizeCallback(requestCode, resultCode, data);
        }else if(requestCode == REQ_SIGN_UP){
        	if(resultCode == RESULT_OK){
        		setResult(RESULT_OK);
        		finish();
        	}
        }
    }
    
	private class MyTask extends AbstractWebserviceTask {

		private String username;
		private String password;
		
		private boolean loginSuccess;
		
		public MyTask(String username, String password) {
			super(sessionContext);
			this.username = username;
			this.password = password;
		}

		@Override
		protected void onSuccess() {
			
			if(loginSuccess) {
				goToProfile();
			} else {
				Toast.makeText(Login.this, getString(R.string.log_in_auth_failed), Toast.LENGTH_LONG).show();
			}
		}
		
		@Override
		protected void onFinish() { /*ignore */	}

		@Override
		protected void onError() {
			
			Toast.makeText(Login.this, getString(R.string.log_in_failed), Toast.LENGTH_LONG).show();
		}

		@Override
		protected void doWebOperation() throws Exception {
			String url = "http://www.dosomething.org/?q=rest/user/login.json";
			
			Map<String,String> params = new HashMap<String, String>();
			params.put("username", username);
			params.put("password", password);
			
			WebserviceResponse response = doPost(url, params);
			
			CookieStore cookieStore = (CookieStore) sessionContext.getHttpContext().getAttribute(ClientContext.COOKIE_STORE);
			
			if(cookieStore.getCookies() != null){
				for(Cookie cookie : cookieStore.getCookies()){
					Log.e("ME", cookie.getName() + " : " + cookie.getValue());
				}
			}
			
			if(response.getStatusCode()>=400 && response.getStatusCode()<500) {
				loginSuccess = false;
			} else {
				JSONObject obj = response.getBodyAsJSONObject();
				JSONObject user = obj.getJSONObject("user");
				String uid = user.getString("uid");
				
				new UserContext(getApplicationContext()).setLoggedIn(uid);
				loginSuccess = true;
			}
		}
	}
}
@@ -91,143 +91,8 @@
 p;%0A%0A
-import org.apache.http.client.CookieStore;%0Aimport org.apache.http.client.protocol.ClientContext;%0Aimport org.apache.http.cookie.Cookie;%0A
 impo
@@ -455,33 +455,8 @@
 le;%0A
-import android.util.Log;%0A
 impo
@@ -3871,16 +3871,18 @@
 s);%0A%09%09%09%0A
+//
 %09%09%09Cooki
@@ -3990,20 +3990,24 @@
 STORE);%0A
+//
 %09%09%09%0A
+//
 %09%09%09if(co
@@ -4039,16 +4039,18 @@
  null)%7B%0A
+//
 %09%09%09%09for(
@@ -4092,16 +4092,18 @@
 ies())%7B%0A
+//
 %09%09%09%09%09Log
@@ -4162,14 +4162,18 @@
 ));%0A
+//
 %09%09%09%09%7D%0A
+//
 %09%09%09%7D

add cookies to all requests