package io.synor.dex;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * Order book sub-client
 */
public class OrderBookClient {
    private final SynorDex dex;

    OrderBookClient(SynorDex dex) {
        this.dex = dex;
    }

    public CompletableFuture<OrderBook> getOrderBook(String market, int depth) {
        return dex.get("/orderbook/" + market + "?depth=" + depth, OrderBook.class);
    }

    public CompletableFuture<Order> placeLimitOrder(LimitOrderParams params) {
        Map<String, Object> body = new HashMap<>();
        body.put("market", params.getMarket());
        body.put("side", params.getSide());
        body.put("price", params.getPrice());
        body.put("size", params.getSize().toString());
        body.put("time_in_force", params.getTimeInForce().name());
        body.put("post_only", params.isPostOnly());
        return dex.post("/orderbook/orders", body, Order.class);
    }

    public CompletableFuture<Void> cancelOrder(String orderId) {
        return dex.delete("/orderbook/orders/" + orderId, Void.class);
    }

    public CompletableFuture<List<Order>> getOpenOrders(String market) {
        String path = market != null ? "/orderbook/orders?market=" + market : "/orderbook/orders";
        return dex.getList(path, Order.class);
    }

    public CompletableFuture<List<Order>> getOrderHistory(int limit) {
        return dex.getList("/orderbook/orders/history?limit=" + limit, Order.class);
    }
}
