package com.zd.laboratory.socket.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.SimpleDateFormat;

/**
 * @author liubo
 */
public class SocketUtils {

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static byte[] str2byte(String str) {
        str = str.replaceAll(" ", "");
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String s = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(s, 16);
        }
        return bytes;
    }

    public static String formatBytes(byte[] bytes, String... insert) {
        StringBuilder buffer = new StringBuilder();
        for (byte aByte : bytes) {
            buffer.append(String.format("%02X", aByte));
            for (String anInsert : insert) {
                buffer.append(anInsert);
            }
        }
        return buffer.toString();
    }

    /**
     * 根据给定的字符串生成发送的十六进制报文字符串
     *
     * @param source
     * @return
     */
    public static String getHexString(String source) {
        if (source == null || source.length() == 0)
            return "";
        byte[] sources = source.getBytes();
        int length = sources.length;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append(getHexString((int) sources[i], 2));
            sb.append(" ");
        }
        return sb.toString();
    }

    public static String getHexString(int value, int count) {
        String result = Integer.toHexString(value);
        int length = count - result.length();
        if (length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                sb.append("0");
            }
            result = sb.toString() + result;
        }
        return result.substring(0, count).toUpperCase();
    }

    public static String getHexString(long value, int count) {
        String result = Long.toHexString(value);
        int length = count - result.length();
        if (length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                sb.append("0");
            }
            result = sb.toString() + result;
        }
        return result.substring(0, count).toUpperCase();
    }

    /**
     * CRC16校验算法
     *
     * @param bytes 字节数组
     * @return
     */
    public static int crc16(byte[] bytes, int length) {
        int crc = 0xffff;
        int dxs = 0xa001;
        int hibyte;
        int sbit;
        for (int i = 0; i < length; i++) {
            hibyte = crc >> 8;
            crc = hibyte ^ (bytes[i] & 0xff);
            for (int j = 0; j < 8; j++) {
                sbit = crc & 0x0001;
                crc = crc >> 1;
                if (sbit == 1)
                    crc ^= dxs;
            }
        }
        return crc & 0xffff;
    }

    public static int getHex2Int(String s) {
        int result = 0;
        try {
            result = Integer.parseInt(s, 16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getHex2Sting(String s, int count) {
        StringBuilder result = new StringBuilder(String.valueOf(getHex2Int(s)));
        int len = count - result.length();
        for (int i = 0; i < len; i++) {
            result.insert(0, "0");
        }
        return result.toString();
    }

    public static String getTimeFormatDay(long time) {
        String date = "";
        int day = (int) (time / 1000 / 60 / 60 / 24);
        if (day > 0) {
            date = day + "天";
        }
        int hour = (int) (time / 1000 / 60 / 60 % 24);
        if (hour > 0) {
            date += hour + "小时";
        }
        int minutes = (int) (time / 1000 / 60 % 60);
        if (minutes > 0) {
            date += minutes + "分";
        }
        int seconds = (int) (time / 1000 % 60);
        if (date.length() == 0 || seconds > 0) {
            date += seconds + "秒";
        }
        return date;
    }

    public static String getHexBit(String bitHex, int count) {
        String bit = "";
        try {
            bit = Integer.toBinaryString(Integer.parseInt(bitHex, 16));
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < count; i++) {
            s.append("0");
        }
        return s + bit;
    }

    public static void setHitText(final JTextField field, final String hintText) {

        field.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                String temp = field.getText();
                if (temp.equals(hintText)) {
                    field.setText("");
                    field.setForeground(Color.BLACK);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                String temp = field.getText();
                if (temp.equals("")) {
                    field.setForeground(Color.GRAY);
                    field.setText(hintText);
                }
            }
        });
    }


}
