package com.ai.ced.bluetooth;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by zhaoli on 2018/5/25.
 */

public class Util {

    public  static int deviceType_other = 0; //设备类型-未识别的设备
    public  static int deviceType_kaer_KT8000 = 1; //设备类型-卡尔KT8000
    public  static int deviceType_senri = 2; //设备类型-森锐
    public  static int deviceType_sensor = 3; //设备类型-神思
    public  static int deviceType_hhd = 4; //设备类型-恒鸿达(三元达)
    public  static int deviceType_kaer_KT8003 = 5; //设备类型-卡尔KT8003
    /**
     * 恒鸿达-工具函数-域名翻译成ip
     * @param host
     * @return
     */
    public static String GetInetAddress(String host){
        String IPAddress = "";
        InetAddress ReturnStr1 = null;
        try {
            ReturnStr1 = InetAddress.getByName(host);
            IPAddress = ReturnStr1.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return  IPAddress;
        }
        return IPAddress;
    }
    public static String bytesToASCString(byte[] src) {
        int len = src.length;

        if (src == null || len <= 0) {
            return null;
        }
        char[] result = new char[len];
        for (int i = 0; i < len; i++) {
            result[i] = (char) src[i];
        }

        return String.valueOf(result);
    }
    public static  byte[] hexStringToByte(String hex_str){

        byte[] data_byte = new byte[hex_str.length()/2];
        int byte_len = 0;
        for (int i = 0;i < hex_str.length();i += 2){
            data_byte[byte_len ++] = HexToByte(hex_str.charAt(i),hex_str.charAt(i+1));
        }
        return data_byte;
    }
    public static  byte HexToByte(char hex1, char hex2)
    {
        byte result = 0;
        for(int i = 0;i < 2;i++)
        {
            char c;
            if (i == 0)
                c = hex1;
            else
                c = hex2;
            byte b = 0;
            switch (c)
            {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    b = (byte)(c - '0');
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                case 'E':
                case 'F':
                    b = (byte)(10 + c - 'A');
                    break;
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                    b = (byte)(10 + c - 'a');
                    break;
            }
            if (i == 0)
            {
                b = (byte)(b * 16);
            }
            result += b;
        }
        return result;
    }
    /**
     * 根据民族编码转换成名称
     * @param nationCode
     * @return
     */
    public static String nationNameFromCode(int nationCode){
        switch (nationCode) {
            case 1:
                return "汉族";
            case 2:
                return "蒙古族";
            case 3:
                return "回族";
            case 4:
                return "藏族";
            case 5:
                return "维吾尔族";
            case 6:
                return "苗族";
            case 7:
                return "彝族";
            case 8:
                return "壮族";
            case 9:
                return "布依族";
            case 10:
                return "朝鲜族";
            case 11:
                return "满族";
            case 12:
                return "侗族";
            case 13:
                return "瑶族";
            case 14:
                return "白族";
            case 15:
                return "土家族";
            case 16:
                return "哈尼族";
            case 17:
                return "哈萨克族";
            case 18:
                return "傣族";
            case 19:
                return "黎族";
            case 20:
                return "傈僳族";
            case 21:
                return "佤族";
            case 22:
                return "畲族";
            case 23:
                return "高山族";
            case 24:
                return "拉祜族";
            case 25:
                return "水族";
            case 26:
                return "东乡族";
            case 27:
                return "纳西族";
            case 28:
                return "景颇族";
            case 29:
                return "柯尔克孜族";
            case 30:
                return "土族";
            case 31:
                return "达斡尔族";
            case 32:
                return "仫佬族";
            case 33:
                return "羌族";
            case 34:
                return "布朗族";
            case 35:
                return "撒拉族";
            case 36:
                return "毛难族";
            case 37:
                return "仡佬族";
            case 38:
                return "锡伯族";
            case 39:
                return "阿昌族";
            case 40:
                return "普米族";
            case 41:
                return "塔吉克族";
            case 42:
                return "怒族";
            case 43:
                return "乌孜别克族";
            case 44:
                return "俄罗斯族";
            case 45:
                return "鄂温克族";
            case 46:
                return "崩龙族";
            case 47:
                return "保安族";
            case 48:
                return "裕固族";
            case 49:
                return "京族";
            case 50:
                return "塔塔尔族";
            case 51:
                return "独龙族";
            case 52:
                return "鄂伦春族";
            case 53:
                return "赫哲族";
            case 54:
                return "门巴族";
            case 55:
                return "珞巴族";
            case 56:
                return "基诺族";
            default:
                return "其他";
        }
    }

