package com.popebp.bitcoin.exchange.btce;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.json.JSONObject;

import android.net.Uri;

import org.apache.commons.codec.binary.Hex;
import org.apache.http.message.BasicHeader;

public class BTCe {
	public static final String API_URL = "https://btc-e.com/tapi";
	private String apiKey = null;
	private String apiSecret = null;
	
	/*
	 * Needs
	 *  o Method that builds the QueryParams and signs
	 *  o Method that builds the Headers
	 *  o Method that takes in api path, query params, headers and makes request (returns JSONObject)
	 *  o ResultFactory that builds each result type
	 */
	
	public BTCe(String key, String secret) {
		this.apiKey = key;
		this.apiSecret = secret;
	}
	
	private JSONObject doRequest(String uri) {
		return doRequest(uri, "");
	}
	
	private JSONObject doRequest(String uri, String postBody) {
		// 1. Build Headers with key and signature of post body
		// 2. Make a POST request to uri, with headers and postBody
		// 3. Read response from server
		// 4. Return parse of JSONObject
		
		BasicHeader keyHeader = new BasicHeader("Key", apiKey);
		// HMAC-SHA512 of the post params with the apiSecret
		SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA512");
		try {
			Mac mac = Mac.getInstance("HmacSHA512");
			mac.init(spec);
			byte[] signatureBytes = mac.doFinal(postBody.getBytes());
			BasicHeader signHeader = new BasicHeader("Sign", Hex.encodeHex(signatureBytes).toString());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public Result getInfo() {
		String uri = BTCe.buildTradeUrl("getInfo");
		return new GetInfoResult();
	}
	
	public Result getTransactionHistory(Parameters params) {
		String uri = BTCe.buildTradeUrl("TransHistory");
		return new TransactionHistoryResult();
	}
	
	public Result getTradeHistory(Parameters params) {
		String uri = BTCe.buildTradeUrl("TradeHistory");
		return new TradeHistoryResult();
	}
	
	public Result getActiveOrders(Parameters params) {
		String uri = BTCe.buildTradeUrl("ActiveOrders");
		return new ActiveOrdersResult();
	}
	
	public Result doTrade(Parameters params) {
		String uri = BTCe.buildTradeUrl("Trade");
		return new TradeResult();
	}
	
	public Result doCancelOrder(Parameters params) {
		String uri = BTCe.buildTradeUrl("CancelOrder");
		return new CancelOrderResult();
	}
	
	private static String buildTradeUrl(String action) {
		
		Uri uri = Uri.parse(API_URL);
		
		Uri.Builder builder = uri.buildUpon();
		builder.appendPath(action);
		
		return builder.toString();
	}
}

@@ -36,16 +36,127 @@
 .btce;%0A%0A
+import java.io.Closeable;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 import j
@@ -401,16 +401,215 @@
 ry.Hex;%0A
+import org.apache.http.client.ClientProtocolException;%0Aimport org.apache.http.client.HttpClient;%0Aimport org.apache.http.client.methods.HttpPost;%0Aimport org.apache.http.impl.client.DefaultHttpClient;%0A
 import o
@@ -1481,16 +1481,46 @@
 bject%0A%09%09
+BasicHeader signHeader = null;
 %0A%09%09Basic
@@ -1703,16 +1703,64 @@
 A512%22);%0A
+%09%09%0A%09%09// TODO: Refactor the code below its nasty%0A
 %09%09try %7B%0A
@@ -1886,28 +1886,16 @@
 ());%0A%09%09%09
-BasicHeader 
 signHead
@@ -2170,24 +2170,538 @@
 e();%0A%09%09%7D%0A%09%09%0A
+%09%09HttpPost post = new HttpPost();%0A%09%09post.addHeader(keyHeader);%0A%09%09post.addHeader(signHeader);%0A%09%09try %7B%0A%09%09%09post.setURI(new URI(uri));%0A%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09HttpClient client = new DefaultHttpClient();%0A%09%09try %7B%0A%09%09%09client.execute(post);%0A%09%09%7D catch (ClientProtocolException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A
 %09%09return nul
@@ -2777,24 +2777,42 @@
 %22getInfo%22);%0A
+%09%09doRequest(uri);%0A
 %09%09return new

Add some boilerplate http client code