package com.gh.gamecenter.common.utils;

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

import com.therouter.TheRouter;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.core.provider.IConfigProvider;
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 navigation = TheRouter.get(IQuickLoginProvider.class);
        if (navigation != null) {
            return navigation.isOpenMobileData(context);
        }
        return false;
    }

    public static boolean isJiguangQuickLoginSwitch() {
        IConfigProvider configProvider = TheRouter.get(IConfigProvider.class);
        if (configProvider != null) {
            return configProvider.isJiguangSwitch();
        } else {
            return false;
        }
    }

    public static boolean isQuickLoginEnabled(Context context) {
        return isJiguangQuickLoginSwitch() && 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";
    }

}
