package com.hh.arome.utlis.device;

import android.app.Activity;
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 com.hh.arome.utlis.log.AppLogUtils;

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.serial
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 兼容版本11的紫鹏
        if (TextUtils.isEmpty(serial)) {
            serial = Build.SERIAL;
        }


        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 "";
    }


}