    /**
     * bitmap转为base64
     * @param bitmap
     * @return
     */
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        result = result.replace("\n", "");
        result = result.replace("\r", "");
        return result;
    }
    public static  void saveBlueToothadress(Context context, String adress, String name) {
        SharedPreferences savePreferences = context.getSharedPreferences(
                "Bluetoothadress", 0);
        SharedPreferences.Editor editor = savePreferences.edit();
        if (adress != null && !adress.equals("")) {
            editor.putString("adress", adress);
        }
        if (name != null && !name.equals("")) {
            editor.putString("name", name);
        }
        editor.commit();
    }

    public static String[] getBlueToothadress(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                "Bluetoothadress", Activity.MODE_PRIVATE);
         String arr[]=new String[2];
        String adress = sharedPreferences.getString("adress", "");
      String name = sharedPreferences.getString("name", "");
      if (adress != null && !adress.equals(""))
      {
        arr[0]=adress;
      }
      if (name != null && !name.equals(""))
      {
        arr[1]=name;
      }
        return arr;
    }

    public  static  int deviceTypeFromName(String name) {

        if (name == null || name.equals("")) {
            return deviceType_other;
        } else if (TextUtils.indexOf(name, "KT8000") == 0) {
            return deviceType_kaer_KT8000;
        } else if (TextUtils.indexOf(name, "KT8003") == 0) {
            return deviceType_kaer_KT8003;
        } else if (TextUtils.indexOf(name, "SR") == 0) {
            return deviceType_senri;
        } else if (TextUtils.indexOf(name, "SS") == 0) {
            return deviceType_sensor;
        } else if (TextUtils.indexOf(name, "HOD") == 0) {
            return deviceType_hhd;
        } else {
            return deviceType_other;
        }
    }


    /**
     * 卡尔-KT8003-工具函数，密码字符串md5加密，登录用
     *
     * @param source
     * @return
     */
    public  static String getMD5(byte[] source) {
        String s = null;
        char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                'e', 'f'};
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            md.update(source);
            byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数，
            // 用字节表示就是 16 个字节
            char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话，使用两个字符，
            // 所以表示成 16 进制需要 32 个字符
            int k = 0; // 表示转换结果中对应的字符位置
            for (int i = 0; i < 16; i++) { // 从第一个字节开始，对 MD5 的每一个字节
                // 转换成 16 进制字符的转换
                byte byte0 = tmp[i]; // 取第 i 个字节
                str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
                // >>> 为逻辑右移，将符号位一起右移
                str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
            }
            s = new String(str); // 换后的结果转换为字符串

        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

    public  static boolean CompareTime(String bitth1,String  now)

    {
      String  str="";

      if(TextUtils.isEmpty(bitth1)||TextUtils.isEmpty(now))
      {
         str="日期格式不对";
         return false;
      }else
      {
        String bitth=bitth1.replace(" ","");
        if(bitth1.contains("长期"))
          return true;
        int year=Integer.parseInt(bitth.substring(0,4));
        int yearN=Integer.parseInt(now.substring(0,4));
        int month=Integer.parseInt(bitth.substring(4,6));
        int monthN=Integer.parseInt(now.substring(4,6));
        int day=Integer.parseInt(bitth.substring(6,8));
        int dayN=Integer.parseInt(now.substring(6,8));
        if(year>yearN)
        {
          str="身份证已过期，不允许办理此业务!";
          return false;
        }else if(year<yearN)
        {
          return true;
        }
        else
        {
          if(month>monthN){
            str="身份证已过期，不允许办理此业务!";
            return false;
          }else if(month<monthN)
            return true;
          else
          {
            if(day>dayN){
              str="身份证已过期，不允许办理此业务!";
              return false;
            }else
              return true;
          }
        }
      }
    }
}
