package com.mogo.cloud.socket.internal;


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

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.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
import com.zhidao.socketsdk.manager.OnSocketAckCallback;
import com.zhidao.socketsdk.manager.OnSocketReceiveCallback;
import com.zhidao.socketsdk.manager.SocketConnManager;
import com.zhidao.socketsdk.manager.SocketConnManagerImpl;


public class InternalSocketManager implements OnSocketReceiveCallback, OnSocketAckCallback {

    private static volatile InternalSocketManager mInstance;
    private static final int MSG_PRODUCT_LINE = MogoCommon.Product.mogoBussiness_VALUE;
    private static final String TAG = "InternalSocketManager";

    private final MoGoAiCloudClientConfig cloudClientConfig;
    private SocketConnManager mSocketConnManager;

    private InternalSocketManager() {
        cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
    }

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

    public void init(Context context) {
        mSocketConnManager = SocketConnManagerImpl.getInstance(context);
        mSocketConnManager.addSocketMessageCallback(this);
        mSocketConnManager.addSocketAckCallback(this);
        String appId = cloudClientConfig.getServiceAppId();
        if (TextUtils.isEmpty(appId)) {
            if (SocketBuildConfig.isPrintLog) {
                Log.e(TAG, "需要初始化 Socket AppId");
            }

            return;
        }
        mSocketConnManager.init(appId);
    }

    @Override
    public void onAck(byte[] headerBytes, byte[] payload) {
        if (SocketBuildConfig.isPrintLog) {
            Log.d(TAG, "InternalSocketManager update ack");
        }
        SocketManager.getInstance().onAck(headerBytes);
    }

    @Override
    public void onMessageReceived(byte[] message) {
        if (SocketBuildConfig.isPrintLog) {
            Log.d(TAG, "InternalSocketManager update message");
        }

        SocketManager.getInstance().update(message, 0);
    }

    public void sendMsg(byte[] pb, int headerType, boolean isAck, long msgId) {
        if (mSocketConnManager.isConnected()) {
            if (SocketBuildConfig.isPrintLog) {
                Log.d(TAG, "isConnected.");
            }

            mSocketConnManager.sendPayload(MSG_PRODUCT_LINE, pb, headerType, isAck, msgId);
        } else {
            Log.e(TAG, "sendMsg error, connect is lost.");
        }
    }

    public void release() {
        if (mSocketConnManager != null) {
            mSocketConnManager.rmSocketMessageCallback(this);
            mSocketConnManager.rmSocketAckCallback(this);
            mSocketConnManager.onRelease();
            mSocketConnManager = null;
        }
        mInstance = null;
    }
}
