package com.handset.serialportsensor.utils;

import com.handset.serialportsensor.constant.Constant;

public class ProtoUtils {

    public static final int HEADER_BYTE_NUM = 2;

    public static final int ID_BYTE_NUM = 4;

    public static final int COMMAND_BYTE_NUM = 2;

    private static final int MAX_MANUFACTURER_NO = 2;

    private static final int MIN_MANUFACTURER_NO = 0;

    public static final int MAX_MANUFACTURER_INTERNAL_NO = 999;

    private static final int MIN_MANUFACTURER_INTERNAL_NO = 1;

    public static int getCommandValue(int[] values) {
        return (values[1] >> 8);
    }

    public static byte getIdHigh1(int[] values) {
        return (byte) ((values[0] >> 24) & 0xFF);
    }

    public static byte getIdHigh2(int[] values) {
        return (byte) ((values[0] >> 16) & 0xFF);
    }

    public static byte getIdHigh3(int[] values) {
        return (byte) ((values[0] >> 8) & 0xFF);
    }

    public static byte getIdHigh4(int[] values) {
        return (byte) (values[0] & 0xFF);
    }

    public static byte getIdHigh1(int low) {
        return (byte) ((low >> 24) & 0xFF);
    }

    public static byte getIdHigh2(int low) {
        return (byte) (low >> 16 & 0xFF);
    }

    public static byte getIdHigh3(int high) {
        return (byte) ((high >> 8) & 0xFF);
    }

    public static byte getIdHigh4(int high) {
        return (byte) (high & 0xFF);
    }

    public static byte[] getByte4Id(String id) {
        byte year = (byte) Integer.parseInt(id.substring(0, 2));
        byte batch = (byte) Integer.parseInt(id.substring(2, 4));
        Integer num = Integer.parseInt(id.substring(4, 8));
        byte numHigh = (byte) (num >> 8 & 0xFF);
        byte numLow = (byte) (num & 0xFF);
        byte[] last = {year, batch, numHigh, numLow};
        return last;
    }

    public static byte[] genSentCommand11Byte(byte command, byte idHigh1, byte idHigh2, byte idHigh3, byte idHigh4) {
        byte[] bytes1 = {idHigh1, idHigh2, idHigh3, idHigh4, 0x00, command, 0x00};
        int crc1021 = CRC16Utils.getCRC1021(bytes1);
        byte low = (byte) (crc1021 & 0xff);
        byte high = (byte) ((crc1021 >> 8) & 0xff);
        byte[] getIdBuffer = {(byte) 0xDD, (byte) 0xDD, idHigh1, idHigh2, idHigh3, idHigh4, 0x00, command, 0x00, low, high};
        return getIdBuffer;
    }

    public static byte[] genSentCommandSetId(byte idHigh1, byte idHigh2, byte idHigh3, byte idHigh4,
                                             byte nidHigh1, byte nidHigh2, byte nidHigh3, byte nidHigh4) {
        byte[] bytes1 = {idHigh1, idHigh2, idHigh3, idHigh4, 0x00, Constant.Command.COMMAND_50, 0x04, nidHigh1, nidHigh2, nidHigh3, nidHigh4};
        int crc1021 = CRC16Utils.getCRC1021(bytes1);
        byte low = (byte) (crc1021 & 0xff);
        byte high = (byte) ((crc1021 >> 8) & 0xff);
        byte[] getIdBuffer = {(byte) 0xDD, (byte) 0xDD, idHigh1, idHigh2, idHigh3, idHigh4, 0x00, Constant.Command.COMMAND_50, 0x04,
                nidHigh1, nidHigh2, nidHigh3, nidHigh4, low, high};
        return getIdBuffer;
    }

    public static int[] getNumber(byte idHigh1, byte idHigh2) {
        int id = (idHigh2 << 8) + idHigh1;
        int interId = id % 1000;//厂家内部ID(0x0041-0x2fff)  0x6042
        int number = id / 1000;//厂家编号(0-4)
        if (interId >= MIN_MANUFACTURER_INTERNAL_NO) {
            if (number >= MIN_MANUFACTURER_NO && number <= MAX_MANUFACTURER_NO) {
                return new int[]{number, interId};
            }
        }
        return null;
    }

    public static int[] getSensorNumber(int idHigh1, byte idHigh2, byte idHigh3, byte idHigh4) {
        int mId = Utils.byteArray2Int(new byte[]{idHigh3, idHigh4});
        int id = (idHigh1 * 100 + idHigh2) * 10000 + mId;
        int number = mId / 1000;//厂家编号(0-4)
        return new int[]{number, id};
    }
}
