package org.knowm.xchange.bittrex.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.knowm.xchange.bittrex.*;
import org.knowm.xchange.bittrex.dto.BittrexException;
import org.knowm.xchange.bittrex.dto.account.BittrexAddress;
import org.knowm.xchange.bittrex.dto.account.BittrexComissionRatesWithMarket;
import org.knowm.xchange.bittrex.dto.withdrawal.BittrexWithdrawal;
import org.knowm.xchange.client.ResilienceRegistries;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.account.AccountInfo;
import org.knowm.xchange.dto.account.AddressWithTag;
import org.knowm.xchange.dto.account.Fee;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.account.AccountService;
import org.knowm.xchange.service.trade.params.TradeHistoryParams;
import org.knowm.xchange.service.trade.params.TradeHistoryParamsZero;

public class BittrexAccountService extends BittrexAccountServiceRaw implements AccountService {

  /**
   * Constructor
   *
   * @param exchange
   */
  public BittrexAccountService(
      BittrexExchange exchange,
      BittrexAuthenticated bittrex,
      ResilienceRegistries resilienceRegistries) {
    super(exchange, bittrex, resilienceRegistries);
  }

  @Override
  public AccountInfo getAccountInfo() throws IOException {
    try {
      return new AccountInfo(BittrexAdapters.adaptWallet(getBittrexBalances()));
    } catch (BittrexException e) {
      throw BittrexErrorAdapter.adapt(e);
    }
  }

  @Override
  public String requestDepositAddress(Currency currency, String... args) throws IOException {
    try {
      return getBittrexDepositAddresses(currency.getCurrencyCode()).get(0).getCryptoAddress();
    } catch (BittrexException e) {
      throw BittrexErrorAdapter.adapt(e);
    }
  }

  @Override
  public AddressWithTag requestDepositAddressData(Currency currency, String... args)
      throws IOException {
    try {
      BittrexAddress address = getBittrexDepositAddresses(currency.getCurrencyCode()).get(0);
      return new AddressWithTag(address.getCryptoAddress(), address.getCryptoAddressTag());
    } catch (BittrexException e) {
      throw BittrexErrorAdapter.adapt(e);
    }
  }

  @Override
  public TradeHistoryParams createFundingHistoryParams() {
    return TradeHistoryParamsZero.PARAMS_ZERO;
  }

  @Override
  public Map<Instrument, Fee> getDynamicTradingFeesByInstrument() throws IOException {
    Map<CurrencyPair, Fee> dynamicTradingFees = getDynamicTradingFees();
    return dynamicTradingFees.entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  }

  @Override
  public Map<CurrencyPair, Fee> getDynamicTradingFees() throws IOException {
    try {
      Map<CurrencyPair, Fee> result = new HashMap<>();
      List<BittrexComissionRatesWithMarket> tradingFees = getTradingFees();
      for (BittrexComissionRatesWithMarket tradingFee : tradingFees) {
        result.put(
            BittrexUtils.toCurrencyPair(tradingFee.getMarketSymbol()),
            new Fee(
                BigDecimal.valueOf(tradingFee.getMakerRate()),
                BigDecimal.valueOf(tradingFee.getTakerRate())));
      }
      return result;
    } catch (BittrexException e) {
      throw BittrexErrorAdapter.adapt(e);
    }
  }

  @Override
  public String withdrawFunds(Currency currency, BigDecimal amount, String address)
      throws IOException {
    try {
      BittrexWithdrawal createdWithdrawal = createNewWithdrawal(currency, amount, address);
      return createdWithdrawal.getId();
    } catch (BittrexException e) {
      throw BittrexErrorAdapter.adapt(e);
    }
  }
}

@@ -435,75 +435,8 @@
 et;%0A
-import org.knowm.xchange.bittrex.dto.withdrawal.BittrexWithdrawal;%0A
 impo
@@ -3508,45 +3508,14 @@
     
-BittrexWithdrawal createdWithdrawal =
+return
  cre
@@ -3561,40 +3561,8 @@
 ess)
-;%0A      return createdWithdrawal
 .get

Format code