package com.app.baselibrary.commonUtil;

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 网络工具类；
 * author: pl
 * date: 2017-03-10 10:14
 */

public class NetWorkUtils {

    /**
     * 判断网络是否连接；
     *
     * @param context
     * @return
     */
    public static boolean isNetConnected(Context context) {

        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isAvailable())
            return true;
        return false;
    }

    /**
     * 判断当前是否是wifi连接；
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)
                return true;
        }
        return false;
    }


    public static boolean isxGConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null != cm) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (null != networkInfo && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                return true;
        }
        return false;
    }

    /**
     * 判断网址是否有效
     */
    public static boolean isLinkAvailable(String link) {
        Pattern pattern = Pattern.compile("^(http://|https://)?((?:[A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\\.)+([A-Za-z]+)[/\\?\\:]?.*$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(link);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }

    public enum NetWorkStatus {
        NETWORK_WIFI, NETWORK_2G, NETWORK_3G, NETWORK_4G, NETWORK_NUKNOWN
    }

    /**
     * 获取当前网络的类型（wifi/2g/3g/4g）
     * @param context
     * @return
     */
    public static NetWorkStatus getNetWorkStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return NetWorkStatus.NETWORK_WIFI;
            }else{
                return getNetWorkClass(context);
            }

        }
        return NetWorkStatus.NETWORK_NUKNOWN;
    }

    /**
     * 获取手机的网络制式，来判断是2g/3g/4g。再详细一点的话，可以获得运营商的信息；
     * @param context
     * @return
     */
    public static NetWorkStatus getNetWorkClass(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        switch (telephonyManager.getNetworkType()) {
            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 NetWorkStatus.NETWORK_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 NetWorkStatus.NETWORK_3G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NetWorkStatus.NETWORK_4G;

            default:
                return NetWorkStatus.NETWORK_NUKNOWN;
        }
    }
}
