package com.hh.hhomc.utils;

import android.app.Activity;
import android.app.ActivityManager;
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.common.lib_base.utils.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);
//            serial = (String)get.invoke(c, "ro.serialno");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return serial;
//    }

    //获取设备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");
                AppLogUtils.e("SDK_INT <= 28--serial:"+serial);
            } else {
                serial = (String) get.invoke(c, "persist.sys.hrSerial");//vendor.gsm.serial
                AppLogUtils.e("SDK_INT > 28--serial:"+serial);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        // 兼容版本11的紫鹏
        if (TextUtils.isEmpty(serial)) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                // 注意清单文件加上READ_PHONE_STATE权限，不然还是会获取失败
//                serial = Build.getSerial();
//            }else{
//                serial = Build.SERIAL;
//            }


            if(TextUtils.isEmpty(serial)) {
                serial = Build.SERIAL;
                AppLogUtils.e("SDK_INT < O--serial:"+serial);
                if(!TextUtils.isEmpty(serial)&&!serial.equals("unknown")) {
                    // 判断非空和非unknown，则返回
                    return serial;
                }else{
                    serial = "";
                }
            }


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                try {
                    // 高版本走到这里，又没有权限，会报错。建议targetSdkVersion为22，避免后续降版本提示应用未安装
                    serial = Build.getSerial();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                AppLogUtils.e("SDK_INT >= O--serial:"+serial);
                if(!TextUtils.isEmpty(serial)&&!serial.equals("unknown")) {
                    // 判断非空和非unknown，则返回
                    return serial;
                }else{
                    serial = "";
                }
            }



        }

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


        return serial;
    }

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

    //获取app版本code
    public static int getAppVersionCode(Activity activity) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = activity.getPackageManager()
                    .getPackageInfo(activity.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 "";
    }


}
