package com.mogo.cloud.location.third.service;


import android.util.Log;

import com.elegant.utils.CheckUtils;
import com.google.protobuf.ByteString;
import com.mogo.cloud.location.third.core.LocUploadConfig;
import com.mogo.cloud.location.third.core.Location;
import com.mogo.cloud.location.third.core.LocationDelegateManager;
import com.mogo.cloud.location.third.core.LocationServiceProvider;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
import com.zhidao.ptech.connsvr.payload.protocol.MogoPayload;
import com.zhidao.ptech.connsvr.protocol.MogoConnsvr;
import com.zhidao.socket.SocketClient;

import java.util.List;

public class SocketRequestUtil {

    private final static String TAG = "SocketRequestUtil";

    public static void sendCoordinateReqBySocket(MogoPayload.MogoCoornateReq coordinateReq,
                                                 boolean ack,
                                                 long msgId) {
        byte[] data = buildSocketPayload(coordinateReq.toByteArray(), MogoPayload.PayloadMsgType.mogoCoordinateReq_VALUE);
        sendPayload(data, ack, msgId);
    }

    private static void sendPayload(byte[] payload, boolean ack, long msgId) {
        Log.d(TAG, "Socket---sendPayload");
        try {
            SocketClient.getInstance().sendData(LocUploadConfig.instance().getAppId(),
                    MogoCommon.Product.mogoBussiness_VALUE,
                    payload, MogoConnsvr.MsgType.mogoMsgTypeCdntSvrUpReq_VALUE, ack, msgId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 构建单个PB-坐标点
     */
    public static MogoPayload.Coordinate buildBCoordinate(Location location) {
        return MogoPayload.Coordinate.newBuilder()
                .setTimestamp(System.currentTimeMillis())
                .setGpsTimestamp(location.getTime())
                .setLat(location.getLatitude())
                .setLon(location.getLongitude())
                .setLockType(location.getLocType())
                .setAccuracy(location.getAccuracy())    //精度
                .setCarStatus(location.getCarStatus())  //车状态
                .setGpsAccuracyStatus(location.getGpsAccuracyStatus()) //精度
                .setSatellites(location.getSatellites()) //卫星数
                .setType(MogoPayload.CoordinateType.SOSO_GCJ_COORDINATE)
                .setGpsSpeed(location.getSpeed())  //
                .setDirection((int) location.getBearing()) //方向
                .setHeight((int) location.getAltitude()) //高度
                .build();
    }

    /**
     * 构建外层PB-文件
     */
    public static MogoPayload.MogoCoornateReq getCoordinateReq(List<MogoPayload.Coordinate> coordinates) {
        if (coordinates == null || coordinates.isEmpty()) {
            return null;
        }

        final LocationServiceProvider provider = LocationDelegateManager.getInstance().getLocationServiceProvider();

        final MogoPayload.MogoCoornateReq.Builder builder = MogoPayload.MogoCoornateReq.newBuilder()
                .setTimestamp(System.currentTimeMillis())
                .addAllCoordinate(coordinates)
                .setSn(MoGoAiCloudClientConfig.getInstance().getSn());

        if (provider != null && !CheckUtils.isEmpty(provider.getAdCode())) {
            builder.setAdCode(provider.getAdCode());
        }

        if (provider != null && !CheckUtils.isEmpty(provider.getCityCode())) {
            builder.setCityId(provider.getCityCode());
        }

        return builder.build();
    }

    //构建整个payload
    private static byte[] buildSocketPayload(byte[] payload, int payloadType) {
        MogoConnsvr.Payload payloadData = MogoConnsvr.Payload.newBuilder()
                .setMsgType(payloadType)
                .setPayload(ByteString.copyFrom(payload)).build();
        return payloadData.toByteArray();
    }
}
