package com.mosty.wzzx.udp;

import com.alibaba.fastjson.JSON;
import com.mosty.base.utils.DateUtils;
import lombok.Data;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 泸州udp转换
 *
 * @author esacpe
 */
@Data
public class UdpLzEntity {

    /**
     * pgisId
     */
    private String pgisId;
    /**
     * 经度，单位度
     */
    private Double lng;
    /**
     * 纬度，单位度
     */
    private Double lat;
    /**
     * 高程，单位米
     */
    private Integer altitude;
    /**
     * 速度，单位Km/h
     */
    private Integer speed;
    /**
     * 方向，0-359，正北为0，顺时针
     */
    private Integer direction;
    /**
     * 定位时间
     */
    private Date time;

    /**
     * 精度
     */
    private Integer precision;

    //解析
    public boolean parseLz(byte[] data) {
        return parseLz(data, data.length);
    }

    private int destPos = 0;

    public boolean parseLz(byte[] data, int dataLength) {
        if (data == null || dataLength < 10 || dataLength > data.length) {
            return false;
        }
        if (data[0] == 0xAA) {
            return false;
        }

        byte[] bytes3 = new byte[20];
//        System.arraycopy(data, 10, bytes3, 0, 20);
//
//        pgisId = new String(bytes3, Charset.forName("UTF-8"));
//         pgisId = new String(bytes3, Charset.forName("UTF-8"));
        destPos = 10;
        pgisId = (new String(arraycopy(data, destPos, 20,20), Charset.forName("UTF-8"))).trim();

//        byte[] jdbt=new byte[8];
//        System.arraycopy(data, 30, jdbt, 0, 8);
//         lng = bytes2Double(jdbt);

        lng = bytes2Double(arraycopy(data, destPos, 8,8));

//        byte[] wdbt=new byte[8];
//        System.arraycopy(data, 38, wdbt, 0, 8);
//         lat = bytes2Double(wdbt);

        lat = bytes2Double(arraycopy(data, destPos, 8,8));


//        byte[] dspeedbyte=new byte[8];
//        System.arraycopy(data, 46, dspeedbyte, 0, 2);
//        speed = byte2int(dspeedbyte);

        speed = byte2int(arraycopy(data, destPos, 2,4));


//        byte[] dirsbyte=new byte[8];
//        System.arraycopy(data, 48, dirsbyte, 0, 2);
//        direction = byte2int(dirsbyte);

        direction = byte2int(arraycopy(data, destPos, 2,4));

        altitude = byte2int(arraycopy(data, destPos, 2,4));

        precision = byte2int(arraycopy(data, destPos, 2,4));

        Integer year = byte2int(arraycopyYear(data, destPos, 2,4));
        Integer month = byte2int(arraycopy(data, destPos, 1,4));
        Integer day = byte2int(arraycopy(data, destPos, 1,4));
        Integer hours = byte2int(arraycopy(data, destPos, 1,4));
        Integer minutes = byte2int(arraycopy(data, destPos, 1,4));
        Integer seconds = byte2int(arraycopy(data, destPos, 1,4));
        try {
            time = DateUtils.stringToDate(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time != null;

    }

    /**
     * 加密有问题 把54 55单独替换位置
     * @param arr
     * @param dest
     * @param len
     * @param end
     * @return
     */
    private byte[] arraycopyYear(byte[] arr, int dest, int len,int end) {
        byte[] copy = new byte[end];
        System.arraycopy(arr, 55, copy, 0, 1);
        System.arraycopy(arr, 54, copy, 1, 1);
        destPos = dest + len;
        return copy;
    }


    private byte[] arraycopy(byte[] arr, int dest, int len,int end) {
        byte[] copy = new byte[end];
        System.arraycopy(arr, dest, copy, 0, len);
        destPos = dest + len;
        return copy;
    }


    private double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public int byte2int(byte[] res) {
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24);
        return targets;
    }

    public static byte[] hexStringToByte(String hexstr) {
        if (hexstr == null)
            return null;
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = hexstr.charAt(j++);
            char c1 = hexstr.charAt(j++);
            b[i] = ((byte) (charToInt(c0) << 4 | charToInt(c1)));
        }
        return b;
    }

    // char转换成int
    public static int charToInt(char c) {
        if (c >= 'a') {
            return c - 'a' + 10 & 0xF;
        }
        if (c >= 'A') {
            return c - 'A' + 10 & 0xF;
        }
        return c - '0' & 0xF;
    }
    public static String byteArrayToString(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        UdpLzEntity lzEntity = new UdpLzEntity();
        byte[] bytes2 = UdpLzEntity.hexStringToByte("AAAACCCC002200000033313334303430313837343700000000000000000016A6EF3504305A403BC43F6CE9BD3F400000000001E4000007E8040B100402");
        lzEntity.parseLz(bytes2);

        System.out.println(JSON.toJSONString(lzEntity));
    }
}
