package com.binance.connector.client.impl;

import com.binance.connector.client.WebSocketStreamClient;
import com.binance.connector.client.enums.DefaultUrls;
import com.binance.connector.client.exceptions.BinanceConnectorException;
import com.binance.connector.client.utils.ParameterChecker;
import com.binance.connector.client.utils.RequestBuilder;
import com.binance.connector.client.utils.UrlBuilder;
import com.binance.connector.client.utils.WebSocketConnection;
import com.binance.connector.client.utils.httpclient.WebSocketStreamHttpClientSingleton;
import com.binance.connector.client.utils.websocketcallback.WebSocketClosedCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketClosingCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketFailureCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketMessageCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketOpenCallback;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/impl/WebSocketStreamClientImpl.class */
public class WebSocketStreamClientImpl implements WebSocketStreamClient {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketStreamClientImpl.class);
    private static final OkHttpClient client = WebSocketStreamHttpClientSingleton.getHttpClient();
    private final String baseUrl;
    private final Map<Integer, WebSocketConnection> connections;
    private final WebSocketOpenCallback noopOpenCallback;
    private final WebSocketClosingCallback noopClosingCallback;
    private final WebSocketClosedCallback noopClosedCallback;
    private final WebSocketFailureCallback noopFailureCallback;

    public WebSocketStreamClientImpl() {
        this.connections = new HashMap();
        this.noopOpenCallback = response -> {
        };
        this.noopClosingCallback = (code, reason) -> {
        };
        this.noopClosedCallback = (code, reason) -> {
        };
        this.noopFailureCallback = (throwable, response) -> {
        };
        this.baseUrl = DefaultUrls.WS_URL;
    }

    public WebSocketStreamClientImpl(String baseUrl) {
        this.connections = new HashMap();
        this.noopOpenCallback = response -> {
        };
        this.noopClosingCallback =( code, reason) -> {
        };
        this.noopClosedCallback = (code, reason) -> {
        };
        this.noopFailureCallback = (throwable, response) -> {
        };
        this.baseUrl = baseUrl;
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int aggTradeStream(String symbol, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return aggTradeStream(symbol.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int aggTradeStream(String symbol, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@aggTrade", this.baseUrl, symbol.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int tradeStream(String symbol, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return tradeStream(symbol.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int tradeStream(String symbol, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@trade", this.baseUrl, symbol.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int klineStream(String symbol, String interval, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return klineStream(symbol.toLowerCase(), interval, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int klineStream(String symbol, String interval, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@kline_%s", this.baseUrl, symbol.toLowerCase(), interval)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int miniTickerStream(String symbol, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return miniTickerStream(symbol.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int miniTickerStream(String symbol, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@miniTicker", this.baseUrl, symbol.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allMiniTickerStream(WebSocketMessageCallback callback) {
        return allMiniTickerStream(this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allMiniTickerStream(WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/!miniTicker@arr", this.baseUrl)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int symbolTicker(String symbol, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return symbolTicker(symbol.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int symbolTicker(String symbol, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@ticker", this.baseUrl, symbol.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allTickerStream(WebSocketMessageCallback callback) {
        return allTickerStream(this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allTickerStream(WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/!ticker@arr", this.baseUrl)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int rollingWindowTicker(String symbol, String windowSize, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        ParameterChecker.checkParameterType(symbol, String.class, "windowSize");
        if (new ArrayList<String>() { // from class: com.binance.connector.client.impl.WebSocketStreamClientImpl.1
            {
                add("1h");
                add("4h");
            }
        }.contains(windowSize)) {
            return rollingWindowTicker(symbol.toLowerCase(), windowSize, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
        }
        throw new BinanceConnectorException(String.format("\"%s\" is not a valid window size.", windowSize));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int rollingWindowTicker(String symbol, String windowSize, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        ParameterChecker.checkParameterType(symbol, String.class, "windowSize");
        if (new ArrayList<String>() { // from class: com.binance.connector.client.impl.WebSocketStreamClientImpl.2
            {
                add("1h");
                add("4h");
            }
        }.contains(windowSize)) {
            return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@ticker_%s", this.baseUrl, symbol.toLowerCase(), windowSize)));
        }
        throw new BinanceConnectorException(String.format("\"%s\" is not a valid window size.", windowSize));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allRollingWindowTicker(String windowSize, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(windowSize, String.class, "windowSize");
        if (new ArrayList<String>() { // from class: com.binance.connector.client.impl.WebSocketStreamClientImpl.3
            {
                add("1h");
                add("4h");
            }
        }.contains(windowSize.toLowerCase())) {
            return allRollingWindowTicker(windowSize.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
        }
        throw new BinanceConnectorException(String.format("\"%s\" is not a valid window size.", windowSize.toLowerCase()));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allRollingWindowTicker(String windowSize, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(windowSize, String.class, "windowSize");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/!ticker_%s@arr", this.baseUrl, windowSize.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int bookTicker(String symbol, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return bookTicker(symbol.toLowerCase(), this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int bookTicker(String symbol, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@bookTicker", this.baseUrl, symbol.toLowerCase())));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allBookTickerStream(WebSocketMessageCallback callback) {
        return allBookTickerStream(this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int allBookTickerStream(WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/!bookTicker", this.baseUrl)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int partialDepthStream(String symbol, int levels, int speed, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return partialDepthStream(symbol.toLowerCase(), levels, speed, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int partialDepthStream(String symbol, int levels, int speed, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@depth%s@%sms", this.baseUrl, symbol.toLowerCase(), Integer.valueOf(levels), Integer.valueOf(speed))));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int diffDepthStream(String symbol, int speed, WebSocketMessageCallback callback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return diffDepthStream(symbol.toLowerCase(), speed, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int diffDepthStream(String symbol, int speed, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        ParameterChecker.checkParameterType(symbol, String.class, "symbol");
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s@depth@%sms", this.baseUrl, symbol.toLowerCase(), Integer.valueOf(speed))));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int listenUserStream(String listenKey, WebSocketMessageCallback callback) {
        return listenUserStream(listenKey, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int listenUserStream(String listenKey, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(String.format("%s/ws/%s", this.baseUrl, listenKey)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int combineStreams(ArrayList<String> streams, WebSocketMessageCallback callback) {
        return combineStreams(streams, this.noopOpenCallback, callback, this.noopClosingCallback, this.noopClosedCallback, this.noopFailureCallback);
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public int combineStreams(ArrayList<String> streams, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback) {
        return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, RequestBuilder.buildWebSocketRequest(UrlBuilder.buildStreamUrl(this.baseUrl, streams)));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public void closeConnection(int connectionId) {
        if (this.connections.containsKey(Integer.valueOf(connectionId))) {
            this.connections.get(Integer.valueOf(connectionId)).close();
            logger.info("Closing Connection ID {}", Integer.valueOf(connectionId));
            this.connections.remove(Integer.valueOf(connectionId));
            return;
        }
        logger.info("Connection ID {} does not exist!", Integer.valueOf(connectionId));
    }

    @Override // com.binance.connector.client.WebSocketStreamClient
    public void closeAllConnections() {
        if (!this.connections.isEmpty()) {
            logger.info("Closing {} connections(s)", Integer.valueOf(this.connections.size()));
            Iterator<Map.Entry<Integer, WebSocketConnection>> iter = this.connections.entrySet().iterator();
            while (iter.hasNext()) {
                iter.next().getValue().close();
                iter.remove();
            }
        }
        if (this.connections.isEmpty()) {
            client.dispatcher().executorService().shutdown();
            logger.info("All connections are closed!");
        }
    }

    private int createConnection(WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback, Request request) {
        WebSocketConnection connection = new WebSocketConnection(onOpenCallback, onMessageCallback, onClosingCallback, onClosedCallback, onFailureCallback, request, client);
        connection.connect();
        int connectionId = connection.getConnectionId();
        this.connections.put(Integer.valueOf(connectionId), connection);
        return connectionId;
    }
}
