package cn.reghao.devops.logstash.ws;

import cn.reghao.jutil.jdk.serializer.JdkSerializer;
import okhttp3.*;
import okio.ByteString;

import java.util.concurrent.TimeUnit;

/**
 * @author reghao
 * @date 2023-02-23 09:26:50
 */
public class WsClient {
    private final String url;
    private WebSocket webSocket;
    private boolean connected;
    private final WebSocketListener webSocketListener;
    private boolean retry;
    private int retryCount;

    public WsClient(String url) {
        this.url = url;
        this.webSocketListener = new WebSocketListenerImpl(this);
        this.retry = true;
        this.retryCount = 0;
    }

    public void setRetry(boolean retry) {
        this.retry = retry;
    }

    public boolean isRetry() {
        return retry;
    }

    public void retryCountIncr() {
        this.retryCount += 1;
    }

    public void resetRetryCount() {
        this.retryCount = 0;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void connect() {
        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", "Bearer ")
                .build();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();
        this.webSocket = okHttpClient.newWebSocket(request, webSocketListener);
    }

    public void setConnected(boolean status) {
        this.connected = status;
    }

    public boolean isConnected() {
        return connected;
    }

    public void send(String dest, Object message) {
        if (isConnected()) {
            byte[] bytes = JdkSerializer.serialize(message);
            webSocket.send(ByteString.of(bytes));
        }
    }

    public void close() {
        setRetry(false);
        webSocket.close(1000, "Client Close Connection");
    }
}
