package com.tomas.ckd_system;

import android.content.Context;
import android.util.Log;

import com.tomas.ckd_system.lib.common.CallbackListener;
import com.tomas.ckd_system.lib.common.MessageListener;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;

public class WsClient {

    private static Context context;
    private String url;

    private static Request request;
    private static WsClientListener wsClientListener;
    public static WebSocket ws;
    private static OkHttpClient client;

    public static int client_id;

    public static CallbackListener callbackListener;
    public static MessageListener messageListener;

    public static STATUS status;
    public enum STATUS {
        IDLE,
        ACCEPT_ACK,
        ACCEPTED,
        JOIN_ACK,
        JOINED,
        WORKING
    }

    // Singleton class
    private static WsClient wsClient = null;

    // prevent default constructor call
    private WsClient() {}

    private WsClient(CallbackListener callbackListener, MessageListener messageListener, Context context, String url) {
        this.callbackListener = callbackListener;
        this.messageListener = messageListener;
        this.context = context;
        this.url = url;

        this.client = new OkHttpClient();
    }

    public static final WsClient getInstance() {
        return wsClient;
    }

    public void setOnMessageListener(MessageListener messageListener) {
        Log.d(context.getString(R.string.tag), "setOnMessageListener to " + messageListener.getClass());
        this.messageListener = messageListener;
    }

    public static final WsClient getInstance(CallbackListener callbackListener, MessageListener messageListener, Context context, String url) {
        if (wsClient == null) {
            wsClient = new WsClient(callbackListener, messageListener, context, url);
        }

        // start listener server
        wsClient.connect();

        return wsClient;
    }

    public void connect() {
        if (ws == null) {
            Log.d(context.getString(R.string.tag), "Web socket connect to " + url);
            request = new Request.Builder()
                    .url(url)
                    .build();
            wsClientListener = new WsClientListener(this.context);
            ws = client.newWebSocket(request, wsClientListener);

            // update info icons
            callbackListener.onFinished("connected");
        }
    }

    public void disconnect() {

        if(ws == null){ return; }

        ws.close(1000, "{\"reason\": \"With love\"}");
        ws= null;

        //client.dispatcher().executorService().shutdown();
        //client.connectionPool().evictAll();

        wsClientListener = null;
    }

    public boolean isConnected() {
        return ws!=null;
    }

    public void sendMessage(String str) throws Exception {
        if (!isConnected()) throw new Exception("The server might crash");

        Log.d(context.getString(R.string.tag), "Send message : " + str);

        ws.send(str);
    }

}
