package at.outdated.bitcoin.exchange.api.account;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;
import at.outdated.bitcoin.exchange.api.market.Market;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ebirn on 25.01.14.
 */
public class Balance {

    Map<Currency,CurrencyValue> available = new HashMap<>();
    Map<Currency,CurrencyValue> open = new HashMap<>();

    public Balance() {
    }

    public Balance(Market market) {
        for(Currency c : market.getCurrencies()) {
            available.put(c, new CurrencyValue(c));
            open.put(c, new CurrencyValue(c));
        }
    }

    public CurrencyValue getOpen(Currency c) {
        return open.get(c);
    }

    public CurrencyValue getAvailable(Currency c) {
        return available.get(c);
    }

    public void setOpen(CurrencyValue cv) {
        open.put(cv.getCurrency(), cv);
    }

    public void setAvailable(CurrencyValue cv) {
        available.put(cv.getCurrency(), cv);
    }

    public boolean isAvailable(Currency c) {
        return (available.containsKey(c) && available.get(c).isPositive());
    }

    public CurrencyValue getTotal(Currency c) {

        CurrencyValue openValue = open.get(c);
        CurrencyValue availValue = available.get(c);


        CurrencyValue total = new CurrencyValue(c);

        if(openValue != null) total.add(openValue);
        if(availValue != null) total.add(availValue);

        return total;
    }

    public String longString() {
        StringBuilder builder = new StringBuilder();

        for(Currency c : Currency.values()) {
            CurrencyValue currencyBalance = getTotal(c);
            if(currencyBalance != null && currencyBalance.isPositive()) {
                builder.append(c.name());
                builder.append("=");
                builder.append(currencyBalance.getValue().toString());
                builder.append(" ");
            }
        }

        return builder.toString();
    }

    @Override
    public String toString() {
        return "Balance: " + longString();
    }
}

@@ -244,37 +244,9 @@
 til.
-HashMap;%0Aimport java.util.Map
+*
 ;%0A%0A/
@@ -446,24 +446,82 @@
 Balance() %7B%0A
+        initCurrencies(Arrays.asList(Currency.values()));%0A
     %7D%0A%0A    p
@@ -559,33 +559,31 @@
         
-for(
+init
 Currenc
-y c : 
+ies(
 market.g
@@ -597,31 +597,148 @@
 ncies())
- %7B%0A            
+;%0A    %7D%0A%0A    private void initCurrencies(Collection%3CCurrency%3E currencies) %7B%0A        for(Currency c : currencies) %7B%0A            this.
 availabl
@@ -773,32 +773,37 @@
 ));%0A            
+this.
 open.put(c, new 
@@ -900,16 +900,21 @@
  return 
+this.
 open.get
@@ -1069,16 +1069,21 @@
         
+this.
 open.put
@@ -1170,16 +1170,21 @@
         
+this.
 availabl
@@ -1280,16 +1280,21 @@
 return (
+this.
 availabl
@@ -1313,16 +1313,21 @@
 y(c) && 
+this.
 availabl
@@ -1440,16 +1440,21 @@
 Value = 
+this.
 open.get
@@ -1493,16 +1493,21 @@
 Value = 
+this.
 availabl
@@ -1806,40 +1806,80 @@
     
-for(Currency c : Currency.values
+boolean hasFunds = false;%0A        for(Currency c : this.available.keySet
 ()) 
@@ -2214,53 +2214,563 @@
     
-%7D%0A        %7D%0A%0A        return builder.toString(
+    hasFunds = true;%0A            %7D%0A        %7D%0A%0A        if(!hasFunds) %7B%0A            return %22none.%22;%0A        %7D%0A%0A        return builder.toString();%0A    %7D%0A%0A    public boolean isZero() %7B%0A%0A        for(Currency c : this.available.keySet()) %7B%0A            if(this.available.get(c).isPositive()) return false;%0A            if(this.open.get(c).isPositive()) return false;%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    public boolean hasFunds() %7B%0A        return !isZero();%0A    %7D%0A%0A    public Set%3CCurrency%3E currencies() %7B%0A        return new HashSet%3C%3E(this.available.keySet()
 );%0A 

add summary methods