package org.knowm.xchange.bitfinex.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.bitfinex.BitfinexErrorAdapter;
import org.knowm.xchange.bitfinex.dto.BitfinexException;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexDepth;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLend;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLendDepth;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexSymbolDetail;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTicker;
import org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTrade;
import org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexPublicFundingTrade;
import org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexPublicTrade;
import org.knowm.xchange.bitfinex.v2.dto.marketdata.Status;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import si.mazi.rescu.HttpStatusIOException;

/**
 * Implementation of the market data service for Bitfinex
 *
 * <ul>
 *   <li>Provides access to various market data values
 * </ul>
 */
public class BitfinexMarketDataServiceRaw extends BitfinexBaseService {

  /**
   * Constructor
   *
   * @param exchange
   */
  public BitfinexMarketDataServiceRaw(Exchange exchange) {

    super(exchange);
  }

  public BitfinexTicker getBitfinexTicker(String pair) throws IOException {
    BitfinexTicker bitfinexTicker = bitfinex.getTicker(pair);
    return bitfinexTicker;
  }

  public BitfinexDepth getBitfinexOrderBook(String pair, Integer limitBids, Integer limitAsks)
      throws IOException {
    BitfinexDepth bitfinexDepth;
    if (limitBids == null && limitAsks == null) {
      bitfinexDepth = bitfinex.getBook(pair);
    } else {
      bitfinexDepth = bitfinex.getBook(pair, limitBids, limitAsks);
    }
    return bitfinexDepth;
  }

  public BitfinexLendDepth getBitfinexLendBook(String currency, int limitBids, int limitAsks)
      throws IOException {
    BitfinexLendDepth bitfinexLendDepth = bitfinex.getLendBook(currency, limitBids, limitAsks);
    return bitfinexLendDepth;
  }

  public BitfinexTrade[] getBitfinexTrades(String pair, long sinceTimestamp) throws IOException {
    BitfinexTrade[] bitfinexTrades = bitfinex.getTrades(pair, sinceTimestamp);
    return bitfinexTrades;
  }

  public BitfinexLend[] getBitfinexLends(String currency, long sinceTimestamp, int limitTrades)
      throws IOException {
    BitfinexLend[] bitfinexLends = bitfinex.getLends(currency, sinceTimestamp, limitTrades);
    return bitfinexLends;
  }

  public Collection<String> getBitfinexSymbols() throws IOException {
    return bitfinex.getSymbols();
  }

  public List<CurrencyPair> getExchangeSymbols() throws IOException {
    try {
      List<CurrencyPair> currencyPairs = new ArrayList<>();
      for (String symbol : bitfinex.getSymbols()) {
        currencyPairs.add(BitfinexAdapters.adaptCurrencyPair(symbol));
      }
      return currencyPairs;
    } catch (BitfinexException e) {
      throw BitfinexErrorAdapter.adapt(e);
    }
  }

  public List<BitfinexSymbolDetail> getSymbolDetails() throws IOException {
    try {
      return bitfinex.getSymbolsDetails();
    } catch (BitfinexException e) {
      throw BitfinexErrorAdapter.adapt(e);
    }
  }

  //////// v2

  public org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker[] getBitfinexTickers(
      Collection<CurrencyPair> currencyPairs) throws IOException {
    return bitfinexV2.getTickers(BitfinexAdapters.adaptCurrencyPairsToTickersParam(currencyPairs));
  }

  public org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker getBitfinexTickerV2(
      CurrencyPair currencyPair) throws IOException {
    org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTicker[] ticker =
        bitfinexV2.getTickers(
            BitfinexAdapters.adaptCurrencyPairsToTickersParam(
                Collections.singletonList(currencyPair)));
    if (ticker.length == 0) {
      throw new BitfinexException("Unknown Symbol");
    } else {
      return ticker[0];
    }
  }

  public BitfinexPublicTrade[] getBitfinexPublicTrades(
      CurrencyPair currencyPair, int limitTrades, long startTimestamp, long endTimestamp, int sort)
      throws IOException {
    try {
      return bitfinexV2.getPublicTrades(
          BitfinexAdapters.adaptCurrencyPair(currencyPair),
          limitTrades,
          startTimestamp,
          endTimestamp,
          sort);
    } catch (HttpStatusIOException e) {
      throw new BitfinexException(e.getHttpBody());
    }
  }

  public BitfinexPublicFundingTrade[] getBitfinexPublicFundingTrades(
      Currency currency, int limitTrades, long startTimestamp, long endTimestamp, int sort)
      throws IOException {
    try {
      return bitfinexV2.getPublicFundingTrades(
          "f" + currency.toString(), limitTrades, startTimestamp, endTimestamp, sort);
    } catch (HttpStatusIOException e) {
      throw new BitfinexException(e.getHttpBody());
    }
  }

  public List<Status> getStatus(List<CurrencyPair> pairs) throws IOException {
    try {
      return bitfinexV2.getStatus(
          "deriv", BitfinexAdapters.adaptCurrencyPairsToTickersParam(pairs));
    } catch (HttpStatusIOException e) {
      throw new BitfinexException(e.getHttpBody());
    }
  }
}

@@ -2864,36 +2864,24 @@
 Exception %7B%0A
-    try %7B%0A  
     List%3CCur
@@ -2930,18 +2930,16 @@
 ();%0A    
- 
- 
 for (Str
@@ -2972,26 +2972,24 @@
 ymbols()) %7B%0A
-  
       curren
@@ -3049,22 +3049,18 @@
 ));%0A    
-  %7D%0A  
+%7D%0A
     retu
@@ -3081,93 +3081,8 @@
 rs;%0A
-    %7D catch (BitfinexException e) %7B%0A      throw BitfinexErrorAdapter.adapt(e);%0A    %7D%0A
   %7D%0A
@@ -3154,36 +3154,24 @@
 Exception %7B%0A
-    try %7B%0A  
     return b
@@ -3203,93 +3203,8 @@
 ();%0A
-    %7D catch (BitfinexException e) %7B%0A      throw BitfinexErrorAdapter.adapt(e);%0A    %7D%0A
   %7D%0A

Remove redundant exception adaptation