package com.handset.serialportsensor.utils;

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

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;

public class Utils {
    private static final String path = "/sys/class/peijian_dev/function/PeijianPowerEn";
    private static final String path1 = "/sys/class/peijian_dev/function/pogo_wake";

    private final static String TAG = "Util";

    public static void initBLNStatus(char status) {
        setBLNStatus(status, path);
        setBLNStatus(status, path1);
    }

    private static void setBLNStatus(char status, String path) {
        File file = new File(path);
        FileOutputStream fos;
        try {
            if (!file.exists()) {
                return;
            }
            fos = new FileOutputStream(file);
            fos.write(status);
            fos.close();
        } catch (Exception e) {
            Log.e("setBLNStatus", "setBLNStatus error: ", e);
        }
    }

    //低位在前，高位在后
    public static byte[] intToByte(int num) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((num >> 24) & 0xff);
        bytes[1] = (byte) ((num >> 16) & 0xff);
        bytes[2] = (byte) ((num >> 8) & 0xff);
        bytes[3] = (byte) (num & 0xff);
        return bytes;
    }

    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data, 0, data1.length);
        System.arraycopy(data2, 0, data, data1.length, data2.length);
        return data;
    }

    public static byte[] addCheckBytes(byte[] byteCommand, int data) {
        byte[] bytes2 = Utils.intToByte(data);//data
        byte[] bytes3 = Utils.addBytes(byteCommand, bytes2);
        int crc1021Egg = CRC16Utils.getCRC1021(bytes3);
        byte[] bytes4 = Utils.intToByte(crc1021Egg);
        return Utils.addBytes(bytes3, bytes4);
    }

    //根据ID获取厂家编号和厂家内部ID
    public static int[] getNumber(int id) {
        //id 厂家ID (number)*0x3000+(interId)
        int b = 0x3000;
        int low = 0x0041;
        int high = 0x2fff;
        int interId = id % b;//厂家内部ID(0x0041-0x2fff)  0x6042
        int number = id / b;//厂家编号(0-4)
        if (interId >= low && interId <= high) {
            if (number >= 0 && number <= 4) {
                return new int[]{number, interId};
            }
        }
        return null;
    }

    //低位在前，高位在后
    public static byte[] convertStringToHex(String str) {
        char[] ch = str.toCharArray();
        byte[] hex = new byte[ch.length];
        for (int i = 0; i < ch.length; i++) {
            hex[i] = (byte) ch[i];
        }
        return hex;
    }

    public static byte[] convertStringToHexDesc(String str) {
        char[] ch = str.toCharArray();
        byte[] hex = new byte[ch.length];
        for (int i = 0; i < ch.length; i++) {
            hex[i] = (byte) ch[ch.length - i - 1];
        }
        return hex;
    }

    public static String convertHexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);
            temp.append(decimal);
        }

        return sb.toString();
    }

    public static int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static byte[] getCrcByte(byte[] command, String data) {
        byte[] hex = Utils.convertStringToHex(data);
        byte[] bytes2 = Utils.addBytes(command, hex);
        int crc1021 = CRC16Utils.getCRC1021(bytes2);
        byte low = (byte) (crc1021 & 0xff);
        byte high = (byte) ((crc1021 >> 8) & 0xff);
        byte[] getBaudBuffer = new byte[bytes2.length + 4];
        getBaudBuffer[0] = (byte) 0xDD;
        getBaudBuffer[1] = (byte) 0xDD;
        for (int i = 0; i < bytes2.length; i++) {
            getBaudBuffer[i + 2] = bytes2[i];
        }
        getBaudBuffer[getBaudBuffer.length - 2] = low;
        getBaudBuffer[getBaudBuffer.length - 1] = high;
        return getBaudBuffer;
    }

    public static byte[] getCrcByte(byte[] command) {
        int crc1021 = CRC16Utils.getCRC1021(command);
        byte low = (byte) (crc1021 & 0xff);
        byte high = (byte) ((crc1021 >> 8) & 0xff);
        byte[] getBaudBuffer = new byte[command.length + 4];
        getBaudBuffer[0] = (byte) 0xDD;
        getBaudBuffer[1] = (byte) 0xDD;
        for (int i = 0; i < command.length; i++) {
            getBaudBuffer[i + 2] = command[i];
        }
        getBaudBuffer[getBaudBuffer.length - 2] = low;
        getBaudBuffer[getBaudBuffer.length - 1] = high;
        return getBaudBuffer;
    }

    public static String byteArrayToString(byte[] b) {
        StringBuilder hexBuilder = new StringBuilder();
        for (byte aB : b) {
            String hexString = Integer.toHexString(aB & 0xFF);
            if (hexString.length() < 2) {
                hexBuilder.append(0);
            }
            hexBuilder.append(hexString);
        }
        BigInteger bigInteger = new BigInteger(hexBuilder.toString(), 16);
        String s = bigInteger.toString(10);
        return s;
    }

    public final static int getInt(byte[] buf, boolean asc, int len) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (len > 4) {
            throw new IllegalArgumentException("byte array size > 4 !");
        }
        int r = 0;
        if (asc)
            for (int i = len - 1; i >= 0; i--) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        else
            for (int i = 0; i < len; i++) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        return r;

    }

    public final static int byteArray2Int(byte[] buf) {
        return getInt(buf, false, 2);
    }


    public static  String getSensorNoShow(String raw) {
        if(raw.length() < 8) {
            return raw;
        }
        StringBuilder sb = new StringBuilder(raw);
        sb.deleteCharAt(4);
        return sb.toString();
    }
}
