package org.knowm.xchange.hitbtc.v2.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.ws.rs.QueryParam;

import org.knowm.xchange.Exchange;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcAddress;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcBalance;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcInternalTransferResponse;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcTransaction;
import org.knowm.xchange.hitbtc.v2.dto.HitbtcTransferType;

import si.mazi.rescu.HttpStatusIOException;

public class HitbtcAccountServiceRaw extends HitbtcBaseService {

  public HitbtcAccountServiceRaw(Exchange exchange) {
    super(exchange);
  }

  public String withdrawFundsRaw(Currency currency, BigDecimal amount, String address) throws HttpStatusIOException {
    Map response = hitbtc.payout(amount, currency.getCurrencyCode(), address);
    //todo: handle "not enough funds" case more gracefully - the service returns a 409 with this body > {"code":"InvalidArgument","message":"Balance not enough"}
    return response.get("transaction").toString();
  }

  public HitbtcInternalTransferResponse transferFunds(Currency currency, BigDecimal amount, HitbtcTransferType hitbtcTransferType) throws IOException {

    return hitbtc.transferToTrading(amount, currency.getCurrencyCode(), hitbtcTransferType.getType());
  }

  public String transferToTrading(Currency currency, BigDecimal amount) throws IOException {

    HitbtcInternalTransferResponse response = transferFunds(currency, amount, HitbtcTransferType.BANK_TO_EXCHANGE);

    if (response.id == null) {
      throw new ExchangeException("transfer failed: " + response);
    }
    return response.id;
  }

  public String transferToMain(Currency currency, BigDecimal amount) throws IOException {
    HitbtcInternalTransferResponse response = transferFunds(currency, amount, HitbtcTransferType.EXCHANGE_TO_BANK);

    if (response.id == null) {
      throw new ExchangeException("transfer failed: " + response);
    }
    return response.id;
  }

  public List<HitbtcBalance> getMainBalance() throws IOException {
    return hitbtc.getMainBalance();
  }

  public List<HitbtcBalance> getTradingBalance() throws IOException {
    return hitbtc.getTradingBalance();
  }

  public String getDepositAddress(String currency) throws IOException {

    HitbtcAddress hitbtcDepositAddress = hitbtc.getHitbtcDepositAddress(currency);
    return hitbtcDepositAddress.getAddress();
  }

  public List<HitbtcTransaction> getTransactions(String currency, Integer limit, Integer offset) throws HttpStatusIOException {
    return hitbtc.transactions(currency, limit, offset);
  }

}

@@ -43,143 +43,8 @@
 e;%0A%0A
-import java.io.IOException;%0Aimport java.math.BigDecimal;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport javax.ws.rs.QueryParam;%0A%0A
 impo
@@ -469,17 +469,16 @@
 erType;%0A
-%0A
 import s
@@ -514,16 +514,119 @@
 ption;%0A%0A
+import java.io.IOException;%0Aimport java.math.BigDecimal;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 public c
@@ -964,169 +964,277 @@
 s);%0A
+%0A
     /
-/todo: handle %22not enough funds%22 case more gracefully - the service returns
+*%0A    todo:%0A    if there isn't enough money we get
  a 40
-9
+0
  with 
-this body %3E %7B%22code%22:%22InvalidArgument%22,%22message%22:%22Balance not enough%22%7D
+body:%0A%0A    %7B%22error%22:%7B%22code%22:20001,%22message%22:%22Insufficient funds%22,%22description%22:%22Check that the funds are sufficient, given commissions%22%7D%7D%0A%0A    ...but currently 400 errors don't reach this code%0A     */%0A
 %0A   
@@ -1255,27 +1255,18 @@
 se.get(%22
-transaction
+id
 %22).toStr
@@ -1425,25 +1425,24 @@
 Exception %7B%0A
-%0A
     return h

fix withdrawFundsRaw return value