package com.mogo.realtime.socket;

import static com.mogo.cloud.passport.MoGoAiCloudClientConfig.HTTP_DNS_ENV_DEMO;
import static com.mogo.cloud.socket.SocketMsgType.MSG_TYPE_DOWNLINK_CAR_DATA;
import static com.mogo.realtime.constant.RealTimeConstant.TAG;

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

import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.socket.IMogoCloudSocketOnMessageListener;
import com.mogo.cloud.socket.SocketManager;
import com.mogo.cloud.socket.entity.MsgBody;
import com.mogo.cloud.socket.entity.SocketDownData;
import com.mogo.realtime.constant.RealTimeConstant;
import com.zhidao.ptech.connsvr.protocol.MogoConnsvr;

import java.util.ArrayList;
import java.util.List;

/**
 * Socket长链 业务服务处理类
 */
public class SocketHandler {

    private static volatile SocketHandler mInstance;
    private static final int HEADER_TYPE = MogoConnsvr.MsgType.mogoMsgTypeCollectSvrNoRspReq.getNumber();


    private String mAppId;
    private final List<IMogoCloudOnMsgListener> onMsgListenerList = new ArrayList<>();

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

    /**
     * 初始化长连接服务
     *
     * @param context 上下文
     * @param appId   外部传入
     */
    public void initSocket(Context context, String appId, double lat, double lon) {
        mAppId = appId;
        SocketManager.getInstance().init(context, "", lat, lon);
        if (MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getNetMode() == HTTP_DNS_ENV_DEMO) {
            SocketManager.getInstance().registerOnMessageListener(RealTimeConstant.DEMO_FREQUENCY_CHANNEL_ID, onMessageListener);
        } else {
            SocketManager.getInstance().registerOnMessageListener(RealTimeConstant.HIGH_FREQUENCY_CHANNEL_ID, onMessageListener);
            SocketManager.getInstance().registerOnMessageListener(RealTimeConstant.LOW_FREQUENCY_CHANNEL_ID, onMessageListener);
        }
    }

    public void registerOnMsgListener(IMogoCloudOnMsgListener onMsgListener) {
        if (onMsgListener != null && !onMsgListenerList.contains(onMsgListener)) {
            onMsgListenerList.add(onMsgListener);
        }
    }

    public void unRegisterOnMsgListener(IMogoCloudOnMsgListener onMsgListener) {
        if (onMsgListener != null) {
            onMsgListenerList.remove(onMsgListener);
        }
    }

    private final IMogoCloudSocketOnMessageListener<SocketDownData.SocketDownDataProto> onMessageListener = new IMogoCloudSocketOnMessageListener<SocketDownData.SocketDownDataProto>() {
        @Override
        public Class<SocketDownData.SocketDownDataProto> target(int msgType) {
            return SocketDownData.SocketDownDataProto.class;
        }

        @Override
        public void onMsgReceived(int msgType, SocketDownData.SocketDownDataProto webSocketData) {
            if (webSocketData == null) {
                Log.e(TAG, "onMsgReceived  SocketDownDataProto3 == null ");
                return;
            }
            if (webSocketData.getMsgType() == MSG_TYPE_DOWNLINK_CAR_DATA.getMsgType()) {
                for (IMogoCloudOnMsgListener listener : onMsgListenerList) {
                    if (listener != null) {
                        listener.onMsgReceived(webSocketData.getData());
                    }
                }
            }
        }
    };

    /**
     * 发送自车和ADAS数据
     *
     * @param msgBody socket消息
     */
    public void sendMsg(MsgBody msgBody) {
        SocketManager.getInstance().sendMsg(mAppId, HEADER_TYPE, msgBody, msgId -> {
            for (IMogoCloudOnMsgListener listener : onMsgListenerList) {
                if (listener != null) {
                    listener.onMsgSend(msgId);
                }
            }
        });
    }

    public void stop() {
        if (MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getNetMode() == HTTP_DNS_ENV_DEMO) {
            SocketManager.getInstance().unregisterOnMessageListener(RealTimeConstant.DEMO_FREQUENCY_CHANNEL_ID, onMessageListener);
        } else {
            SocketManager.getInstance().unregisterOnMessageListener(RealTimeConstant.HIGH_FREQUENCY_CHANNEL_ID, onMessageListener);
            SocketManager.getInstance().unregisterOnMessageListener(RealTimeConstant.LOW_FREQUENCY_CHANNEL_ID, onMessageListener);
        }
        SocketManager.getInstance().release();
        onMsgListenerList.clear();
        mInstance = null;
    }

}
