package se.koc.trader.exchange.binance;

import se.koc.trader.api.ExchangeMarket;
import se.koc.trader.struct.Symbol;
import zutil.net.ws.rest.RESTClientFactory;
import zutil.parser.DataNode;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class BinanceMarket implements ExchangeMarket {

    private transient BinanceRestAPI rest;

    private transient List<String> symbolsCache;


    public BinanceMarket() {
        try {
            rest = RESTClientFactory.createClient(new URL("https://api.binance.com"), BinanceRestAPI.class);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }


    public long getServerTime() {
        return rest.getServerTime().getLong("serverTime");
    }

    @Override
    public List<String> getSymbols() {
        if (symbolsCache == null) {
            symbolsCache = new ArrayList<>();
            DataNode symbols = rest.getExchangeInfo().get("symbols");

            for (DataNode symbol : symbols) {
                symbolsCache.add(symbol.getString("symbol"));
            }
        }

        return symbolsCache;
    }

    @Override
    public Symbol getSymbol(String symbol) {
        return null; // /api/v3/ticker/price?symbol=xxx
    }
}
