/* ownCloud Android Library is available under MIT license
 *   Copyright (C) 2014 ownCloud Inc.
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *   
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *   
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package com.owncloud.android.lib.common;

import java.io.IOException;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.httpclient.Cookie;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;

/**
 * Map for {@link OwnCloudClient} instances associated to ownCloud {@link Account}s
 * 
 * TODO check synchronization
 * 
 * TODO consider converting into a non static object saved in the application context 
 * @author David A. Velasco
 * @author masensio
 */

public class OwnCloudClientMap {
    
	
    private static ConcurrentMap<String, OwnCloudClient> mClients = 
            new java.util.concurrent.ConcurrentHashMap<String, OwnCloudClient>();
    
    public static synchronized OwnCloudClient getClientFor(Account account, Context context) 
            throws OperationCanceledException, AuthenticatorException, 
            AccountNotFoundException, IOException {
        
        OwnCloudClient client = mClients.get(account.name);
        if (client == null) {
            client = OwnCloudClientFactory.createOwnCloudClient(
                            account, 
                            context.getApplicationContext());
            mClients.putIfAbsent(account.name, client);
        }
        return client;
    }

    
    public static synchronized OwnCloudClient removeClientFor(Account account) {
    	return mClients.remove(account.name);
    }
    
    
    public static synchronized void clearPool() {
        mClients.clear();
    }
    
    public static synchronized void saveClient(Account account, Context context) {
    	
    	// Account Manager
    	AccountManager ac = AccountManager.get(context);
    	
    	OwnCloudClient client = mClients.get(account.name);
    	
    	Cookie[] cookies =client.getState().getCookies(); 
    	String cookiesString ="";
    	for (Cookie cookie: cookies) {
    		cookiesString = cookiesString + cookie.toString();
    	}
    	ac.setUserData(account, Constants.KEY_COOKIES, cookiesString); 
    	//Log.d("OwnCloudClientMap", "Saving Cookies: "+ cookiesString );
    }
}

@@ -1586,16 +1586,41 @@
 Context;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -3326,29 +3326,81 @@
 (context
-);%0A    %09%0A    
+.getApplicationContext());%0A    %09%0A    %09if (account!= null) %7B%0A    %09
 %09OwnClou
@@ -3444,26 +3444,22 @@
 .name);%0A
+%0A
     %09
-%0A    
 %09Cookie%5B
@@ -3507,16 +3507,17 @@
 ; %0A    %09
+%09
 String c
@@ -3539,16 +3539,17 @@
 %22;%0A    %09
+%09
 for (Coo
@@ -3577,16 +3577,17 @@
 %7B%0A    %09%09
+%09
 cookiesS
@@ -3631,22 +3631,30 @@
 ng()
+ + %22;%22
 ;%0A    %09
+%09
 %7D%0A    %09
+%09
 ac.s
@@ -3718,18 +3718,17 @@
 ; %0A    %09
-//
+%09
 Log.d(%22O
@@ -3783,16 +3783,420 @@
 ring );%0A
+    %09%7D%0A    %7D%0A    %0A    public static synchronized void saveAllClients(Context context, String accountType) %7B%0A    %09%0A    %09// Get all accounts%0A    %09Account %5B%5D accounts = AccountManager.get(context.getApplicationContext()).getAccountsByType(accountType);%0A    %09%0A    %09// Save cookies for all accounts%0A    %09for(Account account: accounts)%7B%0A    %09%09saveClient(account, context.getApplicationContext());%0A    %09%7D%0A    %09%0A
     %7D%0A%7D%0A

Create saveAllClients in OwnCloudClientMap. Changes from notes in commits: 8111900 and b18dffe