package com.tsi.app.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class SysUtils {
    public static int toInt(String paramVal, int defVal) {
        int result = defVal;

        try {
            result = Integer.parseInt(paramVal);
        } catch (Exception var4) {
        }

        return result;
    }

    public static float toFloat(String paramVal, float defVal) {
        float result = defVal;

        try {
            result = Float.parseFloat(paramVal);
        } catch (Exception var4) {
        }

        return result;
    }

    public static double toDouble(String paramVal, double defVal) {
        double result = defVal;

        try {
            result = Double.parseDouble(paramVal);
        } catch (Exception var6) {
        }

        return result;
    }

    public static String getSysTime() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
        Date dtLog = new Date();
        return sdfDate.format(dtLog);
    }

    public static String getSysTimeStr() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dtLog = new Date();
        return sdfDate.format(dtLog);
    }

    public static int gapTime(String startTm, String endTm, int calcType) {
        if (startTm != null && !startTm.equals("") && startTm.length() == 14) {
            if (endTm != null && !endTm.equals("") && endTm.length() == 14) {
                Date startDateTime = TimeUtils.stringToDate(startTm);
                Date endDateTime = TimeUtils.stringToDate(endTm);
                if (startDateTime != null && endDateTime != null) {
                    GregorianCalendar gcStartDateTime = new GregorianCalendar();
                    GregorianCalendar gcEndDateTime = new GregorianCalendar();
                    gcStartDateTime.setTime(startDateTime);
                    gcEndDateTime.setTime(endDateTime);
                    long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
                    long hour = gap / 1000L / 60L / 60L;
                    long min = gap / 1000L / 60L;
                    long second = gap / 1000L;
                    if (10 == calcType) {
                        return (int)hour;
                    } else if (12 == calcType) {
                        return (int)min;
                    } else {
                        return 13 == calcType ? (int)second : -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

    public static String byteArrayToString(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length);

        for(int ii = 0; ii < data.length && data[ii] >= 33 && data[ii] <= 126; ++ii) {
            sb.append((char)data[ii]);
        }

        return sb.toString();
    }

    public static String byteArrayToHex(byte[] AData) {
        if (AData != null && AData.length != 0) {
            int ALen = AData.length;
            int line = ALen / 16;
            StringBuffer sb = new StringBuffer(ALen * 3 + line);
            sb.append("\r\n");

            for(int ii = 0; ii < ALen; ii += 16) {
                for(int jj = 0; jj < 16; ++jj) {
                    int pos = ii + jj;
                    if (pos >= ALen) {
                        break;
                    }

                    String hexNumber = "0" + Integer.toHexString(255 & AData[pos]).toUpperCase();
                    sb.append(hexNumber.substring(hexNumber.length() - 2));
                    sb.append(" ");
                }

                sb.append("\r\n");
            }

            return sb.toString();
        } else {
            return "";
        }
    }

    public static int getBitValue(byte b, int pos) {
        int val = b >> pos & 1;
        return val;
    }

    public static long ipToLong(String ipAddress) {
        String[] ipAddressInArray = ipAddress.split("\\.");
        long result = 0L;

        for(int i = 0; i < ipAddressInArray.length; ++i) {
            int power = 3 - i;
            int ip = Integer.parseInt(ipAddressInArray[i]);
            result = (long)((double)result + (double)ip * Math.pow(256.0D, (double)power));
        }

        return result;
    }

    public static long ipToLong2(String ipAddress) {
        long result = 0L;
        String[] ipAddressInArray = ipAddress.split("\\.");

        for(int i = 3; i >= 0; --i) {
            long ip = Long.parseLong(ipAddressInArray[3 - i]);
            result |= ip << i * 8;
        }

        return result;
    }

    public static String longToIp(long i) {
        return (i >> 24 & 255L) + "." + (i >> 16 & 255L) + "." + (i >> 8 & 255L) + "." + (i & 255L);
    }

    public static String longToIp2(long ip) {
        StringBuilder sb = new StringBuilder(15);

        for(int i = 0; i < 4; ++i) {
            sb.insert(0, Long.toString(ip & 255L));
            if (i < 3) {
                sb.insert(0, '.');
            }

            ip >>= 8;
        }

        return sb.toString();
    }
}
