package com.mogo.cloud.socket.third;

import static com.mogo.cloud.socket.SocketServicesConstants.SOCKET_CHANNEL_ID;
import static com.mogo.cloud.socket.SocketServicesConstants.getEnvironment;
import static com.mogo.cloud.socket.SocketServicesConstants.getTag;
import static com.zhidao.socket.location.MLocation.GCJ02;

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

import androidx.annotation.NonNull;

import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.mogo.cloud.socket.SocketBuildConfig;
import com.mogo.cloud.socket.SocketManager;
import com.mogo.cloud.socket.SocketServicesConstants;
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
import com.zhidao.socket.Callback;
import com.zhidao.socket.CallbackManager;
import com.zhidao.socket.ConnectionLifecycleListener;
import com.zhidao.socket.ErrorCallback;
import com.zhidao.socket.SocketClient;
import com.zhidao.socket.SocketConfig;
import com.zhidao.socket.location.MLocation;

public class ThirdSocketManager implements Callback, ErrorCallback {

    private static volatile ThirdSocketManager mInstance;
    private final MoGoAiCloudClientConfig cloudClientConfig;

    private ThirdSocketManager() {
        CallbackManager.getInstance().register(this);
        cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
    }

    public static ThirdSocketManager getInstance() {
        if (mInstance == null) {
            synchronized (ThirdSocketManager.class) {
                if (mInstance == null) {
                    mInstance = new ThirdSocketManager();
                }
            }
        }
        return mInstance;
    }

    public void init(Context context, String sn, double lat, double lon) {
        MLocation mLocation = new MLocation();
        mLocation.setCoordinate(GCJ02);
        mLocation.setLatitude(lat);
        mLocation.setLongitude(lon);
        String tempSn = cloudClientConfig.getSn();
        if (tempSn == null || tempSn.isEmpty()) {
            tempSn = sn;
        }
        SocketConfig.instance()
                .setAppContext(context.getApplicationContext())
                .setEnvironment(getEnvironment())
                .setClient(MogoCommon.Client.car)
                .setChannelId(SocketServicesConstants.SOCKET_CHANNEL_ID)
                .setOpenAnalytics(true)
                .setSn(tempSn)
                .setToken(cloudClientConfig.getToken())
                .setSecretKey(cloudClientConfig.getSecretKey())
                .setAuthPubKey(cloudClientConfig.getAuthPubKey())
                .setDebug(cloudClientConfig.isShowDebugLog())
                .setCustomTechBaseUrl(cloudClientConfig.getSocketTechUrl())
                .setCustomBaseUrl(cloudClientConfig.getSocketBaseUrl())
                .setLocation(mLocation)
                .setHeartBeat(cloudClientConfig.getHeartBeatInterval());
        SocketClient.getInstance().registerSocketCallback(this);
        SocketClient.getInstance().registerErrorCallback(this);
        SocketClient.getInstance().start(context);
    }

    public void reConnect() {
        SocketClient.getInstance().stopAndRestart();
    }

    @Override
    public void update(@NonNull CallbackManager manager, @NonNull byte[] message, String appId, long msgId) {
        if (SocketBuildConfig.isPrintLog) {
            Log.d(getTag(), "ThirdSocketManager update message");
        }
        SocketManager.getInstance().update(message, msgId);
    }

    @Override
    public void onAck(@NonNull CallbackManager manager, @NonNull byte[] header, byte[] content) {
        if (SocketBuildConfig.isPrintLog) {
            Log.d(getTag(), "ThirdSocketManager update ack");
        }

        SocketManager.getInstance().onAck(header);
    }

    public void sendMsg(String appId,
                        byte[] payload,
                        int headerType,
                        boolean ack,
                        long msgId) {
        SocketClient.getInstance().sendData(SOCKET_CHANNEL_ID, MogoCommon.Product.mogoBussiness.getNumber(), payload, headerType, ack, msgId);
    }

    public synchronized void release() {
        SocketClient.getInstance().unregisterSocketCallback(this);
        SocketClient.getInstance().stop();
        mInstance = null;
    }

    public void registerSocketConnCallback(ConnectionLifecycleListener listener) {
        SocketClient.getInstance().registerSocketConnCallback(listener);
    }

    public void unregisterSocketConnCallback(ConnectionLifecycleListener listener) {
        SocketClient.getInstance().unregisterSocketConnCallback(listener);
    }

    @Override
    public void onError(int code, String msg) {
        SocketManager.getInstance().onError(code, msg);
    }
}
