package com.bonait.bnframework.HBL.DataUtil;




import com.bonait.bnframework.HBL.Enum.DataFormat;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public  class DataConvertLib {

    /***
     * 字节数组转换成浮点数
     * @param ，需要转换的字节
     * @param ，转换的数据格式
     * @return
     */
    public static Float BytesToFloat(byte[] buffers, DataFormat df) {
        if (buffers.length == 4) {
            byte[] bytes = new byte[4];
            if (df == DataFormat.ABCD) {
                bytes[0] = buffers[3];
                bytes[1] = buffers[2];
                bytes[2] = buffers[1];
                bytes[3] = buffers[0];
            } else if (df == DataFormat.CDAB) {
                bytes[0] = buffers[1];
                bytes[1] = buffers[0];
                bytes[2] = buffers[3];
                bytes[3] = buffers[2];
            } else if (df == DataFormat.BADC) {
                bytes[0] = buffers[2];
                bytes[1] = buffers[3];
                bytes[2] = buffers[0];
                bytes[3] = buffers[1];
            } else if (df == DataFormat.DCBA) {
                bytes[0] = buffers[0];
                bytes[1] = buffers[1];
                bytes[2] = buffers[2];
                bytes[3] = buffers[3];
            }
            return ByteBuffer.wrap(bytes).getFloat();
        }
        return 0.0f;
    }

    /**
     * 字节数组转换成整形
     * @param ，需要转换的字节数组
     * @param ，转换的数据格式
     * @return
     */
    public static   Integer BytesToInt(byte[] buffers, DataFormat df) {
        if (buffers.length == 4) {
            byte[] bytes = new byte[4];
            if (df == DataFormat.ABCD) {
                bytes[0] = buffers[3];
                bytes[1] = buffers[2];
                bytes[2] = buffers[1];
                bytes[3] = buffers[0];
            } else if (df == DataFormat.CDAB) {
                bytes[0] = buffers[1];
                bytes[1] = buffers[0];
                bytes[2] = buffers[3];
                bytes[3] = buffers[2];
            } else if (df == DataFormat.BADC) {
                bytes[0] = buffers[2];
                bytes[1] = buffers[3];
                bytes[2] = buffers[0];
                bytes[3] = buffers[1];
            } else if (df == DataFormat.DCBA) {
                bytes[0] = buffers[0];
                bytes[1] = buffers[1];
                bytes[2] = buffers[2];
                bytes[3] = buffers[3];
            }
            return ByteBuffer.wrap(bytes).getInt();
        }
        return 0;
    }

    /**
     * 32位整数转换成字节
     * @param ，number
     * @return
     */
    public static byte[] IntToByte(int number) {
        int temp = number;
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    /**
     * 32位整形转换成16位整数数组
     * @param ，value
     * @return
     */
    public static  short[] IntToShorts(int value) {
        short[] res = new short[2];
        int temp = value;
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        for (int i = 0; i < res.length; i++) {
            short s0 = (short) (b[i * 2] & 0xff);// 最低位
            short s1 = (short) (b[i * 2 + 1] & 0xff);
            s1 <<= 8;
            res[i] = (short) (s0 | s1);
        }
        return res;
    }

    public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException {
        short[] temp = new short[len];
        for (int i = 0; i < len; i++) {
            temp[i] = src[i + start];
        }
        byte[] bytesTemp = shorts2Bytes(temp);
        for (int i = 0; i < bytesTemp.length; i++) {
            byte b = bytesTemp[i];
        }
        String str = new String(bytesTemp, "UTF-8");
        return str;
    }

    public static short[] ByteToShort(byte[] bytes) {

        int len=bytes.length;
        if (bytes.length%2!=0) len++;
        byte[] tempBytes=new byte[len];
        short[] result = new short[len/2];
        for (int i=0;i<len;i++){
            if (i<bytes.length)tempBytes[i]=bytes[i];
        }
        for(int m=0;m<result.length;m++) {
            result[m] = (short) ((tempBytes[m * 2] & 0xff) | ((tempBytes[m * 2 + 1] & 0xff) << 8));
        }
        return result;
    }


    public static byte[] shorts2Bytes(short[] data) {
        byte[] byteValue = new byte[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            byteValue[i * 2] = (byte) (data[i] & 0xff);
            byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8);
        }
        return byteValue;
    }

    public static int GetAddress(String address) {
        if (address == null) return -1;
        if (address.length() > 0) {
            address = address.trim();
            if (address.toUpperCase().contains("M") && address.length() >= 4) {
                String[] res = address.substring(1).split("[.]");
                if (res != null && res.length == 2) {
                    try {
                        int firstAdd = Integer.parseInt(res[0]);
                        int endAdd = Integer.parseInt(res[1]);
                        if (endAdd >= 0 && endAdd <= 7) {
                            return (firstAdd * 8) + 320 + endAdd;
                        }
                    } catch (NumberFormatException e) {
                        return -1;
                    }
                }
            } else if (address.toUpperCase().contains("I") && address.length() >= 4) {
                String[] res = address.substring(1).split("[.]");
                if (res != null && res.length == 2) {
                    try {
                        int firstAdd = Integer.parseInt(res[0]);
                        int endAdd = Integer.parseInt(res[1]);
                        if (endAdd >= 0 && endAdd <= 7) {
                            return (firstAdd * 8) + endAdd;
                        }
                    } catch (NumberFormatException e) {
                        return -1;
                    }
                }
            } else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) {
                String res = address.substring(2);
                if (res != null) {
                    try {
                        int tempAdd = Integer.parseInt(res);
                        return (tempAdd / 2) + 100;
                    } catch (NumberFormatException e) {
                        return -1;
                    }
                }
            }else if (address.toUpperCase().contains("GI") &&address.length() >=3){
                String res = address.substring(2);
                if (res != null) {
                    try {
                        return Integer.parseInt(res);
                    } catch (NumberFormatException e) {
                        return -1;
                    }
                }
            }
            else {
                try {
                    return Integer.parseInt(address);
                } catch (NumberFormatException e) {
                    return -1;
                }
            }
        }
        return -1;
    }


    /**
     * 获取布尔位地址信息
     * 列：M2.5 = getBitSingleAdd("M",2,5);
     *
     * @param Prefix   地址标头
     * @param startAdd 起始地址编号
     * @param num      要获取的第几位数量
     * @return
     */
    public static String getBitSingleAdd(String Prefix, int startAdd, int num) {
        if (num > 0) {
            int FirstAdd = num / 8;
            int EndAdd = num % 8;
            if (EndAdd == 0) {
                FirstAdd--;
                EndAdd = 7;
            } else {
                EndAdd--;
            }
            return Prefix + FirstAdd + startAdd + "." + EndAdd;
        }
        return "";
    }

}
