package com.mogo.cloud.live.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;


import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * @author wangzhiyuan
 * @since 2018/2/7
 */

public class TelephoneUtil {
    public static String sn = "";
    public static String fotaVersion = "";
    public static String fotaDevice = "";
    public static String imei = "";
    public static String iccid = "";
    public static String vin = "";

    public static String getFotaDevice() {
        if (TextUtils.isEmpty(fotaDevice)) {
            String device = "";
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                device = (String) get.invoke(c, "ro.fota.device");
                if (!TextUtils.isEmpty(device)) {
                    device = device.trim();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            fotaDevice = device;
        }
        return fotaDevice;
    }

    public static String getFotaVersion() {
        if (TextUtils.isEmpty(fotaVersion)) {
            String version = "";
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                version = (String) get.invoke(c, "ro.fota.version");
                if (!TextUtils.isEmpty(version)) {
                    version = version.trim();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            fotaVersion = version;
        }
        return fotaVersion;
    }

    public static boolean getMobileDataState(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Android 5.0之前
            ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> conMgrClass = null;
            Object iConMgr = null;
            try {
                conMgrClass = Class.forName(conMgr.getClass().getName());//ConnectivityManager
                Field iConMgrField = conMgrClass.getDeclaredField("mService");//IConnectivityManager
                iConMgrField.setAccessible(true);
                iConMgr = iConMgrField.get(conMgr);//获得ConnectivityManager的IConnectivityManager实例

                Class<?> iConMgrClass = Class.forName(iConMgr.getClass().getName());
                Method getMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("getMobileDataEnabled");
                if (getMobileDataEnabledMethod != null) {
                    getMobileDataEnabledMethod.setAccessible(true);
                    boolean result = (Boolean) getMobileDataEnabledMethod.invoke(iConMgr);
                    Log.d("TelephoneUtil", "getMobileDataEnabled = " + result);
                    return result;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // Android 5.0之后（包含）
            TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
                if (null != getDataEnabled) {
                    boolean result = (Boolean) getDataEnabled.invoke(telephonyService);
                    Log.d("TelephoneUtil", "getDataEnabled = " + result);
                    return result;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void setMobileDataState(Context context, boolean enable) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Android 5.0之前
            ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> conMgrClass = null;
            Object iConMgr = null;
            try {
                conMgrClass = Class.forName(conMgr.getClass().getName());//ConnectivityManager
                Field iConMgrField = conMgrClass.getDeclaredField("mService");//IConnectivityManager
                iConMgrField.setAccessible(true);
                iConMgr = iConMgrField.get(conMgr);//获得ConnectivityManager的IConnectivityManager实例

                Class<?> iConMgrClass = Class.forName(iConMgr.getClass().getName());
                Method setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                if (setMobileDataEnabledMethod != null) {
                    setMobileDataEnabledMethod.setAccessible(true);
                    setMobileDataEnabledMethod.invoke(iConMgr, enable);
                    Log.d("TelephoneUtil", "setMobileDataEnabled = " + enable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // Android 5.0之后（包含）
            try {
                TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Method setDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
                if (null != setDataEnabledMethod) {
                    setDataEnabledMethod.invoke(telephonyService, enable);
                    Log.d("TelephoneUtil", "setDataEnabled = " + enable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static String getSerialNumber() {
        if (TextUtils.isEmpty(sn)) {
            String serial = "";
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                serial = (String) get.invoke(c, "zhidao.serial");
                if (TextUtils.isEmpty(serial)) {
                    serial = (String) get.invoke(c, "gsm.serial");
                }
                if (TextUtils.isEmpty(serial)) {
                    serial = (String) get.invoke(c, "ro.serialno");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (serial.startsWith("ZD") || serial.startsWith("XT")) {
                    sn = serial;
                } else {
                    return serial;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sn;
    }

}
