package com.renrenliao.im.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

/**
 * author：   ljy
 * date：     2017/8/19 0019 17:47
 * description: 网络状态工具类
 */
public class NetworkUtils {

    //    public static final int INVALID_NETWORK_TYPE = -1;
//    public static final int MOBILE_NETWORK_TYPE = 0;
//    public static final int WIFI_NETWORK_TYPE = 1;
    public static int CURRENT_NET_TYPE;//当前网络类型
    public static String CURRENT_NET_TYPE_STRING;//当前网络类型字符串
    public static final int NETTYPE_WIFI = 0x01;//wifi
    public static final int NETTYPE_CM = 0x02;//移动网络

    /**
     * 判断当前网络状态是否可用
     *
     * @param context context
     * @return 可用返回true, 否则false
     */

    // 是否连接了网络
    public static boolean isNetWorkAvailable(Context context) {
        ConnectivityManager mConnectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo gprs = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo ethernetInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        boolean isConnectedGprs = gprs != null && gprs.isConnected();
        boolean isConnectedWifi = wifi != null && wifi.isConnected();
        boolean isConnectedEther = ethernetInfo != null && ethernetInfo.isConnected();
        return isConnectedGprs || isConnectedWifi || isConnectedEther;
    }

//    /**
//     * 获取当前网络连接类型
//     *
//     * @param context context
//     * @return -1不可用，0移动网络，WIFI网络
//     */
//    public static int getNetWorkType(Context context) {
//        NetworkInfo mNetworkInfo = null;
//        if (context != null) {
//            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
//        }
//        return (mNetworkInfo != null && mNetworkInfo.isAvailable()) ? mNetworkInfo.getType() : INVALID_NETWORK_TYPE;
//    }

    /**
     * 当前是否为WIFI连接
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 当前是否为移动网络
     *
     * @param context
     * @return
     */
    public static boolean isMobileConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }


    /**
     * 获取apn 类型
     *
     * @param context context
     * @return 类型
     */
    public static String getAPNType(Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return null;
        }
        if (networkInfo.getExtraInfo() == null) {
            return null;
        }
        return networkInfo.getExtraInfo().toLowerCase();
    }

    /**
     * 获取当前网络类型
     *
     * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            CURRENT_NET_TYPE_STRING = "NONET";
            return CURRENT_NET_TYPE;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            String extraInfo = networkInfo.getExtraInfo();
            if (!TextUtils.isEmpty(extraInfo)) {
                CURRENT_NET_TYPE_STRING = "MOVE";
                CURRENT_NET_TYPE = NETTYPE_CM;
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            CURRENT_NET_TYPE_STRING = "WIFI";
            CURRENT_NET_TYPE = NETTYPE_WIFI;
        }
        return CURRENT_NET_TYPE;
    }

    public static String getNetworkTypeStr(Context context) {
        if (StringUtils.isEmpty(CURRENT_NET_TYPE_STRING)) {
            getNetworkType(context);
        }

        return CURRENT_NET_TYPE_STRING;
    }
}
