package com.gh.gamecenter.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.core.provider.IQuickLoginProvider;

public class NetworkUtils {

    /**
     * 判断是否有网络连接
     *
     * @param context 上下文
     * @return true 有网络连接 false 无网络连接
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            context = context.getApplicationContext();
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断WIFI网络是否可用
     *
     * @param context 上下文
     * @return true wifi可用 false wifi不可用
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            context = context.getApplicationContext();
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isConnected();
            }
        }
        return false;
    }

    public static boolean isWifiOr4GConnected(Context context) {
        String network = DeviceUtils.getNetwork(context);
        if ("WIFI".equals(network) || "4G".equals(network)) {
            return true;
        }
        return false;
    }

    public static boolean isWifiOr4GOr3GConnected(Context context) {
        String network = DeviceUtils.getNetwork(context);
        return "WIFI".equals(network) || "4G".equals(network) || "3G".equals(network);
    }


    /**
     * 判断MOBILE网络是否可用
     *
     * @param context 上下文
     * @return true mobile可用 false mobile不可用
     */
    public static boolean isMobileConnected(Context context) {
        if (context != null) {
            if (isNetworkConnected(context)) {
                String network = DeviceUtils.getNetwork(context);
                return !"WIFI".equals(network);
            }
        }
        return false;
    }

    /**
     * 判断是否打开数据流量(一键登录必须要打开流量才能获取到手机号码)
     * <p>
     * AuthnHelper的getNetworkType方法用于获取用户当前的网络环境和运营商，返回的是JSONObject，其中
     * operatortype字段获取网络运营商: 0.未知 1.移动流量 2.联通流量 3.电信流量，
     * networktype字段获取网络状态：0.未知；1.流量 2.wifi；3.数据流量+wifi
     */
    public static boolean isOpenMobileData(Context context) {
        IQuickLoginProvider quickLoginHelper = (IQuickLoginProvider) ARouter.getInstance().build(RouteConsts.provider.quickLogin).navigation();
        return quickLoginHelper.isOpenMobileData(context);
    }

    /**
     * 获取当前网络连接的类型信息
     *
     * @param context 上下文
     * @return 当前网络连接的类型信息
     */
    public static String getConnectedType(Context context) {
        if (context != null) {
            context = context.getApplicationContext();
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                switch (mNetworkInfo.getType()) {
                    case ConnectivityManager.TYPE_BLUETOOTH:
                        return "BLUETOOTH";
                    case ConnectivityManager.TYPE_DUMMY:
                        return "DUMMY";
                    case ConnectivityManager.TYPE_ETHERNET:
                        return "ETHERNET";
                    case ConnectivityManager.TYPE_MOBILE:
                        return "MOBILE";
                    case ConnectivityManager.TYPE_MOBILE_DUN:
                        return "MOBILE_DUN";
                    case ConnectivityManager.TYPE_MOBILE_HIPRI:
                        return "MOBILE_HIPRI";
                    case ConnectivityManager.TYPE_MOBILE_MMS:
                        return "MOBILE_MMS";
                    case ConnectivityManager.TYPE_MOBILE_SUPL:
                        return "MOBILE_SUPL";
                    case ConnectivityManager.TYPE_WIFI:
                        return "WIFI";
                    case ConnectivityManager.TYPE_WIMAX:
                        return "WIMAX";
                    default:
                        break;
                }
            }
        }
        return "NONE";
    }

    /**
     * 获取当前移动网络连接的类型信息（当连接的网络是移动网络时使用）
     *
     * @param context 上下文
     * @return 当前移动网络连接的类型信息
     */
    public static String getMobileNetworkType(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int networkType = mTelephonyManager.getNetworkType();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "4G";
            case 20://暂未适配AndroidQ，这里粗暴设置为20
                return "5G";
            default:
                return "unknown";
        }
    }

}
