package com.salesforce.samples.vfconnector;

import java.util.HashMap;

import org.apache.http.cookie.Cookie;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.phonegap.DroidGap;
import com.salesforce.androidsdk.app.ForceApp;
import com.salesforce.androidsdk.rest.ClientManager;
import com.salesforce.androidsdk.rest.ClientManager.RestClientCallback;
import com.salesforce.androidsdk.rest.RestClient;

public class VFConnectorActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //ensure that we allow urls from all salesforce domains to be loaded
        this.addWhiteListEntry("force.com", true);
        this.addWhiteListEntry("salesforce.com", true);
        
        //super.loadUrl("file:///android_asset/www/unauthorized.html");
        
        final String accountType = getString(R.string.account_type);
		new ClientManager(this, accountType).getRestClient(this, new RestClientCallback() {
			public void authenticatedRestClient(RestClient client) {
				if (client == null) {
					ForceApp.APP.logout(accountType);
					return;
				}
				
				loggedIn(client);			
			}
		});
    }
    
    protected void loggedIn(RestClient client) {
    	//TODO setup cookies
    	
    	//TODO load real content
        //super.loadUrl("file:///android_asset/www/index.html");

    	this.setSidCookies(client);
    

    	String host = client.getBaseUrl().getHost();
    	String finalURL = "https://" + host + "/apex/BasicVFPage";
    	super.loadUrl(finalURL);
    	
    }
    
    
    protected void addSidCookieForDomain(CookieManager cookieMgr, String domain, String sid) {
        String cookieStr = "sid=" + sid + "; domain=" + domain;
    	cookieMgr.setCookie(domain, cookieStr);
    	Log.i("Cookiez","addSidCookie: " + cookieStr);
    }
    
    protected void setSidCookies(RestClient client) {
    	//ensure we have a CookieSyncManager
    	CookieSyncManager cookieSyncMgr = CookieSyncManager.createInstance(this);

    	CookieManager cookieMgr = CookieManager.getInstance();
    	//cookieMgr.removeSessionCookie(); //tODO check
    	String accessToken = client.getAuthToken();
    	String domain = client.getBaseUrl().getHost();

    	addSidCookieForDomain(cookieMgr,".force.com",accessToken);
    	addSidCookieForDomain(cookieMgr,".salesforce.com",accessToken);
    	addSidCookieForDomain(cookieMgr,domain,accessToken);

	    cookieSyncMgr.sync();
	    
    	


    }
    
//    protected void sendJavascriptLoginEvent(RestClient client) {
//		HashMap<String, String> data = new HashMap<String, String>();
//		data.put("clientId", getString(R.string.oauth_client_id));
//		data.put("loginUrl", getString(R.string.login_url));
//		data.put("apiVersion", getString(R.string.api_version));
//		data.put("accessToken", client.getAuthToken());
//		data.put("instanceUrl", client.getBaseUrl().toString());
//		data.put("refreshToken", client.getRefreshToken());
//		
//		String eventJs = "{'data':" + new JSONObject(data).toString() + "}";
//		String jsCall = "onSalesforceOAuthLogin(" + eventJs + ")";
//		sendJavascript(jsCall);
//    }
}
@@ -567,16 +567,63 @@
 idGap %7B%0A
+    public static String TAG = %22VFConnector%22;%0A%0A
     /** 
@@ -668,16 +668,16 @@
 ted. */%0A
-
     @Ove
@@ -776,24 +776,112 @@
 anceState);%0A
+    %09//ensure we have a CookieSyncManager%0A    %09CookieSyncManager.createInstance(this);%0A%0A
         //en
@@ -1485,32 +1485,283 @@
 %09%7D);%0A    %7D%0A    %0A
+    @Override%0A    public void onResume() %7B%0A    %09CookieSyncManager.getInstance().startSync();%0A    %09super.onResume();%0A    %7D%0A    %0A    @Override%0A    public void onPause() %7B%0A    %09CookieSyncManager.getInstance().stopSync();%0A    %09super.onPause();%0A    %7D%0A    %0A
     protected vo
@@ -2345,17 +2345,11 @@
 g.i(
-%22Cookiez%22
+TAG
 ,%22ad
@@ -2362,23 +2362,29 @@
 okie
-: %22 + cookieStr
+ForDomain: %22 + domain
 );%0A 
@@ -2451,50 +2451,8 @@
 ) %7B%0A
-    %09//ensure we have a CookieSyncManager%0A
     
@@ -2496,38 +2496,35 @@
 okieSyncManager.
-create
+get
 Instance(this);%0A
@@ -2512,31 +2512,32 @@
 getInstance(
-this);%0A
+);%0A    %09
 %0A    %09Cookie
@@ -2590,18 +2590,16 @@
 );%0A    %09
-//
 cookieMg
@@ -2626,21 +2626,9 @@
 e();
- //tODO check
+%0A
 %0A   
@@ -2722,24 +2722,144 @@
 getHost();%0A%0A
+    %09//set the cookie on all possible domains we could access%0A    %09addSidCookieForDomain(cookieMgr,domain,accessToken);%0A
     %09addSidC
@@ -2982,66 +2982,8 @@
 en);
-%0A    %09addSidCookieForDomain(cookieMgr,domain,accessToken);
 %0A%0A%09 
@@ -3011,22 +3011,14 @@
 ();%0A
+
 %09    %0A
-    %09%0A%0A%0A
     

fix problem with cookiesyncmgr not syncing