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.performance.CurrencyPerformance;
import at.outdated.bitcoin.exchange.api.performance.Performance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ebirn
 * Date: 26.05.13
 * Time: 14:54
 * To change this template use File | Settings | File Templates.
 */
public abstract class Wallet {

    protected CurrencyValue balance;

    protected CurrencyValue openOrders;

    protected Currency currency;

    protected List<WalletTransaction> transactions = new ArrayList<>();


    public Wallet() {
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setBalance(CurrencyValue balance) {
        this.balance = balance;
    }

    public CurrencyValue getBalance() {
        return balance;
    }

    public CurrencyValue getOpenOrders() {
        return openOrders;
    }

    public void addTransaction(WalletTransaction trans) {



        switch(trans.getType()) {
            case DEPOSIT:
            case FEE:
            case IN:
                balance.subtract(trans.getValue());
                break;

            case OUT:
            case WITHDRAW:
            case SPENT:
                balance.add(trans.getValue());
                break;

            default:
                throw new IllegalArgumentException("transaction type not implemented in wallet");
        }

        if(trans.getBalance() != null)
            this.balance = new CurrencyValue(trans.getBalance());
        else
            trans.setBalance(this.balance);


        transactions.add(trans);
    }

    public void setTransactions(List<WalletTransaction> transactions) {
        this.transactions = transactions;
    }

    public List<WalletTransaction> getTransactions() {
        return transactions;
    }

    public Performance getPerformance() {
        return getPerformance(new Date(0L));
    }

    public Performance getPerformance(Date since) {

        Performance perf = new CurrencyPerformance(getCurrency());
        for(WalletTransaction trans : getTransactions()) {
            if(since.before(trans.getTimestamp())) {
                perf.includeTransaction(trans);
            }
        }

        return perf;
    }


    public String toString() {
        return "Wallet: " + getCurrency() + ": " + getBalance();
    }
}

@@ -1213,30 +1213,8 @@
 IT:%0A
-            case FEE:%0A
     
@@ -1258,16 +1258,11 @@
 nce.
-subtract
+add
 (tra
@@ -1293,32 +1293,54 @@
         break;%0A%0A
+            case FEE:%0A
             case
@@ -1412,35 +1412,40 @@
         balance.
-add
+subtract
 (trans.getValue(

fix transaction / balance calculation