package org.opensourcecurrency.hack;

import static org.opensourcecurrency.hack.ConstantsProviders.PROVIDER_URL;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.net.URI;
import java.util.ArrayList;
import android.view.Gravity;
import android.widget.Toast;

import org.apache.http.client.methods.HttpGet;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import org.opensourcecurrency.hack.RestTask;

public class Transactions extends ListActivity {    
	TextView selection;
	private static final String OAUTH_LISTPAYMENTS_ACTION = "org.opensourcecurrency.hack.OAUTH_LISTPAYMENTS";
	private static final String TAG = "OpenTransact";
	private ProviderData providers;
	private ProgressDialog progress;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transactions);

		providers = new ProviderData(this);
		Asset asset = providers.getCurrentAsset(this);
		Provider provider = asset.getProvider();
		
		String access_token = provider.getAccessToken();
        Log.d(TAG,"access_token : " + access_token);
        
        if(access_token.equals("")) {
    		Toast toast = Toast.makeText(this, "No access token yet!", Toast.LENGTH_LONG);
    		toast.setGravity(Gravity.CENTER, 0, 0);
    		toast.show();
        	return;
        }
    	
    	try {
        	HttpGet transactionsRequest = new HttpGet(new URI(asset.url));
      		transactionsRequest.setHeader("Accept","application/json");
      		transactionsRequest.setHeader("Authorization","Bearer " + access_token);
        	RestTask task = new RestTask(this, OAUTH_LISTPAYMENTS_ACTION);
        	task.execute(transactionsRequest);
        	progress = ProgressDialog.show(this, "Fetching Transactions", "Waiting...", true);
    	} catch(Exception e) {
        	e.printStackTrace();    		
    	}
	}
    
	public void onListItemClick(ListView parent, View v, int position, long id) {
	 	//selection.setText(items[position]);
	}
	
    @Override
    public void onResume() {
      super.onResume();
      registerReceiver(receiver, new IntentFilter(OAUTH_LISTPAYMENTS_ACTION));
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	unregisterReceiver(receiver);
    }
    
    private BroadcastReceiver receiver = new BroadcastReceiver() {
    	private String counterparty(JSONObject transact,String emailAddress) {
    	    try {
          	    String to = transact.getString("to");
        	    String from = transact.getString("from");
                if(emailAddress.equals(to)) {
                	return from;
                } else {
                	return to;
                }
    	    } catch (Exception e) {
            	e.printStackTrace();    		    	    	
    	    }
    	    return "";
    	}
    	
    	private Boolean isPayer(JSONObject transact, String emailAddress) {
    		try {
                if(emailAddress.equals(transact.getString("from"))) {
                	return true;
                } else {
                	return false;
                }
    	    } catch (Exception e) {
            	e.printStackTrace();    		    	    	
    	    }
    	    return false;    		
    	}
    	
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		Log.d(TAG,"onReceive called (Transactions)!");
    		if(progress != null) {
    			progress.dismiss();
    		}
    		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    	    String emailAddress = prefs.getString("emailPref","");
    		String response = intent.getStringExtra(RestTask.HTTP_RESPONSE);
    		Log.d(TAG,"response(Transactions): "+response);

    		try {
              JSONArray transactions_response = new JSONArray(response);
          	  String[] items = new String[transactions_response.length()];
              for(int i=0;i<transactions_response.length();i++) {
            	  JSONObject transact = transactions_response.getJSONObject(i);
            	  String amount = transact.getString("amount");
            	  String sign = isPayer(transact,emailAddress) ? "-" : "+";
            	  items[i] = sign + amount + " " + counterparty(transact,emailAddress);
              }
              setListAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,items));
    		  selection=(TextView)findViewById(R.id.selection);
    		} catch (JSONException e) {
    		  e.printStackTrace();
    		}
    	}
    };
}

@@ -1167,16 +1167,45 @@
 ogress;%0A
+%09Provider m_Provider = null;%0A
     %0A%09@O
@@ -1436,18 +1436,11 @@
 ;%0A%09%09
-Provider p
+m_P
 rovi
@@ -1493,17 +1493,19 @@
 token = 
-p
+m_P
 rovider.
@@ -3849,95 +3849,8 @@
 %09%09%7D%0A
-    %09%09SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
 %0A   
@@ -3881,39 +3881,34 @@
 s = 
-prefs.getString(%22emailPref%22,%22%22)
+m_Provider.getUser().email
 ;%0A  

use email from user_info to determine counterparty in transactions