package org.knowm.xchange.bithumb.service;

import java.io.IOException;
import java.util.Optional;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.bithumb.BithumbAdapters;
import org.knowm.xchange.bithumb.BithumbErrorAdapter;
import org.knowm.xchange.bithumb.BithumbException;
import org.knowm.xchange.bithumb.dto.trade.BithumbOpenOrdersParam;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.dto.trade.UserTrades;
import org.knowm.xchange.exceptions.NotAvailableFromExchangeException;
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;
import org.knowm.xchange.service.trade.TradeService;
import org.knowm.xchange.service.trade.params.*;
import org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair;
import org.knowm.xchange.service.trade.params.orders.OpenOrdersParams;

public class BithumbTradeService extends BithumbTradeServiceRaw implements TradeService {

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

  @Override
  public OpenOrders getOpenOrders() throws IOException {
    throw new NotAvailableFromExchangeException();
  }

  @Override
  public OpenOrders getOpenOrders(OpenOrdersParams params) throws IOException {

    final CurrencyPair currencyPair =
        Optional.ofNullable(params)
            .filter(p -> p instanceof OpenOrdersParamCurrencyPair)
            .map(p -> ((OpenOrdersParamCurrencyPair) p).getCurrencyPair())
            .orElse(null);

    try {
      return BithumbAdapters.adaptOrders(getBithumbOrders(currencyPair));
    } catch (BithumbException e) {
      throw BithumbErrorAdapter.adapt(e);
    }
  }

  @Override
  public String placeMarketOrder(MarketOrder marketOrder) throws IOException {
    return placeBithumbMarketOrder(marketOrder).getOrderId();
  }

  @Override
  public String placeLimitOrder(LimitOrder limitOrder) throws IOException {
    return placeBithumbLimitOrder(limitOrder).getOrderId();
  }

  @Override
  public boolean cancelOrder(CancelOrderParams orderParams) throws IOException {

    if (orderParams instanceof CancelOrderByPairAndIdParams) {
      final CancelOrderByPairAndIdParams params = (CancelOrderByPairAndIdParams) orderParams;
      return cancelBithumbOrder(Long.valueOf(params.getOrderId()), params.getCurrencyPair());
    } else {
      throw new NotYetImplementedForExchangeException(
          "Only CancelOrderByPairAndIdParams supported");
    }
  }

  @Override
  public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {

    final CurrencyPair currencyPair =
        Optional.ofNullable(params)
            .filter(p -> p instanceof TradeHistoryParamCurrencyPair)
            .map(p -> ((TradeHistoryParamCurrencyPair) p).getCurrencyPair())
            .orElse(null);
    try {
      return BithumbAdapters.adaptUserTrades(bithumbTransactions(currencyPair), currencyPair);
    } catch (BithumbException e) {
      throw BithumbErrorAdapter.adapt(e);
    }
  }

  @Override
  public TradeHistoryParams createTradeHistoryParams() {
    return new BithumbTradeHistoryParams();
  }

  @Override
  public OpenOrdersParams createOpenOrdersParams() {
    return new BithumbOpenOrdersParam();
  }
}

@@ -1872,32 +1872,44 @@
 s IOException %7B%0A
+    try %7B%0A  
     return place
@@ -1946,32 +1946,115 @@
 ).getOrderId();%0A
+    %7D catch (BithumbException e) %7B%0A      throw BithumbErrorAdapter.adapt(e);%0A    %7D%0A
   %7D%0A%0A  @Override
@@ -2126,24 +2126,36 @@
 Exception %7B%0A
+    try %7B%0A  
     return p
@@ -2198,24 +2198,107 @@
 tOrderId();%0A
+    %7D catch (BithumbException e) %7B%0A      throw BithumbErrorAdapter.adapt(e);%0A    %7D%0A
   %7D%0A%0A  @Over
@@ -2443,24 +2443,38 @@
 IdParams) %7B%0A
+      try %7B%0A  
       final 
@@ -2557,24 +2557,26 @@
 rams;%0A      
+  
 return cance
@@ -2647,24 +2647,113 @@
 ncyPair());%0A
+      %7D catch (BithumbException e) %7B%0A        throw BithumbErrorAdapter.adapt(e);%0A      %7D%0A
     %7D else %7B

add missed try/catch