package com.hh.lib_base.utils.device;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

public final class DeviceUtils {


    //获取设备SN码
    public static String getDeviceSN() {
        //如224F010T01400859
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            if (Build.VERSION.SDK_INT <= 28) {//28 is andoird 9.0 24-7.0 22-5.1
                serial = (String) get.invoke(c, "ro.serialno");
            } else {
                serial = (String) get.invoke(c, "persist.sys.hrSerial");//vendor.gsm.com.hh.xzx_readcard.serial
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(TextUtils.isEmpty(serial)){
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                try {
                    Runtime runtime = Runtime.getRuntime();
                    Process proc = runtime.exec(new String[]{"getprop", "ro.serialno"});
                    InputStream is = proc.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is), 8192);
                    serial = br.readLine();
                } catch (IOException ex) {
                    Log.e("Serial Number", "The Serial could not be found");
                }
            }
        }
        if (TextUtils.isEmpty(serial)) {
//            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//                // 注意清单文件加上READ_PHONE_STATE权限，不然还是会获取失败
//                com.hh.xzx_readcard.serial = Build.getSerial();
//            }else{
//                com.hh.xzx_readcard.serial = Build.SERIAL;
//            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                serial = Build.getSerial();
                if(!TextUtils.isEmpty(serial)&&!serial.equals("unknown")) {
                    // 判断非空和非unknown，则返回
                    return serial;
                }else{
                    serial = "";
                }
            }

            if(TextUtils.isEmpty(serial)) {
                serial = Build.SERIAL;
                if(!TextUtils.isEmpty(serial)&&!serial.equals("unknown")) {
                    // 判断非空和非unknown，则返回
                    return serial;
                }else{
                    serial = "";
                }
            }
        }




        if (TextUtils.isEmpty(serial)) {
            serial = "sn号获取失败";
        }

        return serial;
    }

    // 获取版本号
    public static String getAppVersionName(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //获取APP版本versionName
        String versionName = packageInfo.versionName;
        //获取APP版本versionCode
        int versionCode = packageInfo.versionCode;
        return versionName;
    }

    //获取版本code
    public static int getAppVersionCode(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //获取APP版本versionName
        String versionName = packageInfo.versionName;
        //获取APP版本versionCode
        int versionCode = packageInfo.versionCode;
        return versionCode;
    }


    // 获取包名
    public static String getAppPackageName(Context context) {
        return context.getPackageName();
    }

    // Android版本 7.1.2
    public static String getAndroidVersion() {
        // 如7.1.2
        String androidVersion = Build.VERSION.RELEASE;
        return androidVersion;
    }


    // 设备型号
    public static String getDeviceModel() {
        // 如F10
        String model = Build.MODEL;
        return model;
    }

    // 设备固件号
    public static String getDeviceDisplay() {
        // 如 F10_ALL_V1.0.3_202206110108
        String deviceDisplay = Build.DISPLAY;
        return deviceDisplay;
    }

    // 设备IP地址
    public static String getDeviceIP() {
        // 如 192.168.1.1
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
                        return addr.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            Log.e("getIPAddress", e.toString());
        }
        return "";
    }


}
