package com.popebp.bitcoin.exchange.btce;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.math.BigDecimal;
import java.math.BigInteger;

public abstract class ResultFactory {
	
	private static Map<Currency, BigDecimal> jsonFundsToList(JSONObject obj) throws JSONException {
		
		Map<Currency, BigDecimal> funds = new HashMap<Currency, BigDecimal>();
		
		for(Iterator<String> keyItr = obj.keys(); keyItr.hasNext();) {
			String currency = keyItr.next();
			funds.put(CurrencyFactory.parseCurrency(currency), BigDecimal.valueOf(obj.getDouble(currency)));
		}
		
		return funds;
	}
	
	public static Rights createRights(JSONObject obj) {
		Rights rights = new Rights();
		try {
			rights.setInfo(obj.getBoolean("info"));
			rights.setTrade(obj.getBoolean("trade"));
		} catch (JSONException ex) {
			// TODO: throw a 'InvalidResultException'
		}
		return rights;
	}
	
	public static Result createGetInfoResult(JSONObject obj) {
		GetInfoResult result = new GetInfoResult();
		
		try {
			result.setFunds(ResultFactory.jsonFundsToList(obj.getJSONObject("funds")));
			result.setRights(ResultFactory.createRights(obj.getJSONObject("rights")));
			result.setTransactionCount(BigInteger.valueOf(obj.getLong("transaction_count")));
			result.setOpenOrders(BigInteger.valueOf(obj.getLong("open_orders")));
			result.setServerTime(BigInteger.valueOf(obj.getLong("server_time")));
		} catch (JSONException ex) {
			// TODO: throw a 'InvalidResultException'
		}
		
		return result;
	}
	
	public static Result createTransactionHistoryResult(JSONObject obj) {
		TransactionHistoryResult result = new TransactionHistoryResult();
		
		JSONArray transactionNames = obj.names();
							
		try {
			if(transactionNames.length() != 1) {
				// Throw exception pertaining to length
				return null;
			} else {
				result.setTransactionId(BigInteger.valueOf(transactionNames.getInt(0)));
			}
			
			JSONObject subObj = obj.getJSONObject(transactionNames.getString(0));
			
			result.setAmount(BigDecimal.valueOf(subObj.getDouble("amount")));
			result.setCurrency(CurrencyFactory.parseCurrency(subObj.getString("currency")));
			result.setDescription(subObj.getString("desc"));
			result.setStatus(TransactionStatus.valueOf(subObj.getString("status")));
			result.setTimestamp(BigInteger.valueOf(subObj.getLong("timestamp")));
			result.setType(TradeType.valueOf(subObj.getString("type")));
		} catch (JSONException ex) {
			// TODO: throw a 'InvalidResultException'
		}
		
		return result;
	}
}

@@ -317,14 +317,13 @@
 %0D%0A%09p
-rivate
+ublic
  sta
@@ -352,31 +352,33 @@
 ecimal%3E 
-jsonFundsToList
+createCurrencyMap
 (JSONObj
@@ -1181,23 +1181,25 @@
 ory.
-jsonFundsToList
+createCurrencyMap
 (obj
@@ -2043,19 +2043,20 @@
 ames.get
-Int
+Long
 (0)));%0D%0A
@@ -2500,24 +2500,24 @@
 stamp%22)));%0D%0A
-
 %09%09%09result.se
@@ -2678,11 +2678,1054 @@
 t;%0D%0A%09%7D%0D%0A
+%09%0D%0A%09public static Result createTradeHistoryResult(JSONObject obj) %7B%0D%0A%09%09TradeHistoryResult result = new TradeHistoryResult();%0D%0A%09%09%0D%0A%09%09JSONArray tradeNames = obj.names();%0D%0A%09%09%09%0D%0A%09%09try %7B%0D%0A%09%09%09if(tradeNames.length() != 1) %7B%0D%0A%09%09%09%09// Throw exception pertaining to length%0D%0A%09%09%09%09return null;%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09result.setTradeId(BigInteger.valueOf(tradeNames.getLong(0)));%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%09JSONObject subObj = obj.getJSONObject(tradeNames.getString(0));%0D%0A%09%09%09%0D%0A%09%09%09result.setAmount(BigDecimal.valueOf(subObj.getDouble(%22amount%22)));%0D%0A%09%09%09result.setPair(CurrencyPairFactory.parsePair(subObj.getString(%22pair%22)));%0D%0A%09%09%09result.setType(TradeType.valueOf(subObj.getString(%22type%22)));%0D%0A%09%09%09result.setRate(BigDecimal.valueOf(subObj.getDouble(%22rate%22)));%0D%0A%09%09%09result.setTimestamp(BigInteger.valueOf(subObj.getLong(%22timestamp%22)));%0D%0A%09%09%09result.setOrderId(BigInteger.valueOf(subObj.getLong(%22order_id%22)));%0D%0A%09%09%09result.setYourOrder(subObj.getBoolean(%22is_your_order%22));%0D%0A%09%09%09%0D%0A%09%09%7D catch (JSONException ex) %7B%0D%0A%09%09%09// TODO: throw a 'InvalidResultException'%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09return result;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Add createTradeHistoryResult to ResultFactory